Download

How to get a specific Actor Object from world?

Hi I’m learning UE4 right now.

For my personal research. I found it can be used by ActorIterator with for loop. like below.

for (TActorIterator<ATargetActor> It(GetWorld()); It; ++It)
	{
		ATargetActor* TActor = *It;
		if (TActor == nullptr)
		{
			continue;
		}

		TActor->ChangeMaterial();
	}

But I want to get a single actor not like for loop.
I know If I create an Actor named unque can be work like what I want. But I’m just want to know is there other way to do that SIMPLE.

If NOT, are using ActorIterator is the best way for now on Unreal Engine ?

ActorIterator isn’t really a great way to find things. It’s very brute force (e.g., slow). There are multiple ways to find Actors:

  1. Through the physics scene ( Queries / Sweeps / Raycasts ).
  2. Have the object register itself when it’s created with some Global object (a subsystem or some such) and then query that subsystem.
  3. Direct references you setup through the Editor in a level (literally assigning an instance to a field on some object).

Which approach is best depends on your situation, but hopefully that gives you some starting points to consider.

2 Likes

Great answer. Listen to this guy.

1 Like

Thank you. Nice adv.
But I’m kinda newbie so…

Is there any simple example or documentation about these three ways ?

  1. Let’s say you want to interact with the object in front of the player. This is where a line/capsule trace would be perfect.

  2. When the object spawns, it automatically registers itself in a list somewhere. The list could be anywhere, but most commonly you want to put them in some kind of a manager. Subsystems are very good starting points for manager objects.

  3. Direct reference, e.g. you have a lever object in the game, and you directly reference the door object to it in the editor. The lever would have a UPROPERTY(EditAnywhere) AActor* Door property which will be visible in the editor so that you can set the reference yourself by hand.

You’ll find documentation on traces, subsystems etc. At the end of the day, this is just software design, so there is no definitive “this is how to do it” guide.

2 Likes

If you want an actor with a specific name or type you do it by iterator. Keep a reference so it only has to be done once.

If you want the actor under the mouse or crosshair then use ray cast.

If you want to know when actors enter or leave a specific area use a trigger volume.

Can’t think of any other use cases but there may be some …