6
\$\begingroup\$

I'm coding a simple maze game in UE4 and I have everything I wanted to implement done except having an obstacle in the scene. What I have is a simple Obstacle class which is a child of an Actor class.

Now in the scene I have a Blueprint which is a child of the Obstacle class and this Blueprint is placed in the maze. What I'm trying to achieve is when the player collects so many objects (let's say 10) then the Obstacle will be destroyed but I don't know how to get a reference to that Obstacle object in my code.

AObstacle* Obstacle /////// This is where I don't know how to initialize the object
if (MyCharacter->collectedPickUps >= requiredAmountOfPickups)
{
    Obstacle->Destroy();
    Obstacle->isActive = false;
}
\$\endgroup\$

1 Answer 1

6
\$\begingroup\$

I have managed to sort it out. Basically I have to use an Actor Iterator which iterates through every Obstacle object in the game world. Here is the code:

for (TActorIterator<AObstacle> ObstacleItr(GetWorld()); ObstacleItr; ++ObstacleItr)
{
    if (MyCharacter->collectedPickUps >= requiredAmountOfPickups)
    {
        ObstacleItr->Destroy();
        ObstacleItr->bIsActive = false;
    }
}
\$\endgroup\$
5
  • \$\begingroup\$ There must be a solution better than this, in Unity you can reference that Actor(GameObject) by exposing a GameObject variable, then dragging the desired GameObject to that variable, just don't know how to make it in UE4 :S \$\endgroup\$
    – Ahmed U3
    Commented Jun 6, 2015 at 16:46
  • 1
    \$\begingroup\$ After weeks of struggling, just make a public UPROPERTY AActor* targetActor, in the editor, when selecting that object select targetActor intended actor from the dropdown list; \$\endgroup\$
    – Ahmed U3
    Commented Jul 2, 2015 at 18:05
  • \$\begingroup\$ @AhmedU3 Thank you so much! This should be the answer! \$\endgroup\$ Commented Feb 8, 2016 at 4:56
  • \$\begingroup\$ @Ahmed, could you please elaborate? \$\endgroup\$
    – pookie
    Commented Feb 10, 2016 at 17:28
  • \$\begingroup\$ Okay; In unity, to refer a gameobject in the scene directly you can declare a "public GameObject targetObject", then it becomes easy to drag&drop it in the inspector. To make the same in UE4, you need to declare "public: UPROPERTY(...) AActor* targetActor;" by building this, if you go to the editor > select your object > in the details isA you'll find a dropdown list which contains all the actors \$\endgroup\$
    – Ahmed U3
    Commented Feb 11, 2016 at 22:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .