Struct in C++ Delegate, pass by value or by const ref?

Hello Devs,

I’m creating a tool in which users can use Blueprint and C++ delegates. I’m wondering if there is a preferred or correct way to pass structs in Delegates.

By const ref:

DECLARE_DYNAMIC_DELEGATE_TwoParams(FOnGetUserDataResponse, const FOperationResult&, Result, const FUserData&, UserData);
DECLARE_DELEGATE_TwoParams(FOnGetUserDataResponseNative, const FOperationResult& Result, const FUserData& UserData);

By value:

DECLARE_DYNAMIC_DELEGATE_TwoParams(FOnGetUserDataResponse, FOperationResult, Result, FUserData, UserData);
DECLARE_DELEGATE_TwoParams(FOnGetUserDataResponseNative, FOperationResult Result, FUserData UserData);

I know technical difference between passing by value and by ref. I’m just wondering if any of these is better for Blueprints reflections system.
AI Chat scared me with this sentence:
**Only trivial USTRUCTs should be passed by const& in dynamic delegates**.
So looking for more experienced devs opinion :slight_smile:

1 Like

As I haven’t received any answer here I tried to read all possible resources and chat with multiple bots. I ended up passing by value in DYNAMIC delegates and by reference in non Dynamic/Native ones:

DECLARE_DYNAMIC_DELEGATE_TwoParams(FOnGetUserDataResponse, FOperationResult, Result, FUserData, UserData);
DECLARE_DELEGATE_TwoParams(FOnGetUserDataResponseNative, const FOperationResult& Result, const FUserData& UserData);

Why:

  • DYNAMIC delegates need to support Blueprints so passing by value is much safer and better support BPs.

  • Native delegates can be use in C++ only, so performance is the key here. We don’t do unnecessary copying.

Anyway I’m still open to any suggestions.

There is a problem:

Hmm I tested multiple times those delegates and I have never received such warning. Can you paste the Delegate declaration?

I created it directly in blueprint

Ahh ok, that might be the case. I declared all my Delegates in C++, so they are usable in C++ and Blueprints.
I haven’t tried BP event dispatcher for this case. Thanks for checking!