The first version, the original one, fails because of this:
You have a GameMode. In this GameMode you have a HUD Class defined. In that HUD Class, you are spawning the Widget on BeginPlay.
In the Widget, you open the Level, probably with the same GameMode, aka the same HUD Class. So as soon as you switch the Level, it calls BeginPlay on the
HUD and opens it again.
The BeginPlay version of the LevelBlueprint is the same, as long as you open the same Level.
If you want to stop this, you have a few things you can do:
Have a different Level that uses a different GameMode
Open the existing level with the option “?game=GameModeShortcut”, which opens the Level and uses the defined GameMode
That means you need a different GameMode that does not use the HUD Class, similar to above. You can add GameModeShortcuts in the Map and Modes section of the Project Settings in 4.14.
Pre 4.14 needs to add these things in the DefaultGame.ini
- Use the GameInstance to create the Widget
The GameInstance only gets added ONCE when you start the Game. It only gets destroyed if you stop playing. Changing the level doesn’t affect the GameInstance (despite destroying current Widgets while the world gets deconstructed).
You can create a GameInstance similar to all other Blueprints and set what GameInstance should be used in the Maps and Nodes options of your ProjectSettings (at the bottom).
The “BeginPlay” function of the GameInstance is actually called “Init”, so search for that.
I hope that answers your question.