[4.25] Widgets referenced by Game Instance keep "existing" between levels oO

Hey all ! I’m still on 4.25 and just discovered an Unreal behavior I had no idea was a thing and that actually had unintended consequences in my main project I’ve been working on for two years.

In theory opening a new map should discontinue / destroy all the widgets created in the first map, right? At least that’s what I thought.

Except I just realized if the widgets’ references are saved as variables inside a Game Instance blueprint then it’s not the case ! Said widgets just stop being “Top Level” but they’re still there.

To illustrate this I created a simple project from scratch that you can download here.

  • Step 1 : on any key pressed, we create a new widget and store its reference in the Game Instance
  • Step 2 : on any key pressed, this time we order the widget to wait 3 seconds and Print String “Hello from the grave” and we load another map using “Load Level” node.
  • Step 3 : we wait 3 seconds and the Print String DOES trigger

Is this intended behavior ? I had no idea widgets could “persist” between levels, to the point a “Delay” is still working even in a new level. It really mind boggled me.

If the widget is not referenced by the game instance then it does NOT persist between levels (I suppose this has something to do with garbage disposal but I’m not on expert on that topic).
On the contrary if you “Add” the widgets to an array in Game Instance, then none of the widgets ever disappear and you’ll accumulate them every time you change level.

Conclusion : now I’m going to be extra careful about how I’m storing my widgets’ references.
Did y’all already knew this was a thing ?

This is what the game instance is for, persisting between level loads. And if you store anything in there, it will persist, even widgets :slight_smile:

Well I learnt something new I guess ^^

Btw to get rid of this problem, in each Level Blueprint on Begin Play I added the following :
image

The first function is just me going through all my widget variables in my Game Instance blueprint and purging them. Then we need to Collect Garbage and everything is clean afterwards. During a split second though in between triggering the Collect Garbage node and it actually cleaning the garbage the widgets keep existing so in theory they could still cause a few problems down the line but it should still be much much better.

1 Like