Is it okay to use Get All Actors Of Class like this?

I have found myself using the “Get All Actors Of Class” node to grab a reference to other blueprints in my project so that my Blueprints can communicate with one another and so that I can change variable, fire custom events between my Blueprints.

So when I need ‘PlayerStats Blueprint’ to talk to ‘Third Person Character Blueprint’ and vice versa:

  • Creating a variable of type PlayerStats called “PlayerStats”
  • Get All Actors Of Class, selecting (PlayerStats) in the drop drown, getting first item in array and setting it to my newly created PlayerStats variable.
  • Now I have access to all variables, events etc of PlayerStats in ThirdPersonCharacter when I get my PlayerStats variable.

I’ve attached an image

to demonstrate. I just want to know if this is okay for me to do. I understand Get All Actors Of Class is supposed to be expensive, but the way I see it, I’m getting the first item in the array so it’s surely not so bad? Or is the issue a scope problem? Or is there no issue with doing this? If this approach to Blueprint communication is wrong, does anyone know the CORRECT way I can communicate with any Blueprint actor that I need to in my project?

Thanks in advance!

Since you mentioned you already knew the performance cost of GetAllActors node, I don’t have to stress the overhead you may run into. A few things to note here: you might wanna add ‘Delay’ before calling GetAllActors node since it is possible that Event Begin Play fires right before your Player Stat class actor(s) is created. This is problem if you have too many instances of that particular class created in the level. Use “Is Valid?” to check to ensure the obj ref is not null.

As much as I hate that node (ugh - it’s often used as a gateway to bad OOP), I saw a thread in the forums regarding its performance which is surprisingly decent (hash tables). I never run into a situation where I had to use it, though (I do use it in a debugger to break things on purpose). There is always a more elegant / efficient / logical way.

If you only have one PlayerStats actor, I’d say it’s OK to use it. With GetAllActors returning an array, you can’t be sure which actor is which in case you have more than one. I also felt like the object order was based on the weather outside :wink:

Why don’t use a direct reference instead? If the PlayerStats actor is spawned dynamically, set up a reference right there and send it to the character via a Cast. If you placed it in the Level manually, you can do the exact same thing.

Also, all what ClarkKhant said.

Hey there, are the Player Stats spawned in runtime, or are they placed in the level? If they are spawned in runtime, then you could do Get Player Pawn, cast it to ThirdPersonCharacter and set the Player Stats variable from there. If they are placed in the level, just set the player stats variable to be instance editable and selected them from the dropdown in your player instance. Either way you probably don’t need a get all actors of class for that.

They are placed in Level actually and I never knew you could simply select the reference from the dropdown. That’s a much more elegant solution that GetAllActors!

Thank you for your help :slight_smile:

No problem :slight_smile:

I have also been thinking, after watching this Unreal video: UE4 Performance and Profiling | Unreal Dev Day Montreal 2017 | Unreal Engine - YouTube, specifically at 33mins where he says you shouldn’t hard reference - is what I’m doing really bad practise? If each Blueprint is hard referencing one another then surely I’m loading everything all at once the moment I load the first one? For example, loading ThirdPersonCharacter is then loading PlayerStats Blueprints which in turn loads a Time Blueprint which in turn loads other Blueprints. Eventually when my project gets big, I will end up accidentally loading in tons of Blueprints.

Am I wrong in assuming this? And If I’m not, what is the correct way to share Events and Variables between blueprints that won’t cause this issue?

Thanks again for your help.

Ideally you want to avoid hard references, to make the best suggestion i would need to know more about what player stats is supposed to do.

Player stats is a seperate Blueprint that is in charge of storing variables about the player like Health, Cash, Stamina. It turns those variables into Enumerations which are picked up by the UI. So it’s really important that ThirdPersonCharacter has access to it’s variables and vice versa.

Why dont you create a custom componentto hold those variables instead?

I guess because I’m doing things to the variables in the event graph like calculating Stamina and Duration since last slept etc.

You can do that with a component as well.

Nice! Okay well I will try that if that’s a more performant way to go about it. Thanks!

Components are more lightweight than actors, and you dont need to do hard references anymore because its part of the actor.