Delegates as Parameter

Hello, iam working on a Userinterface in Blueprint and have a hard time dealing with callbacks of buttons etc.

Is there any easy way to supply a delegate reference to a function via a parameter or any way to give my function something so i can bind events in that function?

It’s funny how there are some completely dead topics on this site isn’t it? I can never get anyone to give me answers related to either delegates or animation.

yeh this sucks the way delegates are right now is completely useless :confused: ill try and get this working in c++ maybe i can add a delegate reference datatype or whatever

not true, you can create an event dispatcher and bind to that.

You need to create an event dispatcher as the ‘variable’ you will store your function in. This is what you will call. Then, in the other class you create a custom event with the same signature you want to be called. get a handle to the first class and find the ‘bind to event x’ option. then pipe the delegate handle from the custom event into the bind node.

now when you call your function the custom event will be called instead.

you can do it in blueprints. see my answer below.

Well, there is a way, and it is actually not that difficult. If you have a set list of functions that you are choosing from, and suspect you do, here is the way I would handle it.

  1. On the event host, create all of your events.
  2. Create event dispatchers for each event and bind them. You will likely need to do that event binding either in the construction script or in the event begin play script.
  3. Create the function you want to call, let it take an int (or enum) as input
  4. Inside the function, use a switch on int to send call the correct event dispatcher.

You can even use variations on this to bind/unbind events from event dispatchers and such. Up to you.

Tony

What if I don’t want to store the function, and just want to pass it into another function which calls it, calculates things and returns without storing it?

That’s not actually answering the question, that’s called making a work-around.

I agree, this is what I need.

Yes, but you can’t do it using only blueprints: only functions created in C++ can accept delegate parameters. Blueprints can only create event dispatchers, which are multicast delegates.

For UI, you create a dispatcher in your custom widget blueprint. Then when you add that widget component to another widget, you can bind the events directly from the design view, just as you do with the built-in button component: your dispatchers are listed at the bottom of the details panel, with buttons that will create the event nodes for you.

Can you comment on how it would look in C++ (snippets of source code perhaps)? Thanks!

Sure, here’s some snippets from our project:

Declaring the delegate type:

DECLARE_DYNAMIC_DELEGATE_OneParam(FOnlineUserImageRetrievedDelegate, UTexture2D*, Texture);

Declaring a method that takes the delegate as a parameter:

/* Retrieves the user thumbnail texture*/
UFUNCTION(BlueprintCallable, Category = OnlineUser)
void RetrieveThumbnail(const FOnlineUserImageRetrievedDelegate& Callback);

Declaring a variable to store the delegate:

FOnlineUserImageRetrievedDelegate ImageRetrievedDelegate;

The implementation does something like this:

void UOnlineUser::RetrieveThumbnail(const FOnlineUserImageRetrievedDelegate& Callback)
{
    ImageRetrievedDelegate = Callback;
}

And to call the delegate something like this:

ImageRetrievedDelegate.ExecuteIfBound(ThumbnailTexture);

And it looks like this in the blueprint:

86888-capture.png

I read through this and ended up using interfaces to implement a callback mechanism similar to passing a delegate.
You can view my workaround here: Building New Worlds - Nose to the Grindstone

I follow your code, but vs report an error said, Unrecognized type ‘FOnlineUserImageRetrievedDelegate’ - type must be a UCLASS, USTRUCT or UENUM.
When I remove the decoration(‘UFUNCTION(BlueprintCallable, Category = OnlineUser)’) above the function RetrieveThumbnail, the project can compile and no error.
I wonder how can you compile those code withour error

I have the same problem… any solution?

I messed up with the macros.
Solution:
DECLARE_DYNAMIC_DELEGATE for the function parameter.
DECLARE_DYNAMIC_MULTICAST_DELEGATE for the class variable.

Handle isn’t needed (isn’t even there iirc), same event input param can be used to unsubscribe.

Kinda cool and everything seems to work, if I have the time I am going to make a tutorial on this.

tell me about it, it’s like nobody uses unreal or at least this forum, but it’s the same at other forums, barely an answer from time to time

why are they called event dispatcher since one sets them up to receive events as far as I nderstand
[edit:jeee this forum sucks to reply to the right person]

You can add delegate parameter to function by dragging event pin from event dispatcher and dropping it on the function.