Pausing the rendering engine when application loses focus

Godot Version

v4.3.stable.official [77dcf97d8]

Question

Is there a way to pause the rendering engine when the application loses focus? Doesn’t make much sense for/my project at least, to continue to update the screen when not visible.

I already pause/unpause the tree when losing or gaining focus but the rendering engine continues to draw frames and the frame rate increases when the window is hidden.

I use the following code to pause/unpause the game on focus changes and this works well, but stopping or slowing the engine as well would be a nice to have.

I haven’t tried it but maybe setting the max fps to 1 when not focused might be a work around.

Kindly

Ryn

func _notification(what: int) -> void:
	if what == NOTIFICATION_APPLICATION_FOCUS_IN:
		get_tree().paused = false
	if what == NOTIFICATION_APPLICATION_FOCUS_OUT:
		get_tree().paused = true
1 Like

Hi Again,

Actually setting the MAX_FPS to 1 when losing focus works well, though be nice if there was a way to turn off the rendering engine completely. CPU and GPU usage plummets this way as well.

Ryn

func _notification(what: int) -> void:
	if what == NOTIFICATION_APPLICATION_FOCUS_IN:
		Engine.max_fps = 0 #Zero means uncapped
		get_tree().paused = false
	if what == NOTIFICATION_APPLICATION_FOCUS_OUT:
		Engine.max_fps = 1
		get_tree().paused = true
2 Likes

Would low processor mode in the project settings help? Only redrawing the screen when necissary.

1 Like

I thought of that and certainly use it in other non game applications, but it comes with its own set of problems, notably the large number of application idle wake up’s the OS has to handle.

I might give it a second look, though dropping the frame rate to 1 drops CPU and Gpu loads even more than low processor mode.

Kindly

Ryn

1 Like

Hi,

Actually doing a combination of all three gives the best results with next to no idle wake up’s. CPU drops to 1.0% and GPU 0% when the application is not focused. As soon as the application is refocused all returns to normal and I get the expected frame rates and CPU/GPU loads.

I’m working on a space sim exploration game of sorts with a retro / unique look and feel.

If you’re not in the application you’re going to get smoked by an asteroid, so pausing the entire game when you’re in another application makes sense and is a niceity to have. I’ve been smote in other games in this fashion and I’d like it to not be the case in mine.

Below is my code to handle this if anyone finds it useful.

Kindly

Ryn

func _notification(what: int) -> void:
	if what == NOTIFICATION_APPLICATION_FOCUS_IN:
		Engine.max_fps = 0 # Zero means uncapped
		OS.low_processor_usage_mode = false
		get_tree().paused = false
	if what == NOTIFICATION_APPLICATION_FOCUS_OUT:
		Engine.max_fps = 1
		OS.low_processor_usage_mode = true
		get_tree().paused = true
1 Like