Package result crash on showing UMG to viewport from C++

Hi All,

I experienced something strange. All this time, I used C++ to create and show/hide widget.
I used the following code:

FStringAssetRef AssetRef("WidgetBlueprint'/Path/To/BPWidget'");
    UObject* SyncResult = StreamableManager.SynchronousLoad(AssetRef);
    UBlueprint* WidgetBP =  Cast<UBlueprint>(SyncResult);
    UUserWidget* UIWidget = CreateWidget<UUserWidget>(GetWorld(), WidgetBP->GeneratedClass);

When I played it in editor (both in selected viewport and standalone game), everything seems fine.
Then I packaged the game.
I tried the game and nothing wrong with the gameplay. However, when I click something to show UI, the game crashed. After I re-packaged with some modification, I found that it got crashed when calling the SynchronousLoad (line 2 in the code above).

How can I fix this error? I noticed there are, at least two safer way to create widget:

  1. From Blueprint and store the reference to C++ variable
  2. Using TSubclassof variable and then set it in the Defaults

Since I think the issue here is also error with the SynchronousLoad, I want to know if there is a way to make it safe for packaging.

Thank you in advance for all your help.

Regards,

Accessing the blueprint at runtime is not allowed. Blueprints are an editor only time concept. They’re effectively the source code, we compile them into UClasses for runtime and cooked games. The UClass is what you should be saving the reference to.

Also, if you’re using FStringAssetRef, there has to be something that loads that asset and uses it during cooking or the class wont be included in the cook.

Thanks for you reply, Nick. I got it now. So I will use the 1st alternative, then…, saving the UUserWidget reference.

Cheers

Hi Nick,

In addition to my question, can share what to do and not to do for Packaging our project (maybe links). Since my developer team are C++ programmer, so they really prefer to do some of development on C++. I noticed that there are some things that we can not do safely in C++, like my question here (Crash using SetWidgetToFocus in C++ - Programming & Scripting - Epic Developer Community Forums). So I can suggest my team to develop using Unreal Rules.