referencing actor by unique name c++

as it is in the title, how do i reference an existing actor by it’s name (ID). lets say referenced actor has the unique name called “THE_ACTOR” (like how you would see under “Label” in the “world outliner” box)

AActor* Actor = "THE_ACTOR" //this is probably wrong but how would you do it instead?

assume the actor being referenced will definitely be in the level/world . i know this is a bit hardcoding but i kinda need to know how to do it for now. (better solutions are also welcome)

This is C++ not some magical scripting language, pointer (Type*) is a integer containing memory address to variable which you can later access with * or → operators. It points to actor in memory and pointer alone can’t do anything. Blueprint is no different, the blue object work same way… so you should be already used to that ;p If you confused with level blueprint, it can do that because level blueprint is part of the map and it can directly reference to actors in it, any actor can do that in property editor. But class alone don’t know what level it gonna be spawned in, so it can not reference any actor.

You need to use other functions to find actor, best practice is to store the reference right after spawn or make actor register it self to other class or reference actor in property editor (which should be you no.1 tactic if you want to link 2 actors together in level, like a switch with the light). If object is more global and has some major role in UE4 gameplay framework theres usually functions ot get it. Searching for actors is more expensive process as engine needs to loop thru all the actors in the world to find it. You got functions from blueprint for searching:

But in C++ you can do more optimize the Blueprints allows, using iterators as you can break the iteretion loop imidietly after you find actor you looking for, saving CPU time:

But again there lot better ways to manage references in UE4, use searching only if anything else does not work.

Theres also soft refrences which let you refrence actor by path and does not guarranty that actor will exist but it will check if and get it if needed, they also better then searching: