I made an editor widget utility. Within that utility in my header file I defined the private variable
TMap<FString, AActor*> ActorNameCache;
In my code I have the following:
AActor* Obj = FindActorByName(Box->Name);
if (Obj == nullptr || !IsValid(Obj))
It all works fine, except after I have loaded the cache, if I then delete an object in the editor then the line:
if (Obj == nullptr || !IsValid(Obj))
is crashing with the error
Unknown() Address = 0x180ecb684 (filename not found) [in libsystem_platform.dylib]
Here is my find actor function:
AActor* UEditorTools::FindActorByName(const FString& Name)
{
FString ModifiedName = Name;
ModifiedName.ReplaceInline(TEXT(“/”), TEXT(““));
ModifiedName.ReplaceInline(TEXT(”.“), TEXT(””));
ModifiedName = ModifiedName.TrimEnd();
// Check the cache first
if (AActor** CachedActor = ActorNameCache.Find(ModifiedName))
{
if (CachedActor && *CachedActor && IsValid(*CachedActor) && IsActorInWorld(*CachedActor)) // Added IsActorInWorld check
{
return *CachedActor;
}
else{
return nullptr;
}
}
// Get the current world context
UWorld* World = GetWorld();
if (!World)
{
return nullptr; // Ensure the world is valid
}
// Iterate over all actors in the world
for (TActorIterator<AActor> It(World); It; ++It)
{
AActor* Actor = *It;
if (Actor && Actor->ActorHasTag(FName(*ModifiedName))) // Converts FString to FName correctly
{
ActorNameCache.Add(ModifiedName, Actor); // Cache the result
return Actor; // Return the found actor
}
}
// If no actor found, cache the result as nullptr to avoid repeated searches
ActorNameCache.Add(ModifiedName, nullptr);
return nullptr; // No matching actor found
}