Access variable from randomized actor

Hello! I have a code to randomize 2 actors, and all the actors are in an array. All the actors have the same variable in them named “Health”.

I want to have the actor with the lowest health to go first, however I can’t get to the variables from my level blueprint.

Can you tell more about it

If you want the ‘health’ variable
You can drag the pin from return value of spawn actor node

If the actors all derive from a common base class where Health is defined, then change the type of your ActorArray to be an array of that base class (instead of Actor). Then change the “Actor” variable type as well. Now the output of SpawnActor should automatically switch to your base type and you can get its Health. Alternatively, don’t change anything and Cast the result of SpawnActor to your base class and get Health from there.

If your actors do not have a common ancestor, but still have a Health variable by convention, you can make an Interface. Declare a method “GetHealth” in the interface, that returns int (or float). Then implement the interface in all your actors that have a health variable and hook up the method so it returns their own variable. Then after SpawnActor you can cast to interface and use GetHealth.

2 Likes

Idk if that would work because the actors are all separate actors

I think I have an idea for how to implement the interface for it now. Thanks! If it works I’ll set you as the solution :slight_smile:

You can give tag to the characters

Then use get all actors of class with tag node
They u will have array of all AIs with that tag

I’ve done this now, but idk how to implement it into my code. If I have, lets say my “Blue” Character gets picked, who has the lowest health, and my “Red” Character, which has slightly higher health, how can I know which one was picked, then subtract them from each other

create diffrent ai characters
low health , blue colour give tag (low health)
high health , red colour give tag (high health)
and so on …

then run that custom event
with tag low health → set health
same for other characters

I don’t get it, very sorry