Best ways to save on quit

Godot Version

Godot 4.2.1

Question

I am experimenting with all the ways you can quit the in-game world that will allow me to save the game.

Currently I have in world’s script:

  1. ‘_on_tree_exiting()’ function: (chose this function in particular since all the objects on the scenes aren’t deleted yet)
    This allows me to:
    a) Save the game in-game when exiting to main menu or through GDScript call to quit
    b) Save the game on Windows if you close the window by pressing X or Alt+F4
    c) Save the game on Windows if you close the task through Task Manager (tho only tried on debug of editor, wonder how it behaves when exported)
    d) Save the game on Android if you close the game by pressing Back (it’s set to quit in config)

  2. ‘func _notification(what)’ function that compares ‘what’ to ‘NOTIFICATION_APPLICATION_PAUSED’
    a) Save the game on Android if you go to task manager and swipe the game close (I think it catches it when you go into manager state)

As of right now I don’t know how well these behave on Linux, MacOS and iOS due to not having these platforms.
I also don’t know what to do on HTML5 if you close the tab/window, it doesn’t save. (only tried Firefox desktop on remote debug)

Any more ways to fill these holes?

1 Like

I can speak to Linux and say that if you capture the exit/quit command to launch a save that it works the same as on Windows.

I haven’t tried to capture an abrupt exit/alt+f4 style though. I generally save on checkpoints or when the player uses the exit/esc/back keys.

2 Likes

You can disable exiting the application when requested and exit it when you have saved the needed stuff.

extends Node


func _ready() -> void:
	get_tree().auto_accept_quit = false


func _notification(what: int) -> void:
	if what == NOTIFICATION_WM_CLOSE_REQUEST:
		# do save stuff here
		get_tree().quit()
3 Likes