Creating a UI widget makes my game unplayably laggy

Hi All,

I am attempting to add a “pause menu” in my game and as soon as i “create the widget” from within the player controller using this logic the game basically stops functioning:

(this is called from the EventBeginPlay)

The menu itself has a couple of buttons (most are not handled) and the EventGraph for it looks like this:

I am overloading the use of the “esc” key to trigger the menu but the “pause / unpause” logic looks like this in my PlayerController EventGraph:

Even when the pause menu has not been activated (using esc) the lag in the game is EXTREME - to the point I initially thought inputs were completely broken - but it turns out they do still work - just with a lag of about 5 seconds.

Before you ask, yes I did follow a video for this: https://www.youtube.com/watch?v=qPpouAIFkec

Does anyone know what’s going on? I am not even sure how to debug this…

  1. because if i break the link in the begin play event (and don’t create the widget) the game performance goes back to normal

  2. surely creating it once and then toggling visibility on the pre-existing widget is more efficient than creating it every time it is needed?

  3. is it not better to just have one? what’s the mistake that’s so prevalent?

Right so did the above - it now creates the pause menu widget when you press esc - so before the widget is created it works fine - as soon as i hit esc (and the resume the game) - both of which work - the game then becomes extremely laggy

So it is definitely something to do with the creation of the widget

Here’s how it’s triggered:

Also this is not a memory issue:

task manager when the “ultra lagginess” is happening

this is task manager when no pause menu (i broke the link that creates the pause menu) - it’s actually using more ram with it not being created - less CPU granted - but not sure 9.4% is really pushing my system very hard:

it could be because you have it on triggered which is usually basically tick, put a print string there to test.

usually for this stuff you’d use started instead of triggered.

In my opinion the visibility vs created/destroy method just depends on how often the widget might be used. Even if you RemoveFromParent garbage collection isnt instant or free so visibility is often the better idea. In this particular case either is fine.

1 Like

The advice to “create the widget only when you need it” might be well meaning, but it’s not going to, by itself, create a meaningful difference in performance.

In older, more resource-constrained days, it was actually common to pre-create everything you would need, so that you knew that there was room for everything, and you wouldn’t suddenly run out of memory when you tried to show a dialog or spawn a bullet or whatever, and pre-created assets will cause fewer runtime pauses for asset loading, so there’s something to be said for that.

Adding the widget to the viewport, will have some cost, because UI events and rendering needs to dispatch to it, although a well implemented widget will not cause any particular performance slowdown when it’s hidden. Similarly, if the widget has tickers, and those tickers are super heavy, then that could also be something to look into.

The memory used by the widget is meaningless compared to the RAM of a normal computer, and any “cache” it uses would be flushed by a single frame of gameplay anyway.

So, why does the game slow down when your widget exists? It’s probably a bug in the widget, not a bug with how you show the widget.

Does the widget have properties that update on change? Do they form a loop of some sort? Are there deep loops or asset re-loads inside the widget implementation? If you turn on the pofiler, and check where the time is spent when the widget is created, what does the profiler say?

Also, what if you make a new empty widget, and create/show that, instead of your “pause menu” widget – does the game still slow down?

Those are the kinds of debugging steps you should take to hone in on what the problem is. All the talk of “widgets take memory and are wasteful” is totally beside the point for this problem. (Unless there’s a 128 GB 3D texture inside the widget or something, of course …)

1 Like

Explain to me how the profiler told you that the memory usage was the cause of this UI widget causing slowdowns?

My expectation is that some other mechanism, such as un-canceled tickers, repeated layout re-computation, or something like that would be the culprit.

1 Like

expect possibly and probably the ‘Triggered’ Pin

1 Like

This is just voodoo cargo culting. If you’re interested in actual engineering, you need to actually debug this problem in the profiler and find out what it is.

Memory comes from somewhere, and you can be specific about it. And even so, memory by itself does not cause performance drops, unless there’s some other resource that gets impacted by that memory, by visiting all of it every frame for example, or by force-loading something like a too big texture into a too-small GPU RAM area. These are fully understandable, deterministic causes of performance loss, and you can’t build a correct, robust game without understanding what the problem actually is.

Many times, the thing you think is causing the problem, is actually just triggering some other, underlying problem, and even though you think you “fixed” it by “not triggering” it, some time later (typically at the least convenient time possible,) the problem will be re-triggered again, by something totally different.

If your goal is not to understand the system, and if your goal is not to practice sound engineering, by all means, wave that dead chicken, observe that it currently doesn’t hurt, and rush on to something else. After all, you made it not hurt right now!

1 Like

Appreciate ppl helping here coz this is a real head-scratcher for me…

so in terms of the “widget itself” here’s how it works:

the creation of the widget is thus:

the widget itself looks like this:

and the BP is this:

there’s no loops - no weirdness as far as i can tel going on at all…other than black magic backend UE stuffs…

the widget itself works - and is even fairly responsive in-and-of-itself…but it makes the game CHUG

these are my sys specs btw:

Some hyptheses you can test:

  • Does the widget get created more than once? If you put a Debug Print String after the Add to Viewport, does it print only once?
  • Does the game get paused? you have a “set game not paused” when the menu is hidden, but I don’t see where you set it paused. Put a debug print in those cases.
  • If you Set Input Mode Game and UI instead of Set Input Mode UI Only, does it still chug?
  • If you create a brand new widget blueprint, with only a static text box in it, and instantiate/show that instead, does it still chug?
  • If it doesn’t, and you add one new button at a time, is there a point at which it starts to chug?
  • If you look at the logs for the renderer when it starts up, is it using your real dedicated GPU, or the low-power on-CPU GPU?

In general, “chugging” typically comes from:

  1. some absolutely massive resource being loaded/reloaded/rendered
  2. running some expensive code (like re-creating the widget) once per step instead of only once
  3. setting the game loop into “background” mode where it will limit frame rate on purpose
  4. too high frame rate (!) causing internal components to thermally throttle
1 Like

So I tried your suggestion of just adding a widget (nothing fancy) to see how it performs:

And…well…it works fine! So must be something in the pause menu itself!

I feel like a dumb dumb for not testing something simple first so thank you!

UI widgets are not intrinsically busted - just something in the pause menu logic!

That said - there’s not a lot going on in the pause menu itself:

Like that is literally all it does…

What is problematic here?

OK so quick update - the game itself doesn’t run slow - just the inputs become extremely unresponsive - things like space ships spawning and flying passed seem unaffected (smooth as per usual)…

Is this something to do with how inputs are being handled?

This is how I am setting up the inputs:

  • The widget is only being created once.
  • Yes the game gets paused - i changed where it is now (it now gets paused in the “visibility changed” event handler:

No behaviour change

  • changed the input mode to game and ui and it did not change anything
  • simple widget didn’t change anything perf wise
  • can’t see where to check which gpu it is using - that said i am running UE in max settings so if it’s not running on my 5090 i’d be surprised it runs so smoothly normally

So I went through it all and tried it with various combinations of stuff on and off…

And it is definitely the usage of these:

I can do everything except set the input mode…it seems…

This works perfectly fine - does the input stuff matter? Given the game’s paused?

UPDATE:

this works:

so i had the game input sets round the wrong way? interesting

1 Like

Glad you found the answer! Also glad it wasn’t some unfathomable deep voodoo magic :wink:

1 Like

me too lol

appreciate your help btw!