Handling quit requests

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By profjle

I’m trying to make my game do something when a quit request has been received. So it runs a function and then the game closes after that is done. The problem is that it only works in either the editor or when the game is exported as a debug. If I were to export the game as a release, this wouldn’t work.

Anyone have any ideas why??

What does your code look like?

jgodfrey | 2020-11-21 15:48

func _notification(what):
    if what == MainLoop.NOTIFICATION_WM_QUIT_REQUEST:
		my_function()
        get_tree().quit() # default behavior

I’m using this which is from the docs just with an added function before the quit is called

profjle | 2020-11-21 15:50

What’s the target platform? I don’t know much about that notification, but digging around, it sounds like it might have a few quirks between various platforms.

Related, have you tried adding this to _ready()?

get_tree().set_auto_accept_quit(false)

jgodfrey | 2020-11-21 16:04

Yes, this is being done on the first scene that is loaded when the game starts. Exporting it for Windows currently.

profjle | 2020-11-21 16:16

:bust_in_silhouette: Reply From: Calinou

To run code when the project is exiting, you can define an _exit_tree() method in a singleton (which will be freed when the project quits):

func _exit_tree():
    print("Exiting...")
    # Don't quit here, as it will be done by Godot automatically.