use of .OnCheckStateChanged

Hello community :slight_smile:

I’m having troubles trying to call a function when the player check a check box.

Following the example of the StrategyGame that use .OnClicked(this, &SStrategyActionGrid::TriggerAction, i*GridRows+j) on a button I tried to do .OnCheckStateChanged(this, &SBookMarks::TextSelected) on my check box.

The function declaration goes like : FReply TextSelected() const;

And I’m getting :
Error 1 error C2664: ‘SCheckBox::FArguments::WidgetArgsType &SCheckBox::FArguments::OnCheckStateChanged(const FOnCheckStateChanged &)’ : cannot convert argument 2 from ‘FReply (__cdecl SBookMarks::* )(void) const’ to ‘void (__cdecl SBookMarks::* )(Param1Type)’ C:\Users\Adinimys\Documents\Unreal Projects\MyProject6\Source\MyProject6\BookMarks.cpp 28

Additionaly, I noticed that even though the StrategyExample almost use “this” in the “parameters” (I don’t know how is it called) of the widgets, the ShooterGame example do that really less often and I never used it in my own code that is working nonetheless (appart from the function call). Could someone explain my what is the point of “this” in that situation ? With slate documation currently returning a 404 error, the unique syntax of widgets is really tricky to understand :confused:

Thank you for answering my questions :slight_smile:

Hi,

From the error you pasted I assume your SBookMarks::TextSelected looks like this:

FReply SBookMarks::TextSelected()
{
/* your code here */
}

this function can be used to create a delegate that will be called during SButton click (OnClicked) however SCheckBox::OnCheckStateChanged uses different function that returns nothing and takes one parameter, so your TextSelected function should look like this:

void SBookMarks::TextSelected(ESlateCheckBoxState::Type InState)
    {
    /* your code here */
    }

About “this” keyword. I assume you know what it does (if not: Keyword this - C++ Forum).

So Slate widgets can have different type of parameters. Most common would be:

SLATE_ARGUMENT() - use it if you want to specify parameter that will passed during creation time of a widget (i.e. IsFocusable flag on SButton)

SLATE_ATTRIBUTE() - use it if you want to specify a dynamic parameter that will change during runtime (i.e. Text on STextBlock) 

SLATE_EVENT() - simple event/delegate that can be executed later on.

If you go to SCheckBox.h file you can see that OnCheckStateChanged is actually SLATE_EVENT parameter so you will need to pass a pointer to a function that should be executed when “check state is changed”.

Also, I would recommend going through examples stored in STestSuite.cpp and SWidgetGallery.cpp when it comes to basic stuff like this. Strategy/Shooter game examples are kinda more advanced and can be confusing if you’re not at least somewhat familiar with Slate.

Hope this helps in some way.

Yes thanks a lot, I’ve been strugling with trying to call a function from widget during the whole day :smiley:

I can’t find STestSuite.cpp and SWidgetGallery.cpp, are they part of the engine source code (my subscription is currently canceled so I can’t access it) ?

But about “this”… I understand what it does in regular c++ but not why we should use it as a parameter for the slate widgets :confused:

You can find those files under Engine\Source\Runtime\AppFramework\Private\Widgets\Testing

Taking SCheckBox::OnCheckStateChanged as an example: OnCheckStateChanged is a delegate, right? FOnCheckStateChanged (you can check it in SCheckBox.h). So what you have to do during creation of SCheckBox is pass a delegate that will be executed during CheckState change.
You can create delegates in a few ways, the most clear one would be FOnCheckStateChanged::CreateRaw(…). CreateRaw will create a delegate using passed pointer to an object and its member function, like this:

OnCheckStateChanged(FOnCheckStateChanged::CreateRaw(this, &SBookMarks::TextSelected))

Above code will work exactly the same like this one:

OnCheckStateChanged(this, &SBookMarks::TextSelected)

So to create (raw) delegate you need have a pointer to an object ( this ) and a member function signature ( &SBookMarks::TextSelected )

So long story short, passing using OnCheckStateChanged(this, &SBookMarks::TextSelected) is just an implicit, shorter use for creating delegates. This way you dont really have to know the exact type of delegate (FOnCheckStateChanged in this case) to create your widget.

Thank you :slight_smile: I never though about searching in these folders ^^