Accessing variable in different actor

Hello, guys. I created a new project for this question. I have 2 actors, ActorOne and ActorTwo. I would like to access the variable in ActorOne and print it in the tick function in ActorTwo. I can’t seem to find a way to do this. Thank you in advance guys.


1_1

Then the question is, which ActorOne? You can place multiple ActorOne instances in a level. So you somehow need to be able to tell your ActorTwo instance which ActorOne instance you want to print that variable from, as each ActorOne might set that number to be something different, like if it was its health or time alive or team number.

The standard way of doing that is by creating a reference which can be set on the actor. Like this:

UPROPERTY(EditAnywhere, BlueprintReadWrite)
TSoftObjectPtr<AActorOne> MyActorOneReference;

Then in the Unreal Editor after you’ve placed both actors in the level, in the details view for your ActorOne instance you can see that there’s a variable called MyActorOneReference with a drop down list to select the other actor, or the Eye Dropper button next to it which you can use to click on the ActorTwo instance in the level.


Another option you might be trying to do is to make your num property public and static, then any other class can directly reference it globally from the class itself, but I generally try to avoid static variables for many reasons regarding general code quality that I won’t get into here.

If it’s a one to many relation of actors (1 actor one and many actor 2) then you could also consider a delegate with parameters in actor one and then bind it in actor two.

You could then call broadcast() on the delegate in actor (in it’s tick function) to all bound actors passing in the needed variables.

Not sure if the overhead beats a direct reference. The staff probably know the performance numbers better.

Hi! To answer your question, there is 1 ActorOne and 6 ActorTwo in the Unreal Editor. I would like for all the 6 ActorTwo to access the variable num that is located in ActorOne.

How to use this broadcast function?

Delegates.zip (24.9 KB)
Just generate the visual studio project (ue 5.1)

Made a quick example scene.
Of course You can pass in many different variable types into the delegates and even many of them.

You can check the unreal engine documentation for all of the combinations.

Thank you. I will try to test this and will update you later on. Thank you!!