Hi guys,
When I add newly created c++ actors to my map through the editor, how can I access them through code? Is there a global registry of class which exist?
Maybe you are looking for UWorld ?
You would have to keep a pointer to the object somewhere. Depends on what you are doing, and how you would want to access it.
Alternatively, you can look up the specific actor using GetAllActorsOfClass() UGameplayStatics::GetAllActorsOfClass | Unreal Engine Documentation [HR][/HR]
Lookup Sample:
// If looking for AGrenade actors
TArray<AActor*> FindActors;
UGameplayStatics::GetAllActorsOfClass(this, AGrenade::StaticClass(), FindActors); // FindActors now has a pointer to all AGrenade objects and any subclasses of it
for(auto& Grenade : FindActors)
{
// You can compare a name or a tag to find a specific Grenade
// Do something with Grenade
}
[HR][/HR]
Caching sample (WARNING: You will have to deal with nullptrs if actors are destroyed/deleted, so be careful here):
// If storing references to AGrenades, create a container of AGrenades in the GameMode
TArray<AGrenade*> GrenadeReferences;
// Spawn AGrenade from GameMode and add to GrenadeReferences
AGrenade* NewGrenade; // Use spawning code
GrenadeReferences.Add(NewGrenade);
You can now get your GameMode class and access GrenadeReferences to find your Grenade Actor. And since it’s a container, you can still iterate it similar to the previous example. [HR][/HR]
Hope this helps!