FPS drop and lags after changing window focus

Godot Version



Hello, I created project, where some objects enter window then leave on regular basis, but noticed that sometimes I get FPS drops and lags, I checked various forums for answers and tried many things that either increased the issue or decrease the issue(the decrease is usually small)…

In end got the issue in relatively stable state where it may not occure or occure 1-few times or happens often(RNG), but its best I could get it.

So I decided to put the issue into smaller parts to see where is the real issue.

So currently I have only scene with objects spawning and moving idefinetly + some autoloads.

The objects had 2 area2D childs with 5 connected signalts each.

After removing signal connections. The issue seems to drops quite a lot imo.

Now can go to 100+ objects without lags and fps drops tho it occasionally happens on start or at 120+ objects.

But as I was doing these tests I noticed new “Issue”, which I guess I should solve first.

The new issue is like this:

  1. I launch the scene F6.
  2. I watch if FPS drops or lags happens.
  3. When I feel like long time passed I go check how many objects are active.
  4. Usually I do this when 80-120+ objects are present.
  5. I go back and FPS drops from 60 to 45-54 and dance around these values.
  6. Never go up to 60 and it just lags in general all time.

If I do the window focus change at start it may cause lags and fps drops too but seems most of time it doesnt tho the lags and FPS drops seems to occure at 60-80+ objects on “itself”(actually its because I lost window focus during runtime atleast once)

I checked forums and stuff, there was something about “editor/editor settings/update continuosly” which resulted in more stable FPS but its still around 50 and wont rise up and still lags.

From basic look at how the issue is happening it seems like, when you start scene it have some priority that is lost and never gained back if window focus is lost…

Can you help me solve this issue or tell me whats wrong, thx :slight_smile:

No node tree, no code, no project files… Hard to guess, but if removing the signals helps maybe you are sending way too many signals. Signals are objects that are created and destroyed in memory. They are useful, but there are more performant ways to do things. If your intent is to have a large amount of objects enter and leave the screen, there are two better ways to approach this.

If it is a purely visual effect, a Particle Emitter is better suited than a collection of objects.

If it is not purely visual and you need complex interactions with other game objects, consider the Object Pooling Pattern. Make the 200 objects you want at the start and instead of destroying them and spawning a new one, just reuse them and replace their properties. The more objects you want, the more benefit you’ll get from this.

Pooling is helpful if we have a LOT of objects (source). Another way of optimizing A LOT of objects is talking to directly Servers like RenderingServer or PhysicsServer2D/3D. However, i think that 60-80 objects simply isnt that much for benefit to be noticeable

Is the game running in exclusive fullscreen/windowed/other mode?

Also turning on the Profiler to track performance might lower the performance itself, maybe that is the step that lowers your performance after switching back to the game window.

Thx for replies :slight_smile:

For the “newest” issue, where losing window focus and going back creates lower FPS and lags…I found out that if I use spawn method as call_defered(“spawn”) than call “spawn” fixes this issue, tho once after this fix I managed to get the issue again, but couldnt get it again afterwards…not sure what causes it etc. but the call_deferred method fix it…

After that I noticed that each time after 40 secs there is 3 FPS frame drop and “small lag”, afterwards it seems to run smootly again…

I tried at this issue remove autoloads until one which seemed to cause it at first, but after changing code and removing last autoload it was happening still

I mean I tried it on simple static scene where I was showing FPS only and FPS drop happened(for lag cant tell since nothing was happening there), then I tried the spawn scene without FPS show and all autoloads removed too, so only spawn scene there and lag did occure at 40 secs also…

Well checked it again and strangely after removing autoloads they still appear in the remote tab…but the globals dont exists when I remove autoloads from project/project settings/autoloads…how you remove autoloads from project if not from those settings with the given button there?

For the pooling method sounds interesting, after I solve current issue and get back to the area2D signal issue it may end up as solution if I wont find anything else there…

Okay…I created new project and added only 2 scenes.

One for FPS showing and one for spawning.

When I press F6 only one of those scenes there nothing else(no autoloads and stuff)
And it does happen for both scenes at 40 secs aprx. 60 FPS to 58 or “movement lag”…

Maybe its some debugger bug or something like that Idk…

Can someone try if he can see lag/FPS drop at 40 secs after start(F6) on scenes too?

For the removing autoloads and they are still there part…

The issue was that I was creating override.cfg for some settings purposes and removing autoloads from project wont affect the override.cfg and that file is taken in prior…after removing them from given cfg files autoloads were removed…

Now Im trying to turn the project to Godot 4.2.1, where the after 40 secs short FPS drop/lag seems not happening,…btw in exported project in 3.5.2, this issue happened too…

For the lose focus then go back lower FPS and lags the fix with deferred call seems not to work after all(on other day it came back and I didnt change a thing welp), tho in export, when this issue occured again it didnt happen so maybe its just rng debug bug…