0
\$\begingroup\$

Let's say I have a class UFoo which has a dynamic delegate myDelegate with no parameters. I cannot modify the contents of UFoo in any way.

DECLARE_DYNAMIC_MULTICAST_DELEGATE(FMyDelegate);
UCLASS()
class UFoo : public UObject
{
 GENERATED_BODY()
public:
 UPROPERTY()
 FMyDelegate myDelegate;
 // rest of class
}

I also have a class UBar which contains three UFoo-type objects foo1, foo2 and foo3. UBar also has a function DoStuff(int32 num) which takes a single integer as a parameter.

UCLASS()
class UBar : public UObject
{
 GENERATED_BODY()
public:
 UFUNCTION()
 void DoStuff(int32 num);
 UPROPERTY()
 TObjectPtr<UFoo> foo1;
 UPROPERTY()
 TObjectPtr<UFoo> foo2;
 UPROPERTY()
 TObjectPtr<UFoo> foo3;
 // rest of class
}

I would like to bind DoStuff to the myDelegate delegate of foo1, foo2 and foo3. But because the list of parameters is different between the delegate and DoStuff, this obviously cannot be done as is.

That being said, I know that in the case of foo1's delegate, the integer value passed to DoStuff should always be 1. Likewise, it should be 2 for foo2, and 3 for foo3. In that case, does this become possible? Is there a way to specify a default parameter value of sorts for DoStuff that is different per UFoo instance, allowing me to bind DoStuff to their delegates despite the differing parameter lists?

asked yesterday
\$\endgroup\$
5
  • \$\begingroup\$ I'm not experienced with Unreal specifically, but couldn't you declare "DoStuff1()", "DoStuff2()", etc, and bind those instead? \$\endgroup\$ Commented 21 hours ago
  • \$\begingroup\$ I'm not familiar with Unreal/C++'s version, but in C# we'd do it with partial application / closure, usually as a lambda: foo1.myDelegate = () => ubar.DoStuff(1); — here we're defining a one-off function that takes no arguments and always just calls DoStuff with a constant argument, then we assign that one-off zero-argument function as the delegate instead of DoStuff directly. We'd define a separate closure for each foo. Would a similar strategy apply for Unreal/C++? \$\endgroup\$ Commented 20 hours ago
  • \$\begingroup\$ @LukeB. In this particular instance, declaring multiple functions would work, but I'm looking for a more general solution for any number (or a list) of UFoos. \$\endgroup\$ Commented 20 hours ago
  • \$\begingroup\$ @DMGregory Unfortunately, as far as I'm aware, lambdas can't be used directly with Unreal's dynamic delegates because of the way their reflection system works (any function bound to a dynamic delegate needs to be what they call a UFUNCTION). I could maybe write some sort of wrapper object to contain the lambda in the form of a TFunction, and then have it implement a UFUNCTION that just executes this lambda? I'll look more into this option. \$\endgroup\$ Commented 20 hours ago
  • \$\begingroup\$ I found a thread discussing something similar here, but it sounds like the conclusion was "do it a different way", since dynamic delegates don't support supplying a payload at binding time the way other delegates do. \$\endgroup\$ Commented 19 hours ago

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.