Announcement

Collapse
No announcement yet.

Crash on CreateWidget and ConstructWidget

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

    Crash on CreateWidget and ConstructWidget

    I have a class UAbilityWidget that extends UUserWidget. Then I have a class called UAbilitiesTree which also inherits from UUserWidget.

    So I want to create UAbilityWidgets and add them to a canvas procedurally on UAbilitiesTree's NativeConstruct. So I tried CreateWidget and widgetTree->ConstructWidget and both crash after being executed (take a look at the callstack below).

    Here's my code:
    Code:
        for (int i = 0; i < abilities.Num(); i++)
        {
            UAbilityWidget* widget = WidgetTree->ConstructWidget<UAbilityWidget>(UAbilityWidget::StaticClass(), FName::FName(*("AbilityWidget[" + FString::FromInt(i) + "]")));
            //Also tried CreateWidget<UAbilityWidget>(canvas, UAbilityWidget::StaticClass());
    
            abilityWidgets.Add(widget);
    
            UCanvasPanelSlot* slot = canvas->AddChildToCanvas(widget);
        }
    Any ideas? Thanks for any help!

     
    Spoiler
    Last edited by ItsaMeTuni; 12-13-2018, 10:15 PM.
    C++ is awesome

    #2
    Ok so apparently the first parameter in CreateWidget (OwningObject) has to be the world in which you want to create the widget. So
    Code:
    UAbilityWidget* widget = CreateWidget<UAbilityWidget>(GetWorld(), abilityWidgetClass);
    did the trick.
    C++ is awesome

    Comment


      #3
      Just as a general practice rule - you should really use the Player Controller that should own (or is displaying) that widget, rather than creating it using a world reference.

      CreateWidget<USomeWidget>(GetPlayerController(), WidgetClass)

      Comment

      Working...
      X