Hey Epic guys,
I have a the following request:
Would it be possible to add a Broadcast Function to FMulticastDelegateBase?
I’m not really good in templates, but wouldn’t it be enough to just add:
template<typename ...ParamTypes>
virtual void Broadcast(ParamTypes... Params) const
{
//...
}
I need this, because I want to make my code more generic.
I have these Delegate
DECLARE_MULTICAST_DELEGATE (FUIOnPressed);
DECLARE_MULTICAST_DELEGATE (FUIOnReleased);
DECLARE_MULTICAST_DELEGATE (FUIOnBeginHover);
DECLARE_MULTICAST_DELEGATE (FUIOnHover);
DECLARE_MULTICAST_DELEGATE (FUIOnEndHover);
DECLARE_MULTICAST_DELEGATE_OneParam (FUIOnValueChanged, float);
and this struct:
USTRUCT(BlueprintType)
struct FActionDelegates
{
GENERATED_BODY()
//Please keep right Order
FUIOnPressed OnPressed;
FUIOnReleased OnReleased;
FUIOnBeginHover OnBeginHover;
FUIOnHover OnHover;
FUIOnEndHover OnEndHover;
FUIOnValueChanged OnValueChanged;
//Broadcast 1
void Broadcast(EUIInputEvent InputEvent)
{
switch (InputEvent)
{
case EUIInputEvent::UI_Pressed:
OnPressed.Broadcast();
break;
case EUIInputEvent::UI_BeginHover:
OnBeginHover.Broadcast();
break;
case EUIInputEvent::UI_Hover:
OnHover.Broadcast();
break;
case EUIInputEvent::UI_EndHover:
OnEndHover.Broadcast();
break;
case EUIInputEvent::UI_Released:
OnReleased.Broadcast();
break;
default:
break;
}
}
//Broadcast 2
void Broadcast(EUIInputEvent InputEvent, float Value)
{
switch (InputEvent)
{
case EUIInputEvent::UI_ValueChanged:
OnValueChanged.Broadcast(Value);
}
}
void AddFunction(EUIInputEvent InputEvent, UObject* Object, FName InFunctionName)
{
switch (InputEvent)
{
case EUIInputEvent::UI_Pressed:
OnPressed.AddUFunction(Object, InFunctionName);
break;
case EUIInputEvent::UI_BeginHover:
OnBeginHover.AddUFunction(Object, InFunctionName);
break;
case EUIInputEvent::UI_Hover:
OnHover.AddUFunction(Object, InFunctionName);
break;
case EUIInputEvent::UI_EndHover:
OnEndHover.AddUFunction(Object, InFunctionName);
break;
case EUIInputEvent::UI_Released:
OnReleased.AddUFunction(Object, InFunctionName);
break;
case EUIInputEvent::UI_ValueChanged:
OnValueChanged.AddUFunction(Object, InFunctionName);
default:
break;
}
}
};
For every type of Delegate I need a new Broadcast Function which helps me Broadcasting this Delegate.
Fortunately most of my Delegates have the same Signature (No Params) but if I want to add for example a Delegate which has a String as a Parameter I have to add another Overloaded Broadcast Function.
I made it like this, because my first attempt didn’t work, because FMulticastDelegateBase doesn’t have a Broadcast Function.
I wanted to do it like this:
//In FActionDelegates
FMethodDelegateBase* GetDelegate(EUIInputEvent Event)
{
switch(Event):
return &OnPressed;
//…and so on
}
//Called like this
template<typename... ParamTypes>
void NotifyEvent(FName ElementName, EUIInputEvent InputEvent, ParamTypes... Params)
{
//ActionDelegates are stored in:
//TMap<FName, FActionDelegates> Delegates;
if (auto ElementDelegates = Delegates.Find(ElementName))
{
//ElementDelegates is of struct type FActionDelegates
ElementDelegates->GetDelegate(InputEvent)->Broadcast(Params...);
}
}
Sry for the wall of code
If it’s not possible to do so, it would be nice to get a good exaplanation to get more into templates
Thank you
Greetings