How to use OnClicked for SButton

I created the standalone window plugin and now I am trying to add a button to the tab. I am confused on how to use the OnClicked function correctly. I want change the color to green if the button color is currently red and vice versa. the following code is:

TSharedRef FTaskManagerModule::OnSpawnPluginTab(const FSpawnTabArgs& SpawnTabArgs)
{
	return SNew(SDockTab)
		.TabRole(ETabRole::NomadTab)
		[
			// Put your tab content here!

			SNew(SBox)
			.HAlign(HAlign_Center)
			.VAlign(VAlign_Center)
			[
                 SNew(SButton).ButtonColorAndOpacity(FLinearColor(1.0f, 0.0f, 0.0f, 1.0f)).OnClicked(/*not sure what to do here*/)
			]

		];
}

I’m not knowledgeable enough to be certain, but I don’t think you can override the functions of a native class in realtime (perhaps with Lambdas, but I’m not familiar enough with using them to say for certain).

My 2 cents, would be to do the following:

  1. Create your own child button Button
    class.
  2. Create an ENum for your desired
    color selection.
  3. Add a property to your custom button
    class for the enum.
  4. Override the OnClicked event for
    your custom button.
  5. Within the OnClicked event code, use
    “switch” to determine the current
    color selection and change the color
    accordingly and update the current
    selection value.

Here’s a quick example:
.h

UENUM(BlueprintType)
enum EButtonColor
{
  Red,
  Green,
}

Within your Custom Button UCLASS of the .h file

UPROPERTY(BlueprintReadOnly, Category = "YourCategory")
  EButtonColor ButtonColor;

in your .cpp file within the overriden OnClicked Event…

switch (ButtonColor)
{
  case EButtonColor::Red:
  {
    // Additional code to change the button's color value to green
    ButtonColor = EButtonColor::Green;
  }
  break;
  case EButtonColor::Green:
  {
    // Additional code to change the button's color value to red
    ButtonColor = EButtonColor::Red;
  }
  break;
}

The above isn’t verbatim, but should get you rolling if applicable.