Hi Epic,
We face a missbehavior with UCheckbox and this is highlighted with MVVM bindings. Where extra calls and a mismatch between callbacks values and visual infos is happening.
Best is to look at the repro steps I mentioned in this report to understand what the outcome is.
The issue isn’t about MVVM specifically but the UCheckbox FieldNotify implementation, which MVVM highlights best.
We are on 5.8.4 at Epic revision 58210709 (from the Release-5.8 branch).
If we look at the behavior,
MVVM calls UCheckBox::SetCheckedState with the initial value (Checked).
It’s interesting to highlight here the function:
void UCheckBox::SetCheckedState(ECheckBoxState InCheckedState)
{
bool bValueChanged = false;
if (CheckedState != InCheckedState)
{
bValueChanged = true;
CheckedState = InCheckedState;
BroadcastFieldValueChanged(FFieldNotificationClassDescriptor::CheckedState);
}
if ( MyCheckbox.IsValid() )
{
MyCheckbox->SetIsChecked(PROPERTY_BINDING(ECheckBoxState, CheckedState));
}
if (bValueChanged)
{
BroadcastEnumPostStateChange(UWidgetCheckedStateRegistration::GetBitfieldFromValue((uint8)CheckedState));
}
}
What is happening there is that:
First, “CheckedState = InCheckedState;” will assign the value to the UCheckBox.
Secondly, “BroadcastFieldValueChanged(FFieldNotificationClassDescriptor::CheckedState);” will notify a change on it. That MVVM (but more general any other FieldNotify-based system too) would use.
Finally, the Slate widget “MyCheckbox” see his value updated with “MyCheckbox->SetIsChecked(PROPERTY_BINDING(ECheckBoxState, CheckedState));”.
The trick is that at the second step I mentioned, FieldNotify is warned that the UCheckbox value changed. So MVVM uses that. And MVVM do then call UCheckBox::GetCheckedState() to get what the new value is.
But let’s look at UCheckBox::GetCheckedState():
ECheckBoxState UCheckBox::GetCheckedState() const
{
if ( MyCheckbox.IsValid() )
{
return MyCheckbox->GetCheckedState();
}
return CheckedState;
}
Yup… It should be pretty obvious by now. The Getter is prioritising the value of the Slate widget. And that value in the Setter is updated AFTER the FieldNotify event is triggered.
That’s also because the Setter call the Slate checkbox status to checked afterward that visually it’s still seen as Checked.
Potential (naive?) fix:
I moved the BroadcastFieldValueChanged in the SetCheckedState function as such:
void UCheckBox::SetCheckedState(ECheckBoxState InCheckedState)
{
bool bValueChanged = false;
if (CheckedState != InCheckedState)
{
bValueChanged = true;
CheckedState = InCheckedState;
}
if ( MyCheckbox.IsValid() )
{
MyCheckbox->SetIsChecked(PROPERTY_BINDING(ECheckBoxState, CheckedState));
}
if (bValueChanged)
{
BroadcastFieldValueChanged(FFieldNotificationClassDescriptor::CheckedState);
BroadcastEnumPostStateChange(UWidgetCheckedStateRegistration::GetBitfieldFromValue((uint8)CheckedState));
}
}
So far. It works as expected with this.
Can you please reflect this change in 5.8.5?
That being said, it still calls the setter nonetheless. Can you also implement a way to prevent the callback during initialisation? At least, locally I can simply do in my setter “if (SourceCheckBoxState != inValue)” to prevent some unexpected behaviours but that’s extra calls that are conceptually unnecessary during initialisation.
Thanks // Adrien