Hello,
i struggle at the moment with the umg c++ and the UE documentation is not realy helpful.
I want to create a dynamic button list and want to give any button a dynamic name and event. So i created a widget with a with a ListView and include the entry Widget WBP_Replay.
My WBP_Replay has:
SizeBox
Button called ReplayButton and is set as Variable
Text called ReplayText and is set as Variable
I created a C++ Class for the Replaywidget and changed the parent from WBP_Replay
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Blueprint/UserWidget.h"
#include "Components/TextBlock.h"
#include "Components/Button.h"
#include "ReplayWidget.generated.h"
/**
*
*/
UCLASS()
class ALVANIAN_API UReplayWidget : public UUserWidget
{
GENERATED_BODY()
public:
UPROPERTY(BlueprintReadWrite, meta = (BindWidget))
class UTextBlock* ReplayText;
UPROPERTY(BlueprintReadWrite, meta = (BindWidget))
class UButton* ReplayButton;
};
now, i want to fill the List with the Buttons, but i can add a Button but not change the Text or add a Event on this.
if(!Dialog->IsInViewport())
{
Dialog->AddToViewport();
}
UReplayWidget* NewReplay = CreateWidget<UReplayWidget>(GetWorld(),ReplayWidgetClass);
if(IsValid(NewReplay))
{
NewReplay->ReplayText->SetText(FText::FromString(TEXT("Make light red.")));
NewReplay->ReplayButton->OnClicked.AddDynamic(this, &ULightControllerComponent::RedLight);
Dialog->Replays->AddItem(NewReplay);
}
In my Dialog Widget, the Button has the default Text. when i print the Text from this button, than it has the Text i set
FString Text = *NewReplay->ReplayText->GetText().ToString();
D( (TEXT("Text: %s"), Text) ); // -> Make light red
i addet a OnClicked Event on the Button himself.
void UReplayWidget::NativeConstruct()
{
if(ReplayButton)
{
ReplayButton->OnClicked.AddDynamic(this, &UReplayWidget::OnClick);
}
}
void UReplayWidget::OnClick()
{
UE_LOG(LogTemp, Warning, TEXT("Button Text is: %s "),*ReplayText->GetText().ToString());
}
This event return the default value.
It’s like two different Buttons, the C++ Button is not the Button in the Widget. What did i Wrong and how can i solve this?
Thanks