Template/Wildcard return type for Blueprints

Well, I have been trying to understand how to do this for a while now, but so far, have failed…

Simply put, I want a template function that is accessible in Blueprints, which, from what I heard, is not quite possible.

Still, there are nodes like “Construct Object of Type” or “Spawn Actor of Type” which take an UClass as input and spits out an object of that type. How can I achieve the same behavior? Maybe I have to do some magic using UObjectProperty instead, but for now it just feels like shooting in the dark.

In what little source code I saw, it seems templates are somehow used. I am still going to do some more research, but I figured I might as well just ask here in case someone already went through this ordeal.

Cheers

Hey there, if you create a function that takes a Class as the parameter and you can do a lot of stuff with it, call SpawnActor, etc using that class. The return type of the function needs to be an Object if you are spawning an object or Actor if you are spawning an actor. I don’t think you can do dynamic casts in blueprints.

Yeah not sure if you can do that without changing the source code.

Yeah, currently I am returning something generic and casting it somewhere else, but I wanted to eliminate this step and automagically return the object of the correct class.

Indeed… going by classes such as “K2Node_SpawnActorFromClass”, custom code would be necessary, unfortunately

Hey Guys, I figured this one out. I stumbled across this thread while searching for the answer myself, so though I should put in down here for all to see.

As with everything in Unreal, it’s solved using a meta specifier, in this case it’s DeterminesOutputType

UFUNCTION(BlueprintCallable, meta = (DeterminesOutputType = "actorClass"))
class AActor* SpawnActor(class TSubclassOf<class AActor> actorClass);

As you can see the DeterminesOutputType = “actorClass” is pointing to the parameter of the same name “actorClass”. This will morph the returned AActor* that was passed in.

258962-capture.png

Hope this helps!
~GameQ

5 Likes

That’s exactly it!

Thank you very, very much!

Based