Announcement

Collapse
No announcement yet.

Templated Blueprint functions

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    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.
    https://github.com/iniside/ActionRPGGame - Action RPG Starter kit. Work in Progress. You can use it in whatever way you wish.

    #2
    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.
    It is by will alone I set my code in motion.
    It is by coding that thoughts acquire speed, the hands acquire shaking, the shaking becomes a warning.
    It is by will alone I set my code in motion.

    Comment


      #3
      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!

      Comment


        #4
        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.

        Comment


          #5
          I found a way to receive wildcard USTRUCTs without creating custom nodes. I posted an example here: https://forums.unrealengine.com/show...131#post206131

          Comment


            #6
            I'm also wondering about this functionality. I have the exact same problem as Iniside.

            Comment


              #7
              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.

              Comment


                #8
                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.
                | Savior | USQLite | FSM | Object Pool | Sound Occlusion | Property Transfer | Magic Nodes | MORE |

                Comment


                  #9
                  Originally posted by iniside View Post
                  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.
                  I have the exact same problem as Iniside.

                  Comment

                  Working...
                  X