How to call a Widget function in C++

Hello guys.

I’d like to “translate” this Blueprint in C++ code:

68692-1.png

How to create a new Widget and then add it to Viewport in C++?

I already tried adding this code to the FirstPersonCharacter class:

68693-2.png

But it gives me errors of undefined references.

Note:

I already created a Widget in the editor:

I just need to know how to instanciate it and add it to the viewport IN C++

I believe your solution its in this video.

Ok I saw the video and I solved the main problem.
Now I have another one.

I have this widget with this function (ChangeText):

I’d like to call this function from C++ in the FirstPersonCharacter.cpp file.

How?

I have this two variables inside the GameMode.cpp

Sorry, I don’t know how to call blueprint function from C++.

Maybe someone else can help you with this.

I’d suggest you to search here or in the forums.
Taking a quick look, I found this

Calling Blueprint function is kind of complex. Easiest and nicer way is create C++ UUserWidget with your functions (implementable events?) and varables you want to do C++ and make a widget blueprint out of it (you can reparent existing one i think)

More complex way is to find UFunction* of you function with FindFunction and then execute it with ProcessEvent like this:

UFunction* ChangeText = ItemPickedHUD_Instance->FindFunction(FName("ChangeText"));

if(ChangeText) {
    FChangeTextParams ChangeTextParams;
    ChangeTextParams.NewText = "Something";
    ChangeTextParams.IncreasingValue = 1337;
    ItemPickedHUD_Instance->ProcessEvent(ChangeText,&ChangeTextParams);
}

As for params (FChangeTextParam) you need to create struct which has declered exact same parameters (they can have any name, types are more importent), you also need to declere them in exact same order as declered in blueprint, because you dealing here with void* (which means function will operate on raw memory data) and it will accept any pointer and struct in memory won’t have any infomation about parameters, it basing on order of data. If you get them wrong you either get crash or some wierd data.

Also FindFunction might be expensive operation so do it once on some init.

1 Like

Thanks for the answer, I’m in panic, i don’t know what to do.

Well I’d take the first way. This is my GameMode.h

And this is my GameMode::BeginPlay (ignore the errors and the LifeHUD)

Now, how do I add functions for the ItemPicked Widget?

I added ChangeText() function to it, but through Blueprints. How to do that in C++?

Which should the parent class be? I tried this:

But gives me errors…

I tried this: Inherit from class UserWidget (UMG) - UI - Unreal Engine Forums

But that didn’t work

First delete function you have in blueprint (copy nodes somewhere for backup)

You need to create UUserWidget class like UItemPickedWidget (or more common name if you plan to reuse ChangeText function in other widgets)

In it declere the function:

UFUNCTION(BlueprintImplementableEvent, Category="Something")
void ChangeText(FString NewText, uint32 IncresmentAmount);

Compile code

And then go to you widget blueprint, go to “File->Reparent Blueprint” and pick your C++ class. Now you can place C++ event on blueprint and it will be called when you call C++ function you declered ;] if you dont see it among events hover over function categoty bar and click Override your event will be there

Als ofr ace of use in GameMode insted of UUSerWidget* varable for widget change it to your class and on spawn cast it to your class so you don’t need to cast on each widget use

And then go to you widget blueprint, go to “File->Reparent Blueprint” and pick your C++ class. Now you can place C++ event on blueprint and it will be called when you call C++ function you declered ;] if you dont see it among events hover over function categoty bar and click Override your event will be there

You must Reparent your widget blueprint not your C++ class

I think you are very confuse using UMG and C++ code. Take a look at the programming tutorial series. There show how to call C++ functions.

I already watched that series, but she doesn’t do what I want to achieve =(

I should pay someone to assist me 24h

I already watched that series, but I didn’t find my problem.
I decided to choose the first option from @anonymous_user_f5a50610

I created a variable

UFunction* ChangeText

To assign this: ItemPickedHUD_Instance->FindFunction(FName("ChangeText"));

i need to get the current Game Mode because ItemPickedHUD_Reference is a variable of the GameMode.

I tried doing so: AMyProjectCPPGameMode* GameModeRef =(AMyProjectCPPGameMode*)()->GetAuthGameMode();

But then If i do this

ChangeText = GameModeRef->ItemPickedHUD_Reference->FindFunction(FName("ChangeText"));

I get this error

This maybe because inside the AMyProjectCPPGameMode.h I declare

class UUserWidget* ItemPickedHUD_Reference;

using a Forward Declaration.

So, how to solve the problem?
I tried including “Blueprint/UserWidget.h” or something like that, but it didn’t work (no such file or directory)

Just solved the problem about the "Blueprint/UserWidget.h"

But now, the engine crashes when I use the ()->GetAuthGameMode(); function =(

@anonymous_user_f5a50610

Either World is null for some reason (calling in wrong place) or cast is invalid (gamemode is actully diffrent), also check the logs there might be hint too

What? :stuck_out_tongue: i said reparent widget blueprint, but he seems to want to do other way

Also if you going this way, best way to implement this is to create a function which calls that function, so you can reuse it anywhere without doing all this UFunction* stuff

How many problems for a damned HUD?

Since my project is a C++ Template, can you tell me which is the best way toc reate a Widget currently?

I had to attach my Widget class to a GameMode. But somewhere I read that a GameMode is only server-side and won’t be available for every client.

Obviously my Widgets are individual: any player has its own Health, Armor and ItemPicked message when he picks a weapon from the ground.

So, tell me. What can I do?

I don’t know where to bang my head.

@anonymous_user_f5a50610

Maybe do it in your AHUD class, using it as medium for widgets won’t do harm UE4 will treat that class as HUD (UMG is something designed to do more then HUD… i mean Slate that powers it was made for editor UI) and good old HUD canvas might be useful too

in UE 4.13.2, I had the same issue, and sloved by 4 steps:

  1. Enable “Project Setting-Project-Packaging-Native Blueprint Assets”

  2. Add #Include to the tail of your “ProjectName”.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”

  3. Add AddRange to your “ProjectName”.Build.h
    PublicDependencyModuleNames.AddRange(new string[] { “Core”, “CoreUObject”, “Engine”, “InputCore”, “UMG” });
    PrivateDependencyModuleNames.AddRange(new string[] { “Slate”, “SlateCore” });

  4. use LoadClass instead of LoadObject

UIS_UIBase :my UI C++ base class name , change to yours.

UIS_UIBase* AIS_UIManager::LoadWidget(FString Name)
{
UClass * UIClass = LoadClass<UIS_UIBase>(NULL, (Path + Name + “.” + Name + “_C”));
if (UIClass)
{
UIS_UIBase
UI = CreateWidget<UIS_UIBase>(()->GetFirstPlayerController(), UIClass);
return UI;
}
UE_LOG(LogClass, Error, TEXT(“No such UIBlueprint! : %s%s”), *Path, *Name);
return nullptr;
}

Hi, how did you get rid of the compile error for not finding function CreateWidget?