Setup My BP Widget to WidgetComponent in AActor via C++

Hi. Sorry for my bad English.

I have problem.
When I try to load my BP widget via WidgetComponent in AMyActor, I have some strange behavior. Its work for started Editor. Compile successful, no errors, all works fine.
BUT.
If I close Editor and start it again - Editor will failed to load with error - cannot find BP Widget.

How it happens:

I have AActor Object with WidgetComponent;

  1. I created struct FWidgetRef form FTableRowBase;

  2. In struct I added TSubclassOf<UUserWidget> MenuWidget;

  3. I created BP MyWidget with some buttons;

  4. I created MyDataTable via Blueprint;

  5. I set link to **MyWidget **in MyDataTable.

  6. I get *FWidgetRef = MyDataTable->FindRow<FWidgetRef>( FName( “NewRow” ), FString( “Cannot find DataTable with Widget via DT->FindRow” ), true );

  7. WidgetComponent->SetWidgetClass(FWidgetRef->MenuWidget);

So. It is work. When Editor is open. But, If I close it, rhen try to open - Editor every time crash with error.

What do I wrong?
How must I to do?

Why do you need it to be in a datatable?
You can add TSubclassOf<UUserWidget> directly to the Component.

Also, if you’re using it in Constructor, your code will run while Editor is loading and your BP_Widget class didn’t load yet… then crash.

You don’t need to call SetWidgetClass() in the Constructor. You can do that in BeginPlay for example.

I have more then 16 blueprint classes, that inhert from C++ class.
So I want to create once WidgetComponent and set once MyWidget. And now its dont want to work =)
And, I don’t try yet set Widget in BeginPlay. And I think - its way too.

If I want to use TSubclassOf in C++ class then I need set MyWidget in all my 16 BPClasses widget manually.
Its not so cool =) If I will want to create another 16 BP classes? I must to remember, that I must set widgets in all new BP manually? =(((

Btw… If you really need that to run in Constructor, first make sure this isn’t a native class constructor running:



if ( HasAnyFlags( RF_Archetype | RF_DefaultObject ) )
    return;

// then load widget here...


That should stop your Editor from crashing at startup at least.

Thanks a lot and big ship with a small cart=))

I tried to move setWidgetClass() into BeginPlay(), and its works fine.
So I decided it normal way to my score =)

Thanks a lot again =)