How can I call the widget from C++?

I created user interface widget in Blueprint…
I am trying to call that widget… But it shows some error…
How can I call my widget?

Maybe provide more info, like the error you are getting and more about what you are trying to do.

As the comment suggested, it’s hard to answer the spirit of your question, but to answer exactly what it asks is as follows:

Create a C++ entry in your .h to allow the C++ blueprint to have a reference to the Widget. Something like this:

   UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Widget To Load")
    TSubclassOf<UUserWidget> WidgetReference;

This will provide your C++ program’s blueprint with a place to drag your widget into.

Create a pointer to store the Widget after it is created

UPROPERTY()
UUserWidget* WidgetPointer;

Create the widget in your C++ program

    if(WidgetReference) {
        WidgetPointer = CreateWidget<UUserWidget>(PtrToPlayerController, WidgetReference);
    }

Make the widget show up

    WidgetPointer->AddToViewport();

If you get compile errors, be sure you have included the Widget h files necessary.

#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"

Another source of potential errors is that there were changes required to the MyProjectName.Build.cs file to add “UMG” to the list of dependencies noted in PublicDependencyModuleNames entry, and “Slate” and “SlateCore” to the PrivateDependencyModuleNames, though oddly, it appears the update to 4.12 commented out the private dependency changes in my cs file and the widgets are still working.

I’ll delete this answer if your question was about something else and this was more basic than you were looking for.

i don’t know how to clear this error help me…
thanks for reply my question

It would appear that you are trying to use the AHUD class to implement UMG, which may not be what you want. The code that you put in that source file (menu.h/.cpp) instead should go into your “APlayerController” class player controller, or into your “ACharacter” class. If the UMG widget is to always be present when the Character is active, perhaps an appropriate place would be in BeginPlay of the ACharacter class.

I know it sounds confusing, but HUD and UMG are completely different things. I can’t explain why they both exist in UE4, but there are some answers related to that. Suffice it to say that if you are using UMG, you don’t need HUD. That is not to say you can’t use both, for example, I am using both, but my “AHUD” class merely displays my crosshairs. But they have nothing to do with one another other than they both have similar functionality.

there is a error shown “undefined” when ever i created a c++ class ,this happens only for the new projects

That may be something that deserves a separate question, but I am sure that you don’t need the AHUD class you created to call your widget into being. If the AHUD class is causing this problem, then it might help to back track and return that created class to it’s default state, or even reload prior to creating it. Then put your widget calling code into your Player Character class. That is just a suggestion concerning the widget and may not apply to what you are doing. I am sorry I can’t help with your error because I do not use Visual C++. I use a Mac and XCode and the oddities of creating a new class are completely different.

Once you do back out your likely unnecessary AHUD class and place your widget creation code into another class, do remember to add the #include statements into the cpp file of that class, and check that your dependencies in your build.cs include “UMG”.

I would be tempted to create a new question that specifically addresses an “undefined” error at the time of creating a new class if this is a general problem you are having. If the problem only has occurred in relation to UMG widgets, then my previous paragraph may have some value.

thanks for reply

You are welcome. If you hadn’t modified your project.Build.cs file to use UMG, I can provide what those entries look like. The statement should look like this, note “UMG” at the end of it.

PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "UMG"});

In 4.12 it may already be that way.

i already add this lines in project.Built.cs file
what ever we create a widget, HUD…, that always shows the same undefined error

Yes, all of the entries you have in menu.h and menu.cpp will be undefined. You don’t need that class to exist at all. It is an AHUD class and that is not relevant to your UMG widget. You don’t need a separate class at all for your widget. The code and definitions need to be integrated into an existing class, very likely your ACharacter class (since you don’t seem to have an explicitly defined PlayerController class). This is purely a guess, but it seems likely that the code would go in BeginPlay of your UICodeCharacter.cpp, and the header file definitions would go in UICodeCharacter.h. Then you would need to add those #include files to the UICodeCharacter.cpp. After getting that to compile, you would place a reference to your widget blueprint into your UICodeCharacter derived blueprint, and when your Character starts, your new widget will start.

You will have to clear the code out of menu.h and menu.cpp or it will continue to give you errors. You may not want to delete those files, but just leave them as they were created and they should be okay, but they can’t have the widget code in them.

I would also note that if you do this, the “CreateWidget” statement will need a change to obtain a pointer to PlayerController. This can be done by changing that statement to look like this:

WidgetPointer = CreateWidget<UUserWidget>(GetWorld()->GetFirstPlayerController(), WidgetReference);

I misspoke on one thing. You can have the code in your menu.h/cpp, but it would require alterations to be able to support that, specifically the include files. Since AHUD and UUserWidget have no relationship other than both are designed to overlay a 2D “HUD” or menu on your 3D world, I could not see the point in having the menu class in you case. But technically, you could put that code there and it would start if your menu class were listed as the Default HUD.

i created a two are more widget that all are in blue print , but now i try to that the same files and functions are in c++ and also my blueprint widgets all are perfectly working i created one widget the name of widget is “PlayMenu” i put three buttons in the widget and i created a another one widget “HostGameMenu” how to call the “playmenu” widget button into “hostgamemenu” widget thats the problem for me. i created some HUD and to call the widget in using some functions but no one should be properly working that always says the same undefined errors. please clearly say whats im doing now.

It sounds like you are asking how to call a blueprint menu from a blueprint menu. That is not difficult but it is completely different than what I have attempted to help you with. I would strongly suggest you create a new question and formulate the question very carefully. Please let me know if you would mind if I delete my answer. I don’t think it has anything to do with what you are attempting.