Download

UPROPERTY(EditAnywhere) bool MyBool Binding Problem

I cannot bind a boolean in my BP widget.



.h
    UPROPERTY(EditAnywhere, BlueprintReadWrite)
    bool MyBool;


When I bind to this variable in the BP Editor, I cannot change its value. It’s always set to true. What am I doing wrong?

I remember seeing someone else posting and answering this same question but I can’t find it.

Thank you for the help.

I ended up binding to an ECheckBoxState variable and calling a function in my class when the UI value changed:



.h
    UPROPERTY(EditAnywhere, BlueprintReadWrite)
    ECheckBoxState MyCheckboxState;
    bool MyBoolVar;

    UFUNCTION(BlueprintCallable)
    virtual void CheckBoxChanged(bool isChecked);


.cpp
void UMyWidget::CheckBoxChanged(bool isChecked)
{
    MyBoolVar = isChecked;
    if (MyBoolVar)
        MyCheckboxState = ECheckBoxState::Checked;
    else
        MyCheckboxState = ECheckBoxState::Unchecked;
}


  1. I wouldn’t expect to have to do this in order to bind variables. I should be able to bind to a boolean, float, enum, FText, FString, int32, uint32 and documentation should clearly state how to do it.

  2. I shouldn’t have to set the checkbox state in the above code but do so just to make sure that the UI and I are on the same page. If my variable is TRUE, I’m making sure that the checkbox state is also TRUE, meaning CHECKED (or as the case may be).

I could rewrite the function to a single line:



void UMyWidget::CheckBoxChanged(bool isChecked)
{
    MyBoolVar = isChecked;
}


Which is kind of silly. Why don’t I just bind to it in the first place?

I suspect that I’m doing something wrong.

The point is that Slate CheckBox can be in one of the THREE states: checked, unchecked and undetermined. The last one is used when you, for example, select multiple actors in the scene and they have different bHidden values to be displayed in property panel. Bool can only express two values: 0 and 1, therefore, its not sufficient for checkboxes.

I’m not sure if thats going to help you since I cant see what you’re trying to achieve, but maybe it would be a better idea to create a function that would return value of given checkbox as bool, like this:


bool GetMyBool() const
{
    return MyCheckbox->GetCheckState() == ECheckBoxState::Checked;
}

I was curious and tried myself, but I could not reproduce your problem: I don’t have any issue changing the value in my BP.

@szyszek, I can understand that particular use-case of the checkbox, but for me it is a UI control that the user interacts with indicating if they want a particular feature/option or not. It is either yes or no, true or false.

@Guronzor, I have a UCheckbox as part of a UUserWidget, and I expose the underlying data value using the UPROPERTY() mentioned above. Then in the BP Editor I set the checkbox Appearance::Bind to the UPROPERTY member:



.h
    UPROPERTY(EditAnywhere, BlueprintReadWrite)
    bool MyBoolVar;


I should mention the UI was composed using the BP Editor, dragging the Check Box control onto the panel, and selecting the binding as described.