Download

Create / fire a custom event in a blueprint, bind a handler in C++?

I can’t really find anyone doing what I’m trying to do, and I’m curious if there’s a way. I have a blueprint that is firing a custom event. I’d like to *set up the binding in C++. *In other words, I’d like to, in my C++ class, define a handler to process the event when it is fired. Is that possible?

Everything I find when searching is about setting up the binding in the blueprint to point to a C++ class, which seems easy and isn’t what I need, or setting up the custom event in C++ and exposing it to a blueprint, which also isn’t what I need.

I’m not entirely sure what you mean, but C++ does not know anything about Blueprint-defined events or functions. You can (some would say, hackily) call blueprint functions through reflection by name - but it’s slow to do that and really bad design considering the alternatives.

If I’m understanding correctly, you’ve got a red ‘Custom Event’ node in Blueprint and you want C++ to do something when the event is called? In which case simply expose your C++ functionality via a Blueprint Callable function and link them to that.

I’ll try to be more clear: I have a custom event dispatcher defined in a blueprint – let’s call it MyCustomDispatcher. In the actor blueprint where it’s defined, I call it when a condition is met.

I would like to do the equivalent of “Bind Event to MyCustomDispatcher” within C++. I would like a C++ function in a different actor to be able to respond to that event.

I think that’s essentially what I’ll have to do. I was hoping to avoid it because I didn’t want every subclass of this C++ class to have to setup those bindings. They’re part of the functionality I was hoping this super class would define.

You can define a dispatcher in a static manner in C++ that all your objects call. Pass a pointer to the object with the dispatcher so the events can still be assotiated with an object. Bind the other class to it.

An Event Dispatcher (Delegate in C++) that you made and added in Blueprint cannot be bound in C++, you can only bind it in the Blueprint (but there’s nothing stopping you binding it to a C++ function).

C++ does not know anything about Blueprint-created types or variables etc. If you want to bind it in C++, it has to exist in C++ first. The way to do this IMO would be to create a BlueprintCallable (and possibly BlueprintAssignable) delegate in the parent C++ class.

Example:



.h
// No Params
DECLARE_DYNAMIC_MULTICAST_DELEGATE(FMyDelegate);

// One Param etc...
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FMyDelegate, UObject*, SomeObjectParam);

// BlueprintCallable = Can be broadcast from Blueprint
// BlueprintAssignable = Can be bound in Blueprint.
UPROPERTY(BlueprintCallable, BlueprintAssignable, Category = "Target")
FMyDelegate OnSomeEvent;

.cpp
// Bind Delegate
OnSomeEvent.AddDynamic(this, &UMyObject::MyFunction);

// Call Delegate
OnSomeEvent.Broadcast();


Yeah C++, thats what I said. Just did not call it Delegate. Having the two names for the same thing is confusing anyway, not to mention Events in C++