Download

Widget cannot be created after openlevel() GameInstance

I tried to call “OpenLogin()” function in Init() function but it didn’t work in no way.

I checked this, (UMG)Widget dont show from GameInstance - UE4 AnswerHub

“When “Event Init” is called no “PlayerController” exist yet in your level , to solve that add a “delay” node (ex 0.05sec) before you create your widget” 'Mhousse1247

As far as I understand, in Init function, we cannot create widgets because some objects are not installed yet.

This time I call the functions in OnStart(). if I remove this line “OpenLevel(LEVEL::LOGIN)”, it works.

Here is my game instance.


class X_API UGameManager : public UGameInstance, public FTickableGameObject
        {
        ...
        static UGameManager game_manager; // initialized in .cpp

        UPROPERTY() ULevelManager* level_manager;
        UPROPERTY() ULoginWidget* login_widget;
        ...
        Init()
        OnStart()
        }

        Init()
        {
        game_manager = this;
        Creating level_manager, login_widget
        }

        OnStart()
        {
        level_manager->OpenLogin()
        }


class ULevelManager : UObject
    {
    void OpenLogin()
     {
    OpenLevel(LEVEL::LOGIN)

    GameManager->LoginWidget->ConstructWidget() --  this doesn't work

    GameManager->GetWorld()->SpawnActor<AActor>(); -- this doesn't work either
    }

     void OpenLevel(uint16 level){....UGamePlayStatics::OpenLevel(..)....}
    }


 class   ULoginWidget : UObject
    {
    void ConstructWidget()
    {  
    CreateWidget()
     ...
    ....
    }

    void CreateWidget()
    {
     LoginWidgetClass = LoadObject<UClass>(nullptr, LOGINWIDGETCLASS_PATH);
    checkifnull(LoginWidgetClass)

    LoginWidget = ::CreateWidget<UUserWidget>(UGameManager::Instance()->GetWorld(), LoginWidgetClass);
    checkifnull(LoginWidget)

    LoginWidget->AddToViewport();

     }
     }



Thanks.