Templated Blueprint functions

Is it possible to create templated blueprint functions ?

Like I want to have generic function to retrieve row from DataTable, exposed for Blueprint.
I can do it easy enough, when I hardcode data types.

But what If I want to specify in blueprint type of data that function will return ?

template <struct T>

Doesn’t work obviously.

Not sure, but you could probably make a class that accepts a class UPROPERTY, and go from there, maybe using the template internally from the class “parameter”. Kinda how the “Spawn Actor With Class” blueprint node works, where you specify a class.

Unfortunately, templated function support is pretty limited at the moment. We’re looking to fix this up in the next few months, and make a generic solution for wildcard parameters, but for now, they have to be handled at the node level. We have support for a wildcard pin type (UEdGraphSchema::PC_Wildcard), which we currently use to get template-like functionality for array functions. You can see how we implement it in UK2Node_CallArrayFunction. The gist is that when you connect something to a wildcard slot, we propagate that type to the rest of the wildcard dependent pins. Under the hood, we pass-by-reference, and use the CustomThunk keyword in order to write our own thunk-layer and read the pointer off the stack and coerce the type.

You could do something similar, but much less invasive, if you made your own subclass of UK2Node_CallFunction, and then allocated a wildcard parameter. Then, in NotifyPinConnectionListChanged(), if the pin that was connected was a wildcard, you could search for an appropriate UFunction for your type, and then call UK2Node_SetFromFunction. That’s not purely templated, but would allow you get a similar effect!

1 Like

Is there any update to this? I’m trying to write a function that takes a wildcard UStruct so I can serialize any struct to JSON (including Blueprint structs) without having to write specialized code for each one of them, however It doesn’t seem to be possible to get functions to accept the UProperty or UStructProperty as parameters.

The problem with using CustomThunk and creating a UK2Node_CallFunction is that I couldn’t find any documentation on how it works and the sheer amount of boilerplate code required for a custom blueprint node is frightening. I consider myself fast at learning other people’s code but the nodes have so many virtual functions and the source for nodes that use wildcard variables all have several hundreds of lines I would probably have to drop everything else I’m doing for a week or so to grasp enough of this.

I found a way to receive wildcard USTRUCTs without creating custom nodes. I posted an example here: [Tutorial] How to accept wildcard structs in your UFUNCTIONs - Community & Industry Discussion - Epic Developer Community Forums

I’m also wondering about this functionality. I have the exact same problem as Iniside.

I know this is an old post but this is the first search result from google and has no information for 5 years. Would be nice if we could get an update here or a link of where to go.

It is possible, I’ve done it few months ago.
I have implemented “Optional” and “Variant” types in Blueprints using wildcards and custom thunk code.
​​​​​​
Very useful when I need nullable ints or when I was storing a float and now I need ro store a vector; instead of creating both a float and a vector property, I use the same variable to save either one of the values in Actor Blueprint.

I have the exact same problem as Iniside.

I know this post is multiple years old at this point, but I’ve been looking for exactly this (Blueprint exposed “Variant” type) forever. Would it be possible for you to share this code if you still have it or remember how you wrote it (assuming you even see this after all this time)?

1 Like

I don’t remember in which machine it is.
Also I think it only support basic blueprint types like Vector, float, color, name, etc… And their arrays.