inside TEXT, I tried widget’s blueprint reference and original C++ reference… Original C++ reference works (which means not NULL), but it does not show anything in the screen… even i checked whether the program initiated adding the widget to viewport… which seems it did(?)… but still nothing. hmm… What could have I done wrong…
TSubclassOf is a variable that you’re supposed to set in a Blueprinted version of that class, usually. If you do this for example:
UPROPERTY(EditDefaultsOnly, Category = "My Settings")
TSubclassOf<class UMyWidget> WidgetClass;
Then create a Blueprint version of that class, you will see this as an exposed variable. You should really avoid using string references to classes and assets where possible.
Also, to avoid loading lots of classes when you don’t neccesarily need to, there’s also a TAssetSubclassOf<>, which doesn’t load the object in question until you call LoadSyncronous() on it.
UPROPERTY(EditDefaultsOnly, Category = "Game Widgets")
TAssetSubclassOf<USCGame_MissionItem> MissionItemWidget;
// Create a new mission widget, add to the viewport and the active widgets array
USCGame_MissionItem* NewItemWgt = CreateWidget<USCGame_MissionItem>(GetOwningPlayerController(), MissionItemWidget.LoadSynchronous());
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Player, HUD and UI")
TAssetSubclassOf < class UActionsPanelWidget > GoodsInventoryInBattleUIClass;
//The instance of the players Inventory UI Widget
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Player, HUD and UI")
class UActionsPanelWidget* GoodsInventoryInBattleWidget;
if (IsLocalPlayerController())
{
if (!GoodsInventoryInBattleWidget) // If the widget is not created and == NULL
{
GoodsInventoryInBattleWidget = CreateWidget<UActionsPanelWidget>(this, GoodsInventoryInBattleUIClass.LoadSynchronous());
if (!GoodsInventoryInBattleWidget)
return;
GoodsInventoryInBattleWidget->AddToViewport();// Add it to the viewport so the Construct() method in the UUserWidget:: is run.
GoodsInventoryInBattleWidget->SetVisibility(ESlateVisibility::Hidden);// Set it to hidden so its not open on spawn
}
}
and… tried GetOwningPlayerController(), but does not work in my controller class. I have to use “this” instead
still though… weird error message “CreateWidget can only be used on UUSerWidget children” with not showing mywidget… what did i mess up this time… hmm…
Yeah so that means you haven’t set the variable to anything.
You need to create a Blueprint version of your player controller and set that variable to something, i.e. a UMG widget you’ve created. At the moment it’s empty.
Yeah so that means you haven’t set the variable to anything.
You need to create a Blueprint version of your player controller and set that variable to something, i.e. a UMG widget you’ve created. At the moment it’s empty.
[/QUOTE]
Alright. Setting BP version of that my controller class in Game Mode solves everything! Thank you so much . It was nightmare for me in few days and you helped me
Just use the HUD class as the manager. It’s a good central place to access all your widgets from and really that’s its purpose now that Canvas isn’t really used anymore.
It’s also relatively easy to access the HUD instance from anywhere in game code, since you only need to get the player controller to access it.
a bit silly question… there is so many Widgets - A B C D E F… e.t.c. but sometimes they have same function or not. hmm…My question is… is it good idea to inherit from base UserWidget class and keep them in Array or TMap?