UCheckbox::CheckedState missbehavior with FieldNotifies (especially with MVVM)

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

Steps to Reproduce
Enable MVVM Plugin (aka: UMG Viewplugin).

Create a Widget with a UCheckbox within it.

Create a C++ class child of UMVVMViewModelBase which is implemented as such:

Header:

UCLASS()
class UCustomMVVM : public UMVVMViewModelBase
{
    GENERATED_BODY()
    
public:
    UPROPERTY(BlueprintReadWrite, FieldNotify, Setter, Getter, meta = (AllowPrivateAccess))
    ECheckBoxState SourceCheckboxState = ECheckBoxState::Checked;

protected:
    ECheckBoxState GetSourceCheckBoxState() const;
    void SetSourceCheckBoxState(ECheckBoxState value);
};

Note the default value of “SourceCheckBoxState” being Checked.

And the CPP:

ECheckBoxState UCustomMVVM::GetSourceCheckBoxState() const
{
    return SourceCheckBoxState;
}
void UCustomMVVM::SetSourceCheckBoxState(ECheckBoxState value)
{
    SourceCheckBoxState = value;
}

Go back to the designer.

Go to ViewModels tab and add a new ViewModel of type UCustomMVVM. In the Details panel of it, configure “Creation Type” to “Create Instance”.

Go to “View Bindings” tab, select the checkbox, and create an MVVM binding for it upon its CheckedState property. Map it as bidirectional (2 ways) upon the UCustomMVVM->SourceCheckBoxState property.

Go back to UCustomMVVM and put breakpoints in GetSourceCheckBoxState() and SetSourceCheckBoxState().

Finally, play the widget in game (PIE or standalone).

Behaviour is the following:

  • The breakpoint in the getter will trigger during widget initialisation. All good so far.
  • The breakpoint in the setter will then trigger, with a value set to ECheckBoxState::Unchecked.

Now the game is in the state where the MVVM value is set to ECheckBoxState::Unchecked.

Interestingly enough, the visuals of the checkbox still remains checked.

Hi,

Apologies for the delay, and thanks for reporting this. I fixed the checkbox issue and a related problem in the Widget Switcher at CL#59740481; I’ll see if I can push that to the next hotfix, though the timing is a bit tight so stay tuned. Note that we still don’t prevent the callback during initialization, as that would be a bigger architectural change that could potentially break some other content. Two-way bindings are built as two separate one-way bindings, and we run those whenever the VM assignment changes to make sure our initial state is properly synced. The proper approach here will be the comparison you mentioned, if you want to skip the unnecessary assignment.

Best,

Cody