Is there a way inside an actor to keep a reference to another actor?

I thought about using a simple pointer to an actor, ATarget, inside the big, main actor, ACannon, to keep track of ATarget’s position.

But it seems that the traditional C++ way of instantiating a ATarget class object with a constructor inside the ACannon constructor isn’t the right way to do. When initializing the ATarget in the ACannon’s constructor, it failed to do so with the use of ObjectInitializer.

Could anyone give hints on what I should do here instead? Thanks in advance.

Constructor only valid for create component for the class, all the reference actor instance need to be create at runtime. You should put that instantiate in BeginPlay(). If not, you can add a ChildActorComponent in the ACannon, and set class to ATarget.

Could you show me an example of instantiating in BeginPlay()? This is just in case I wasn’t able to use ChildActorComponent for specific reasons, and I wouldn’t know of any other ways to instantiate actors. Thanks.

Ok, you can use GetWorld()->SpawnActor to spawn your ATarget in ACannon.

Check this one if you have problem.

Thank you.