Announcement

Collapse
No announcement yet.

[Tutorial] Extend UUserWidget for UMG

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    [Tutorial] Extend UUserWidget for UMG

    So i just started with UMG and after a week of messing around with it.
    I wanted to extend the Widget Blueprint, i got some great help from Nick Darnell so thanks again.

    Disclaimer
    This was done in UE 4.4.2 as UMG is experimental and may change this solution may change/ "break" in future versions.

    UPDATED VERSION CAN BE FOUND HERE

    In order to derive my class from UUserWidget:: i did the following.
    * 1 Header Files:
    Added several headers to my project header. "(YourProjectName.h)".
    Code:
    #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"
    * 2 Adding Modules:
    And then i also made sure that the UMG and Slate Modules was included as well.
    In "(YourProjectName.Build.cs)":
    Code:
    PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "UMG", "Slate", "SlateCore"});
    * 3 Compile
    Compile and re-open the editor.
    And add a new class to the project derived from UUserWidget::

    If you where wondering how the header of the derived class looks like after created/ added from the editor.
    Code:
    #pragma once
        
    #include "Blueprint/UserWidget.h"
    #include "InventoryWidget.generated.h"
       
     /**
       * 
       */
    UCLASS()
    class MYGAME_API UInventoryWidget : public UUserWidget
    {
           GENERATED_UCLASS_BODY()
    };
    * 4 Using the new User Widget class.
    Create a new Widget Blueprint


    * 5 Set the Widget Parent
    Open up the new Widget you just created and go to the Graph of the Widget Blueprint.
    Press on the button on the top of the window.

    * 6 Go down to the details.
    Under the Category "Globals" you set the class you derived from UUserWidget.


    You are done, now you have a simple and fast way to extend the user widget.
    From everything from Data Storage to picking up "events" delegates and so on.

    Hope this was helpfull.
    WCode

    Wiki Entry: https://wiki.unrealengine.com/UMG,_H...MG_in_C%2B%2B.
    New Wiki Entry: Extend UserWidget for UMG Widgets
    Last edited by WCode; 03-30-2015, 02:40 PM. Reason: Updated Version

    #2
    Awesome tutorial!

    Comment


      #3
      Originally posted by NickDarnell View Post
      Awesome tutorial!
      Thank you Its, thanks to your help so you can take some credit as well.
      Cheers!

      Comment


        #4
        Thanks for sharing this WCode!

        I am sure it will help many people get started with C++ UMG !



        Rama

        PS: Have you considered making a Wiki of this?
        UE4 Marketplace: Melee Weapon Plugin & Compressed Binary Save System Plugin | Rama's C++ AI Jumping Videos | Vertex Snap Editor Plugin

        ♥ Rama

        Comment


          #5
          Originally posted by Rama View Post
          Thanks for sharing this WCode!

          I am sure it will help many people get started with C++ UMG !



          Rama

          PS: Have you considered making a Wiki of this?
          Hey Rama glad you liked it .
          I have been meaning to read up on the "How to" on the Wiki for adding new pages.
          I will try get it done this weekend.
          Cheers.

          Comment


            #6
            Originally posted by WCode View Post
            Hey Rama glad you liked it .
            I have been meaning to read up on the "How to" on the Wiki for adding new pages.
            I will try get it done this weekend.
            Cheers.
            I just added my own UMG tutorial, on the BP side of things funnily enough!

            you can go to my tutorial and click on it to edit it to see how I put it together!

            https://wiki.unrealengine.com/UMG,_C..._Dynamic_Array

            Most important thing is to add this tag at the bottom of your tutorial:

            [[Category:Code]]

            or

            [[Category:Tutorials]]
            UE4 Marketplace: Melee Weapon Plugin & Compressed Binary Save System Plugin | Rama's C++ AI Jumping Videos | Vertex Snap Editor Plugin

            ♥ Rama

            Comment


              #7
              I will thank you .

              Comment


                #8
                Originally posted by Rama View Post
                I just added my own UMG tutorial, on the BP side of things funnily enough!

                you can go to my tutorial and click on it to edit it to see how I put it together!

                https://wiki.unrealengine.com/UMG,_C..._Dynamic_Array

                Most important thing is to add this tag at the bottom of your tutorial:

                [[Category:Code]]

                or

                [[Category:Tutorials]]
                Hello again i have added this tutorial to the wiki.
                https://wiki.unrealengine.com/UMG,_H...MG_in_C%2B%2B.
                Last edited by WCode; 09-22-2014, 06:03 PM.

                Comment


                  #9
                  Originally posted by WCode View Post
                  Hello again i have added this tutorial to the wiki.
                  https://wiki.unrealengine.com/UMG,_H...MG_in_C%2B%2B.

                  Oooh very nice! This will be very helpful for people!

                  I've linked to your tutorial in my UMG related tutorials section!

                  https://wiki.unrealengine.com/UMG,_C...ated_Tutorials

                  Yay for WCode!



                  Rama
                  UE4 Marketplace: Melee Weapon Plugin & Compressed Binary Save System Plugin | Rama's C++ AI Jumping Videos | Vertex Snap Editor Plugin

                  ♥ Rama

                  Comment


                    #10
                    Hi,

                    I'm trying to extend UUserWidget, but can't cast the widget to my class.

                    I get
                    cannot convert from 'UUserWidget *' to 'UMyUserWidget *'
                    This is what I have:

                    Code:
                    UCLASS()
                    class UMyUserWidget : public UUserWidget
                    {
                    	GENERATED_UCLASS_BODY()
                    }
                    Error shows up here with:
                    Code:
                    BackButton = CreateWidget<UMyUserWidget>(PlayerController, BackButtonClass);
                    or
                    BackButton = Cast<UMyUserWidget>(CreateWidget<UUserWidget>(PlayerController, BackButtonClass));
                    BackButtonClass is of type TSubclassOf<UUserWidget>

                    Any idea how I can go around this?

                    Thanks.

                    Comment


                      #11
                      Any error messages ?

                      Comment


                        #12
                        Ok, for some unknown reason the cast is now working lol

                        What's still not working is getting the BackButtonClass from the widget specified on the defaults.

                        DGMenuHUD.h
                        Code:
                        UCLASS()
                        class ADGMenuHUD : public AHUD
                        {
                        	GENERATED_UCLASS_BODY()
                        
                        public:
                        	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = HUD)
                        	TSubclassOf<UUserWidget> BackButtonClass;	// <-- Also tried with TSubclassOf<UDGMenuUserWidget>
                        
                        private:
                        	UPROPERTY()
                        	class UDGMenuUserWidget*  BackButton;
                        }
                        DGMenuHUD.cpp
                        Code:
                        #include "DGMenuUserWidget.h"
                        #include "DGMenuHUD.h"
                        
                        ADGMenuHUD::ADGMenuHUD(const class FPostConstructInitializeProperties& PCIP)
                        	: Super(PCIP){}
                        
                        void ADGMenuHUD::BeginPlay()
                        {
                        	Super::BeginPlay();
                        
                        	UUserWidget* UserWidget = CreateWidget<UUserWidget>(this->PlayerController, BackButtonClass); // <-- BackButtonClass is NULL here
                        	
                        	// so UserWidget is also NULL
                        	
                        	BackButton = Cast<UDGMenuUserWidget>(UserWidget);
                        
                        }
                        Is this wrong:
                        Code:
                        UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = HUD)
                        TSubclassOf<UUserWidget> BackButtonClass;
                        Or is there another way to specify the widget/class through the editor?

                        Comment


                          #13
                          Ok.. never mind. I was setting up the blueprint, but using the C++ class in the game mode.. gawwh.. *facepalm*

                          Comment


                            #14
                            Thank you! This was very helpful!

                            Comment


                              #15
                              Hi,

                              I followed the described Steps and also checked the wiki, but this seems to not work anymore in 4.6.1
                              Is this already known? or (may be) it's just a Mac thing..

                              In Addition, also It did not help, but I suppose it is important to explicitly set the UCLASS Specifiers to get the wanted behavior also in the subclass:

                              UCLASS(Abstract, editinlinenew, BlueprintType, Blueprintable, meta=( Category="IngameUI" ))
                              class UIngameUIWidget : public UUserWidget, public IIngameUICanvasChild

                              The problem is: Also my code compiles fine, I cannot select my class to reparent a newly created UMG Widget.
                              Any advises on this?

                              thanks
                              ciao Matthias
                              Last edited by sureal; 01-12-2015, 01:46 PM.

                              Comment

                              Working...
                              X