Q
Q
Quark2021-03-16 21:53:38
unreal engine
Quark, 2021-03-16 21:53:38

How to get a reference to a UI element in c++?

Hello. I created a UI using the Blueprint class(Widget) and now I want to get a c++ reference to its elements (buttons, pictures, etc.). How can I do this?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Q
Quark, 2021-04-16
@Quark_Hell

Don't use NewObject to get a reference to the UUserWidget class. With this approach, BindWidget simply won't work and your widget references will be null.

A
Alexei No, 2021-03-17
@alien2023

For example, you can do this. I create a "Say Hello world" button in the blueprint widget.
60521fcbbf74d488495348.png
Further, for example, in the C ++ class GameMode I will add a new function that will display a message on the screen.
GameMode.h

UFUNCTION(BlueprintCallable, Category = "Custom")
    void SayHello();

GameMode.cpp
void AMyGameModeBase::SayHello()
{
  GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Orange, TEXT("Hello World"));
}

We compile the project and return to the widget.
We create an event for a new button, then we turn to the current GameMod and look for our function in the Custom tab.
60522168d8315077952944.png
We check.
605222a408598066229738.jpeg
Everything is working.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question