UMG Button OnClicked Event C++

Hello, I have problems when playing the “OnClick” event. I am using a “Blueprint Widget” and apart create a class “My User Widget”. Inside the “Blueprint Widget” in “Class Settings” I select the parent class, which in this case is my “My User Widget”. inside it I create a macro with the UPROPERTY value (meta = (BindWidget)) and class UButton * OkayButton; to select my button that believes in my “Blueprint Widget” which both have the same name “OkayButton” to establish a communication between the Widget and the blueprint. Now I want to play the event “OnClick” but I do not get the result. Here is my code:

Build.cs:

// Fill out your copyright notice in the Description page of Project Settings.

using UnrealBuildTool;

public class GameParty : ModuleRules
{
	public GameParty(ReadOnlyTargetRules Target) : base(Target)
	{
		PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
	
		PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "UMG" });

		PrivateDependencyModuleNames.AddRange(new string[] {  });

		// Uncomment if you are using Slate UI
		PrivateDependencyModuleNames.AddRange(new string[] { "Slate", "SlateCore" });
		
		// Uncomment if you are using online features
		// PrivateDependencyModuleNames.Add("OnlineSubsystem");

		// To include OnlineSubsystemSteam, add it to the plugins section in your uproject file with the Enabled attribute set to true
	}
}

MyUserWidget.h:

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "Blueprint/UserWidget.h"
#include "MyUserWidget.generated.h"

/**
 * 
 */
UCLASS()
class GAMEPARTY_API UMyUserWidget : public UUserWidget
{
	GENERATED_BODY()

public:

	virtual void Construct_Implementation();

	UPROPERTY(meta = (BindWidget))
		class UButton* OkayButton;

	void OnClick();

};

MyUserWidget.cpp:

// Fill out your copyright notice in the Description page of Project Settings.

#include "MyUserWidget.h"
#include "Runtime/UMG/Public/UMG.h"
#include "Runtime/UMG/Public/UMGStyle.h" 
#include "Runtime/UMG/Public/Slate/SObjectWidget.h" 
#include "Runtime/UMG/Public/IUMGModule.h" 
#include "Runtime/UMG/Public/Blueprint/UserWidget.h"
#include "UMG/Public/Components/Button.h"

void UMyUserWidget::Construct_Implementation()
{
	OkayButton->OnClicked.AddDynamic(this, &UMyUserWidget::OnClick);
}

void UMyUserWidget::OnClick()
{
	UE_LOG(LogTemp, Warning, TEXT("Click"));
}

MyHUD.h:

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/HUD.h"
#include "MyHUD.generated.h"

/**
 * 
 */
UCLASS()
class GAMEPARTY_API AMyHUD : public AHUD
{
	GENERATED_BODY()

public:

	AMyHUD();

	virtual void DrawHUD();

	virtual void BeginPlay() override;

private:
	
	UPROPERTY(EditAnywhere, Category = "Health")
		TSubclassOf<class UUserWidget> HUDWidgetClass;

	UPROPERTY(EditAnywhere, Category = "Health")
		class UUserWidget* CurrentWidget;
};

MyHUD.cpp:

// Fill out your copyright notice in the Description page of Project Settings.


#include "MyHUD.h"
#include "UObject/ConstructorHelpers.h"
#include "Blueprint/UserWidget.h"

AMyHUD::AMyHUD()
{
	static ConstructorHelpers::FClassFinder<UUserWidget> HealthBarObj(TEXT("/Game/UI/MainMenu"));
	HUDWidgetClass = HealthBarObj.Class;

}

void AMyHUD::DrawHUD()
{
	Super::DrawHUD();
}

void AMyHUD::BeginPlay()
{
	Super::BeginPlay();

	if (HUDWidgetClass != nullptr)
	{
		CurrentWidget = CreateWidget<UUserWidget>(GetWorld(), HUDWidgetClass);

		if (CurrentWidget)
		{
			CurrentWidget->AddToViewport();
		}
	}

}

GamePartyGameModeBase.h:

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/GameModeBase.h"
#include "GamePartyGameModeBase.generated.h"

/**
 * 
 */
UCLASS()
class GAMEPARTY_API AGamePartyGameModeBase : public AGameModeBase
{
	GENERATED_BODY()

public:

	AGamePartyGameModeBase();

	void BeginPlay() override;
};

GamePartyGameModeBase.cpp:

// Fill out your copyright notice in the Description page of Project Settings.


#include "GamePartyGameModeBase.h"
#include "Blueprint/UserWidget.h"
#include "UObject/ConstructorHelpers.h"
#include "MyHUD.h"
#include "MyPlayerController.h"


AGamePartyGameModeBase::AGamePartyGameModeBase()
{
	HUDClass = AMyHUD::StaticClass();

	DefaultPawnClass = AMyPlayerController::StaticClass();
}

void AGamePartyGameModeBase::BeginPlay()
{
	Super::BeginPlay();
}

MyPlayerController.h:

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/PlayerController.h"
#include "MyPlayerController.generated.h"

/**
 * 
 */
UCLASS()
class GAMEPARTY_API AMyPlayerController : public APlayerController
{
	GENERATED_BODY()

public:

	AMyPlayerController();
	
};

MyPlayerController.cpp:

// Fill out your copyright notice in the Description page of Project Settings.


#include "MyPlayerController.h"

AMyPlayerController::AMyPlayerController()
{
	bEnableClickEvents = true;
	bShowMouseCursor = true;
}

Result: the record is not shown when I click

1 Like

Have a look here:

https://forums.unrealengine.com/development-discussion/c-gameplay-programming/1598263-how-to-make-a-button-do-something-in-c

You must not modify any .cs file for this to work properly.

Use NativeConstruct instead of Construct_implementation:

in .h

virtual void NativeConstruct() override;

in. cpp

void UMyUserWidget::NativeConstruct()
{
Super::NativeConstruct()
OkayButton->OnClicked.AddDynamic(this, &UMyUserWidget::OnClick);
}

The Onclick method must be a UFUNCTION.

Only these two includes are needed in the .cpp:

#include “MyUserWidget.h”

#include “UMG/Public/Components/Button.h”

1 Like

thank you very much, that was all