5
\$\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\$
5
\$\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\$
  • \$\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 Jun 6 '15 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 Jul 2 '15 at 18:05
  • \$\begingroup\$ @AhmedU3 Thank you so much! This should be the answer! \$\endgroup\$ – markasoftware Feb 8 '16 at 4:56
  • \$\begingroup\$ @Ahmed, could you please elaborate? \$\endgroup\$ – pookie Feb 10 '16 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 Feb 11 '16 at 22:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.