Creating Custom Event Delegates!

Hello Everyone!

I was just wondering if some people could help me out with creating and broadcasting custom events from c++!
I am having a really hard time understanding all the ins and out of delegates. Is it possible
for someone to maybe post some examples? I have been over the docs for them a bunch of times, but still having issues.

Thanks!
Parad0x_

Hey Paradox_!

One of the moderators, TheJamsh, posted a thread not too long ago asking how to use Delegates. There are some code examples in it that may help you out!

:slight_smile:

Hey a quik example of this:


// Header File over the class.
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FSomethingHappendSignature, bool, bWasItTrueOrFalse);

Note that you devide the data type (bool) and the param name with a β€œ,”.
Now add it as a new member to your class.


public:

	UPROPERTY(VisibleAnywhere, BlueprintAssignable, Category = "Your Delegate")
		FSomethingHappendSignature OnSomethingHappen;

When the thing you want to Broadcast happens you do the following.


OnSomethingHappen.Broadcast(bWasTrue);

To Bind to this event in a difrent class you can do the following to bind the event to a function in a other class.


// In your source file in a second class.
PtrToClassWithDelegate->OnSomethingHappen.AddDynamic(this, &ASecondClassName::FunctionToCallWhenOnSOmethingHappenFires);

Also note that the function you bind to needs to have the same parameters as the delegate, in this case one bool value.
And as long as the delegate is BlueprintAssignable, you can get it in Blueprints as well.
Don`t forget to check your pointers ;).

Hope this helps.

1 Like