Can't bind this delegate?

Once again I find myself at the delegate documentation :slight_smile:

Advanced Delegates in C++ · ben🌱ui

Delegates | Unreal Engine 4.27 Documentation

I wish to bind a function on a UObject to this delegate on FSlateApplication:

DECLARE_MULTICAST_DELEGATE_FiveParams(FOnFocusChanging, const FFocusEvent&, const FWeakWidgetPath&, const TSharedPtr<SWidget>&, const FWidgetPath&, const TSharedPtr<SWidget>&);

Since this is a non dynamic multicast delegate the following ways to bind come to mind:

Header:
#include "CoreMinimal.h"
#include "Templates/SharedPointer.h"
#include "Layout/WidgetPath.h"
#include "Input/Events.h"
#include "Widgets/SWidget.h"

CPP:
AddLambda
- Not tried.
AddRaw
- You cannot use raw method delegates with UObjects.
AddStatic
- Not static
AddSP
- AsShared is not a member of UXXXXXXXX
AddUFunction
- As a UFUNCTION: "Unable to find class, delegate, enum, or struct with name FWeakWidgetPath" (and the others)
AddUObject
- Compiles as long as I do not mark the function UFUNCTION.
AddWeakLambda
- Not tried.
AddThreadSafeSP
- Not tried.

None of the official documentation states that AddUObject to a method NOT marked UFUNCTION is a valid combination. The BenUI documentation states that it is not.

So this leaves me with the impression we have 10 ways to bind but none can do this?

bump

up up :slight_smile:

Hmm AddUFunction is not compatible in this case because some parameters of the delegate are not BP-compatible, so you cannot have an UFUNCTION with a signature that matches the delegate signature.

AddUObject should work with non-UFUNCTIONS for non-dynamic delegates. They are bound directly by member function pointer.
Only dynamic delegates require UFUNCTIONS, they are bound by name and triggered via reflection system. Methods like AddUObject, AddStatic or AddLambda do not exist for dynamic delegates.

AddLambda is a good alternative, although I’d suggest using AddWeakLambda whenever possible as it will automatically cancel the delegate if the bound object gets destroyed.
In this specific scenario, you can use the weak lambda to trim out the incompatible parameters and trigger an UFUNCTION on the object.

// UObject* Obj
Delegate.AddWeakLambda(Obj, [](const FFocusEvent& Event, /*other args*/) {
    Obj->SomeUFunction(Event);
});
1 Like

Ah, that is great! I don’t need blueprint functionality on this method and the delegate is not dynamic so removing the UFUNCTION and using AddUObject would work just fine? Or does the AddWeakLambda provide the benefits AddUObject does not?

They are pretty much the same from the looks of it.

1 Like