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"

Cast UClass to UUserWidget

i wanna write a C++ function library, it contain a function called CreateAndAddWidget

.h file

 UFUNCTION(BlueprintCallable, Category = "LBP/User Interface")
         UClass* CreateAndAddWidget(APlayerController* OwningPlayer, TSubclassOf<class UUserWidget> UserWidgetClass, int ZOrder = 1);

.cpp file

 UClass* ULanslotBP::CreateAndAddWidget(APlayerController* OwningPlayer, TSubclassOf<class UUserWidget> UserWidgetClass, int ZOrder)
 {
     UClass* wid = CreateWidget<UClass>(OwningPlayer, UserWidgetClass->GetClass());
     
     UUserWidget* a = Cast<UUserWidget>(wid);
 }

i cannot cast uclass to uuserwidget, so i cannot call AddToViewport. Could you help me ?Or show me a new way?

Product Version: UE 4.16
Tags:
more ▼

asked Jul 11 '17 at 02:08 AM in C++ Programming

avatar image

Lanslot Chung
5 1 2

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

2 answers: sort voted first

Hey ! Your solution is totally wrong. You try to use template, but your function isnt templatized, uclass return is useless, return UUserWidget instead of UClass.. GetClass isnt needed..

Of course you can use template class function, but for this first read how templates works: https://www.tutorialspoint.com/cplusplus/cpp_templates.htm

Anyway i highly recommend to do this in way:

.h

 UUserWidget* CreateAndAddWidget(APlayerController* OwningPC, TSubclassOf<UUserWidget> WidgetClass;

.cpp

 UUserWidget* YourClass::CreateAndAddWidget(APlayerController* OwnerPC, TSubclassOf<UUserWidget> WidgetClass)
 {
    If(!WidgetClass)
        return nullptr;
 
    UUserWidget* wid = CreateWidget<UUserWidget>(OwnerPC, WidgetClass);
 
    If(wid)
         wid->AddToViewport(1);
 
   return wid;
 }


In this case you dont need cast and every widget is based on uuserwidget, so safe to give back uuserwidget :) Cheera

more ▼

answered Jul 14 '17 at 01:10 PM

avatar image

AmphDev
556 12 7 31

avatar image Lanslot Chung Jul 17 '17 at 09:31 AM

thanks for your answer; but i got some errors; alt text

alt text alt text alt text

header.png (20.8 kB)
cpp.png (48.9 kB)
project_header.png (34.8 kB)
build.png (71.6 kB)
avatar image AmphDev Jul 17 '17 at 10:17 AM

Hey

Add UMG as public depedency ;) Include should Blueprint/UserWidget.h You need add Slate and SlateCore too for your build as public depedency

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

answered Jul 17 '17 at 10:20 AM

avatar image

AmphDev
556 12 7 31

(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