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?
foo1.myDelegate = () => ubar.DoStuff(1);— here we're defining a one-off function that takes no arguments and always just callsDoStuffwith a constant argument, then we assign that one-off zero-argument function as the delegate instead ofDoStuffdirectly. We'd define a separate closure for each foo. Would a similar strategy apply for Unreal/C++? \$\endgroup\$UFoos. \$\endgroup\$UFUNCTION). I could maybe write some sort of wrapper object to contain the lambda in the form of aTFunction, and then have it implement aUFUNCTIONthat just executes this lambda? I'll look more into this option. \$\endgroup\$