x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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?

Product Version: Not Selected
Tags:
more ▼

asked Nov 17 '14 at 01:14 PM in Blueprint Scripting

avatar image

ruohki
166 6 65 16

avatar image Asher S Einhorn Dec 26 '14 at 11:51 AM

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.

avatar image phil123456789 Dec 13 '17 at 06:47 PM

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

avatar image ruohki Dec 26 '14 at 12:46 PM

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

avatar image Asher S Einhorn Dec 26 '14 at 01:13 PM

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

avatar image phil123456789 Dec 13 '17 at 06:50 PM

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]

(comments are locked)
10|2000 characters needed characters left

5 answers: sort voted first

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.

more ▼

answered Nov 20 '15 at 07:12 PM

avatar image

manoelneto
358 8 11 25

avatar image Lweniel Apr 13 '16 at 11:06 AM

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

avatar image manoelneto Apr 14 '16 at 06:58 PM

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:

alt text

capture.png (63.4 kB)
avatar image alonecat06 Nov 20 '16 at 02:25 PM

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

avatar image Elathan Apr 19 '17 at 05:37 PM

I have the same problem... any solution?

avatar image Elathan Apr 19 '17 at 06:27 PM

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.

avatar image MisoShiru Jan 24 '19 at 07:42 AM

This answer just changed my life, thank you. Don't know why I never thought of this before!

(comments are locked)
10|2000 characters needed characters left

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

alt text alt text

delegate.png (47.0 kB)
more ▼

answered Oct 31 '18 at 08:03 AM

avatar image

PhoenixMarket
1 1 3

(comments are locked)
10|2000 characters needed characters left

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.

more ▼

answered Dec 26 '14 at 01:12 PM

avatar image

Asher S Einhorn
650 93 80 180

avatar image Acren Aug 18 '15 at 04:22 AM

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?

avatar image Griffork_Opaque Sep 23 '15 at 05:19 AM

I agree, this is what I need.

(comments are locked)
10|2000 characters needed characters left

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

more ▼

answered Dec 27 '14 at 05:39 PM

avatar image

RAVaught
699 43 28 68

avatar image Griffork_Opaque Sep 23 '15 at 05:17 AM

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

(comments are locked)
10|2000 characters needed characters left

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: http://bit.ly/2cj6YLu

more ▼

answered Sep 12 '16 at 05:56 PM

avatar image

jdannenbring
1 1 3

(comments are locked)
10|2000 characters needed characters left
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question