I am creating a user widget which generates random number and I followed this video from YouTube:
https://www.youtube.com/watch?v=bWoew0fa_xA&t=731s
but the problem is that my button is not responding when I am trying to click it do not generates a random
number but when I call function OnClick with the blueprint it works so How to get it working with the c++
this is my CustomWidget.h file:
#pragma once
#include "CoreMinimal.h"
#include "Blueprint/UserWidget.h"
#include <Runtime/UMG/Public/Components/TextBlock.h>
#include <Runtime/UMG/Public/Components/Button.h>
#include "CustomWidget.generated.h"
/**
*
*/
UCLASS()
class CHAPTER5_API UCustomWidget : public UUserWidget
{
GENERATED_BODY()
protected:
UFUNCTION(BlueprintCallable)
void GenerateRandom();
UPROPERTY(meta = (BindWidget))
UTextBlock* RandomNumberLabel;
UPROPERTY(meta = (BindWidget))
UButton* GenerateButton;
void NativeConstruct() override;
};
here is the cpp file:
#include "CustomWidget.h"
void UCustomWidget::NativeConstruct()
{
Super::NativeConstruct();
GenerateRandom();
GenerateButton->OnClicked.AddUniqueDynamic(this, &UCustomWidget::GenerateRandom);
}
void UCustomWidget::GenerateRandom()
{
int32 RandomNumber = FMath::FRandRange(0, 100);
RandomNumberLabel->SetText(FText::AsNumber(RandomNumber));
}
then i blueprinted this class, and created UI components likw button, textbox etc…
then in the level blueprint I created widget instance as shown in the tutorial but the button still didn’t worked.