Download

How to use Actor Class as C++ type?

I’ve copy & pasted this function from the answers hub (cannot find exactly where now).


UClass* UMyStatics::FindActorByName(const FString& ClassName)
{
	check(*ClassName)

	UObject* ClassPackage = ANY_PACKAGE;

	if (UClass* Result = FindObject<UClass>(ClassPackage, *ClassName))
		return Result;

	return nullptr;
}


It works just fine, however I need to pass result of this function to a blueprint node “Spawn Actor from Class”. Sadly when I try to connect it, it wont as “Object Class is not compatible with Actor Class”.

I’ve tried to check source code of that node and I found declaration of that pin


UEdGraphPin* ClassPin = CreatePin(EGPD_Input, K2Schema->PC_Class, TEXT(""), AActor::StaticClass(), false, false, FK2Node_SpawnActorFromClassHelper::ClassPinName);

However I am totally clueless (rookie here) how to use AActor::StaticClass() as a return type of the function. Is that even possible?

If you’re trying to spawn an actor, you should use GetWorld()->SpawnActor(). You’ll need to pass in the actor you want to spawn, along with transform you want the actor to have once spawned in.

As for your issue, you can always cast the object to an actor, by dragging out and typing “Cast to Actor”. You can then connect the node up with no problems.

Amazing, I’ve totally missed the node Cast To Actor Class, that’s exactly what I needed. Thanks !

[MENTION=629930]Daniel K.[/MENTION] Since your function is specific to actors, you should just change the return type of your function from UClass* to TSubclassOf< AActor >. Then the cast will be done implicitly in code and there will be no need to cast in blueprint.

It’s not a very well written function - I’d suggest renaming it to FindActorClassByName, and you can also replace the if and two return statements with simply:


return FindObject<UClass>(ClassPackage, *ClassName);

Ah, that’s what I have been missing, how to do subclass in the code :slight_smile: Thank you.

I am wondering why there is actual difference between AActor::StaticClass() and TSubclassOf< AActor > except that former one is not type. Can be TSubclassOf< AActor > passed same way around? Kinda confusing having two such different syntaxes for almost a same thing.

What’s also confusing me, that I can call FindObject<UClass> and yet have a return type of TSubclassOf<AActor> and it will autocast somehow?

I’ve renamed function, makes sense. Regarding the if condition, where can I find documentation to FindObject? I mean how can I know it returns nullptr in case of a failure?

TSubclassOf< X > is largely interchangeable with UClass* in C++. The former can be constructed from the latter (which answers your second question), and just does some runtime type checking, along with compile time checks if you try to assign a TSubclassOf< Y > to a TSubclassOf< X >. You’re better off using this than a raw UClass* in 99% of cases.

StaticClass() is a method that returns a pointer to an instance of a UClass.

Pretty much any UE function returning a pointer will use a nullptr return in the case of a failure. You can search the UE codebase on github or in Visual Studio if you want to check the code. Documentation is very limited.