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…
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
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
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.
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…