Create Function That Takes Events/functions as parameters.

Hi!

Im trying to create a function in a “User Interaction Component”.

The function we call: “PromptForConfirmationOrCancel”

Now I call the prompt function from another BP with 2 functions as parameter. Those 2 functions I want to bind to confirm and cancel events.

Are there special technical problems for this? Its pretty low lvl stuff to work with function pointers and many languages suport this type of delegation trickery. It would be a great pattern to use since I can unbind the functions after executing them and the whole dialog logic would be in the dialog function… all im doing in my class requiring it is calling for the prompt and assigning 2 functions…

Here a sketch of what I mean:

This would be so incredibly useful and basis to support some good design patterns. Function pointers/eventhandlers are considered a common basic thing in many languages and work like I described above.

Hope you can help me, maybe i did miss something, but it seems not doable in BP currently

Not too sure what you mean but you could try making what you’re suggesting in a event graph, select all and then right-click “Collapse to function”.

Function pointers only work in conjunction with C++.
You can however expose Delegates as Input Parameters in functions. You have to do this in C++.

Please, I’ve been hard-stucked on this for a Day. Is it not possible at all to declare a callback and assign it as a Blueprint parameter?? I desperately need this, and I’ve no problem coding in C++.

I’ve tried:


// Class.h
DECLARE_DYNAMIC_DELEGATE_OneParam(FOnEachEntity, AEntity*, Entity);
...

UFUNCTION(BlueprintCallable, Category = "EntitiesSet") void ForEach(FOnEachEntity Callback) {
    for (auto& Entity : Entities)
    {
    UE_LOG(LogTemp, Warning, TEXT("HERE"));
    //Callback.Execute(Entity); // How to invoke this?
    }
} // This approach crashes as I try to run the function ForEach


// Attempt2.h
UFUNCTION(BlueprintCallable, Category = "EntitiesSet") void ForEach(TFunctionRef<void(AEntity* Entity)> Callback) {
    for (auto& Entity : Entities)
    {
    UE_LOG(LogTemp, Warning, TEXT("HERE"));
    //Callback(Entity); // I can't compile, because Callback is not a UPROPERTY, UFUNCTION or UENUM
    }
}


This is just a trivial example of what I want to achieve, for demo purposes. I’m looking for a way to achieve patterns like map-reduce and use them in many other places. I just want functional programming to be a thing

Callback.ExecuteIfBound(Entity); // no crashes if nothing is subscribed.

I wanted to do something similar, and couldn’t figure it out. My blueprint only work-around was:

  • Make an interface to define the names of the callback functions. Using the OP example, the names of the functions would be “OnCancel” and “OnConfirm”
  • Pass an instance of the interface to the node. In the example, the promptUserForConfirmOrCancel node
  • Have the node call the interface functions at the appropriate times

Not a great solution, as you can’t look at the blueprints and see clearly when the callback functions get called, and it constrains the developer to which function names can be used as callbacks.

We should probably ask if Epic Games could make this a Blueprint feature. I mean allowing for passing function callbacks as parameters.

1 Like

I guess for now one of us should create the desired functionality via cpp and expose it as a custom blueprint node (or small collection thereof) and put it up on GitHub for the community to make use of. Maybe package it as an Engine-wide Plugin…

2 Likes