x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

[C++] Create Widget

How could I recreate this blueprint in C++ code. It'd be great! I can't seem to figure it out.

alt text

Product Version: Not Selected
Tags:
more ▼

asked Nov 09 '14 at 04:50 PM in C++ Programming

avatar image

Skydive
116 18 24 24

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

2 answers: sort voted first

Hi there, are you asking how to spawn a UMG widget in c++ code? I am doing a bit of this, so I can give our artists control over looks and layout, but have control over logic and interfacing with things like the online subsystem etc.

Anyway I will just give you the basics here, let me know if you need any more. I am storing mine currently on the player controller. You want a reference to the blueprint and then the instance you create.

 UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = UI)
 TSubclassOf<UUserWidget> WidgetTemplate;
 
 UPROPERTY()
 UUserWidget* WidgetInstance;

Then you create like so: if (WidgetTemplate) { if (!WidgetInstance) { WidgetInstance = CreateWidget(this, WidgetTemplate); }

     if (!WidgetInstance->GetIsVisible())
     {
         WidgetInstance->AddToViewport();
     }        
 }

That will get it showing up.

Now to do the Input and Cursor parts:

 FInputModeUIOnly Mode;
 Mode.SetWidgetToFocus(WidgetInstance->GetCachedWidget());
 SetInputMode(Mode);
 bShowMouseCursor = true

When you are done don't forget to reset the input mode back to game , hide the cursor and remove the widget

 WidgetInstance->RemoveFromViewport();
 WidgetInstance = nullptr;
 FInputModeGameOnly GameMode;
 SetInputMode(GameMode);
 FSlateApplication::Get().SetFocusToGameViewport();
 bShowMouseCursor = false;

I have only just started working with using UMG widgets in C++ (Before just slate) so I've found it a bit of a headache at times, especially with input and focus. So feel free to ask questions or let me know any little interesting issues/behaviours you run into.

Note: You can create your own class based on UUserWidget and then add custom behaviour at that level too, you just need to reparent the blueprint when in the UMG editor.

more ▼

answered Nov 10 '14 at 04:47 AM

avatar image

Ben.Driehuis
496 18 27 142

avatar image Omberone Nov 10 '14 at 12:31 PM

Thank you, this helped us.

avatar image Skydive Nov 10 '14 at 08:58 PM

What headers do I include to access UUserWidget?

avatar image Ben.Driehuis Nov 10 '14 at 09:53 PM

These are the main two

 #include "Runtime/UMG/Public/UMG.h"
 #include "Slate.h"

Also make sure UMG, Slate and SlateCore are in your public or private dependencies in your .Build.cs

avatar image trutty Dec 16 '14 at 10:38 AM

Hi, thanks Ben.Driehuis for the example. When I'm declaring the variables

 TSubclassOf<UUserWidget> WidgetTemplate;
 UUserWidget* WidgetInstance;

I get 'Error: variable "UUserWidget" is not a type name'

I am in my PlayerController and am including

 // including "Slate.h" is deprecated
 #include "SlateBasics.h"
 #include "Runtime/UMG/Public/UMG.h"
 // maybe I need this?
 #include "Runtime/UMG/Public/Blueprint/UserWidget.h"

I have added Slate to my build.cs like this:

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

Any idea why I get the error?

avatar image Ben.Driehuis Dec 19 '14 at 11:22 PM

Which version of unreal are you on?

avatar image AustinK Jan 28 '15 at 06:23 AM

I realize this is an old answer, but any idea why I'm getting an access violation in the CreateWidget(UWorld, UClass) tempalted function? Here is my code:

 UGameSettingsWidget *MyWidget = CreateWidget<UGameSettingsWidget>(GetWorld(), UGameSettingsWidget::StaticClass());

Thanks :)

EDIT: It happens on the line:

 NewWidget->Initialize();

avatar image IanBreeg Feb 24 '15 at 08:36 AM

Had this problem. The main thing you need to understand is that you don't create your custom widget class directly, you want to create the widget you've parented to that custom widget class. So instead of UGameSettingsWidget::StaticClass() you need to do something like this:

 YourCustomWidgetUIClass = LoadClass<YourCustomWidget>(..., TEXT(path_to_your_widget_in_content_browser), ...);

"path_to_your_widget_in_content_browser" can be something like "Game\UI\MyWidget.MyWidget_C".

You can find the complete syntax reference for LoadClass in the docs.

avatar image Omberone Nov 12 '14 at 09:17 AM

CreateWidget crashes for us when we join an existing session. Anyone else have this problem?

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

As far as i know, UMG Widget are based on "Slate" a programming language made up by some of the Epic Games guys. They invented UMG to get rid of the C++ horror with Slate, because it's not that easy to learn, at least not for me.

Rama made some Tutorials for Slate:

https://wiki.unrealengine.com/Slate,_Hello

But i would recommend to stay with UMG since they made it for us, so we don't have to use Slate.

more ▼

answered Nov 09 '14 at 09:08 PM

avatar image

eXi
6.8k 333 185 470

avatar image undercover Nov 10 '14 at 12:52 PM

Slate isn't a programing language, it's a GUI framework.

I think it's better to use UMG for the design part. The main reason for introducing UMG was probably to let the GUI designer focus on the layout and hiding the implementation. In this way the implementation and GUI are nicely separated.

avatar image Wallhalla Jan 13 '15 at 09:10 AM

I recommend UMG too, but i highly recommend to learn something about the Slate UI Framework too, because UMG = Visual Slate Editor (i just came up with this word myself ;) ). So in order to get your widgets work as they are intended to, you should also know how the Widgets f.e. SOverlay, Boxes, etc. work.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question