How to pass very simple parameters to Button function binders from “C++”. By simple parameters I really mean ANYTHING : the void* of the bound widget, an int32, or even a single byte.
Currently, between two evil macros overdoses, I create a single function for each binding. For instance, in the following example, one for each Clock speed buttons.
It only works for UI buttons that can be enumerated beforehand, and for UI with a dynamic count I have to create a dedicated class only to cache the parameter I want to be passed, which is syntax heavy to say the least.
void UW_Clock::NativeConstruct() {
Super::NativeConstruct();
CheckBoxStop->OnCheckStateChanged.AddUniqueDynamic(this, &UW_Clock::HandleOnClockStopped);
CheckBoxStart->OnCheckStateChanged.AddUniqueDynamic(this, &UW_Clock::HandleOnClockStarted);
CheckBoxSpeed1->OnCheckStateChanged.AddUniqueDynamic(this, &UW_Clock::HandleOnSpeedChanged_1);
CheckBoxSpeed2->OnCheckStateChanged.AddUniqueDynamic(this, &UW_Clock::HandleOnSpeedChanged_2);
CheckBoxSpeed3->OnCheckStateChanged.AddUniqueDynamic(this, &UW_Clock::HandleOnSpeedChanged_3);
CheckBoxSpeed4->OnCheckStateChanged.AddUniqueDynamic(this, &UW_Clock::HandleOnSpeedChanged_4);
CheckBoxSpeed5->OnCheckStateChanged.AddUniqueDynamic(this, &UW_Clock::HandleOnSpeedChanged_5);
}
void UW_Clock::NativeTick(const FGeometry& MyGeometry, float InDeltaTime) {
Super::NativeTick(MyGeometry, InDeltaTime);
static FDateTime& CurrentDateTime = USSTClock::CurrentDateTime;
FString date = FString::Printf(TEXT("%d/%d/%d"), CurrentDateTime.GetYear(), CurrentDateTime.GetMonth(), CurrentDateTime.GetDay());
float day_progress = float(CurrentDateTime.GetHour()) / 24.0f;
TextBlockDate->SetText(FText::FromString(date));
ProgressBarDay->SetPercent(day_progress);
static int& Speed = USSTClock::Speed;
CheckBoxStop->SetIsChecked(!USSTClock::IsRunning);
CheckBoxStart->SetIsChecked(USSTClock::IsRunning);
CheckBoxSpeed1->SetIsChecked((Speed == 1));
CheckBoxSpeed2->SetIsChecked((Speed == 2));
CheckBoxSpeed3->SetIsChecked((Speed == 4));
CheckBoxSpeed4->SetIsChecked((Speed == 8));
CheckBoxSpeed5->SetIsChecked((Speed == 16));
}
void UW_Clock::HandleOnClockStopped(bool bIsChecked) { USSTClock::IsRunning = !bIsChecked; }
void UW_Clock::HandleOnClockStarted(bool bIsChecked) { USSTClock::IsRunning = bIsChecked; }
void UW_Clock::HandleOnSpeedChanged_1(bool bIsChecked) { if (bIsChecked) { USSTClock::Speed = 1; } }
void UW_Clock::HandleOnSpeedChanged_2(bool bIsChecked) { if (bIsChecked) USSTClock::Speed = 2; }
void UW_Clock::HandleOnSpeedChanged_3(bool bIsChecked) { if (bIsChecked) USSTClock::Speed = 4; }
void UW_Clock::HandleOnSpeedChanged_4(bool bIsChecked) { if (bIsChecked) USSTClock::Speed = 8; }
void UW_Clock::HandleOnSpeedChanged_5(bool bIsChecked) { if (bIsChecked) USSTClock::Speed = 16; }