How can i get reference to textblock created in UMG from c++ code?

Hello!

I’m trying to change text in my textblocks created in Widget Blueprint form c++ code. I can get reference to my WIdget Class by ConstructorHelpers::FClassFinder, then i can create my widget and add it to a vievport. But how do i acces textblocks created in widget blueprint?

i can’t figuer it out.enter code here

.h
	/*Main menu BLueprint reference*/
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Widgets")
		TSubclassOf<class UUserWidget> wMainMenu;

	/*My Main Menu reference*/
	UUserWidget* MainMenu;

.cpp
AMyPlayerController::AMyPlayerController(const FObjectInitializer& ObjectInitializer)
	:Super(ObjectInitializer)
{
        //get my widget blueprint class
	static ConstructorHelpers::FClassFinder<UUserWidget> MainMenuCl(TEXT("/Game/Blueprints/GUI/BP_MainMenuWidget"));
	wMainMenu = MainMenuCl.Class;
}

void AMyPlayerController::OnMenuShow()
{

			bIsShowingMenu = true;
			// Create the widget and store it.
			MainMenu = CreateWidget<UUserWidget>(this, wMainMenu);
			
			

			// Extra check to  make sure the pointer holds the widget.
			if (MainMenu)
			{
				FInputModeGameAndUI Mode;
				Mode.SetWidgetToFocus(MainMenu->GetCachedWidget());
				SetInputMode(Mode);
                                
                                //ok and here is my problem 
                               // how can i acces my textblocks in MainMenu widget from                                here??? and then set the text of my text blocks.
                               UTextBlock* text1 = MainMenu-> ??
                              // or do i have to do it other way?
				
				//let add it to the view port

				MainMenu->AddToViewport();
			}

			//Show the Cursor.
			bShowMouseCursor = true;
}

have you resolve this??

Hey i have just figured it out!.. firts, you can get your UTextBlock or any other widget using the function GetWidgetFromName(FText name) from your UUserWidget object. There you pass the name, of course, of your TextBlock… but that its going to return an object of type UWidget, so you now have to cast it like this: Cast(w) where “w” is the UWidget we just referenced from GetWidgetFromName function. after that, you can call SetText(FText text) to change the TextBlock text… hope i helped !

1 Like