Hello, I’m in my project code and I’ve several AActor subclasses for my actors.
But How may I exchange data between them ?
In other words, is there a way to get, from an AActor subclass, the list of all AActor instances in the game. By the name, I will then be able to get the instance I’m searching for and call a method.
From what I’ve seen before, this functionality is not present in UE4 by default. What you can do is iterate through the list of the actors in a level and retrieve the ones you need. Another option would be to add the actors which you spawn to your own list, hence, you can easily access them when needed.
Just a small note, if you are considering iterating often, to make things a little more efficient, here is what I recommend:
On the constructor - or better yet, on the initializer/spawn function - make a call to a function - probably store it in the current Game mode - which adds your actor to a map. Something like this :
Something like that - that wasn’t tested at all, just wrote it here so there might be problems.
Just keep in mind you’ll have to cleanup (delete) the initialized TMaps - second-level TMaps.
The handlers are optional, if you want to manipulate/ know exactly which instance is there, you just add the instance name where the handler is. Or if you don’t want manipulation, just make it a TArray (the second-level TMap).
Lastly, don’t forget to remove them from the map when they unspawn
Do you mean exchange data locally or via networking?
If we assume it’s locally:
If the data exchanges happen sporadically throughout running the game, iterating through all actors of the world each time isn’t very efficient.
Why not, when your actors have been instantiated, you give them an ID?
Say for example you make a class UActorIDHandler.
UActorIDHandler has lists of references to your actors:
Edit: This is just a naive solution, UE MOST likely already has a solution for this. But I’m pretty new to the engine. You might wanna check out Tags or FNames though.