Use GetAllActorsOfClass

Hi all !

I have a problem using GetAllActorsOfClass function. UE4 make a crash.
I use it as (.cpp):

UMySingleton * UMySingleton::Instance = nullptr;

void UMySingleton::TestFunction()
{
   TArray<AActor*> list;
   UGameplayStatics::GetAllActorsOfClass(GetWorld(), MyActor::StaticClass(), list);
}

When this code is called from my personal PlayerController, there is no problem. But when the same code is called inside a singleton (UObject), the crash comes.
Here is my way to get my singleton before calling the function (.h) :

	static UMySingleton * GetMySingleton()
	{
		if (Instance == nullptr)
			Instance = NewObject<MySingleton>();
		return Instance;
	}

Does anyone has an idea about that ?
Thank you !

MyActor is an actor. Code is called in a UObject function.

I don’t fully understand. It sounds like you are trying to use getAllActorsOfClass and pass it a uObject. Uobjects are not actors. So there are no actors of the class

if uObject is calling getStaticClass would it not pass it a uObject Class instead of an actor?

AActors are extensions of UObject, So While Actors know all about everything in the UObject. UObjects don’t know about everything in the AActor.

That being said, I don’t think getWorld is a function UObject knows since it’s defined in AActor not UObject. So that function simply will not exist in your singleton UObject.

I’m not clear :frowning:
Objects I want to get are actors. No problem. Moreover, when I use the same code in MyPlayerController.cpp or MyGameMode.cpp, I get all actors I want.

I would like to write this code in MySingleton (which is an UObject) and work with actors I get in this singleton. But when I execute the code in MySingleton, I have a crash.

You’re [REDACTED] right : GetWorld() was unknown. It works now, thank you !

STAFF EDIT: Please refrain from using profanity on the Answerhub.