Components - Handling delegates with return values between c++ and blueprints

Hi. I’m not a core UE developer, but I have some answers and ideas how to play around this. At first as you wrote you using multicast delegate(s). Non-const references is impossible to use with multicast delegate just because it’s multicast - it can be invoked for many event listeners in a random order. If event listener changes a context it can broke something important for other listeners. But if you really need to change something inside master component inside events you can create a mock FStruct that will be acting as usual structure (and will be copied to the event context) but have a reference to the UObject you want to interact.

USTRUCT(BlueprintType)
struct TBRNRUNTIME_API FMockingContext
{
	GENERATED_BODY()

public:

	TWeakObjectPtr<UObject> ImpersonatingObject;
};

UCLASS()
class UMockingContextBlueprintLibrary : public UBlueprintFunctionLibrary
{
	GENERATED_BODY()

public:
	UFUNCTION(BlueprintPure, Category = "Utility|MockingContext")
	static UObject* Impersonate(const FMockingContext& In) { return In.ImpersonatingObject.Get(); };

	UFUNCTION(BlueprintCallable, Category = "Utility|MockingContext")
	static void SetObject(UPARAM(ref) FMockingContext& In, UObject* Obj) { In.ImpersonatingObject = Obj; };
};

P.S. Maybe it will also work with Struct Box, but I didn’t test it.

1 Like