How can I make a game that the system will save the game before the game close?

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

How can I make a game that the players can save thier game level or score or point (I don’t know how to explain) , for example : Player Jeremy is level 12 in the first day he play the game , the second day he logged in , his level is also 12 (The system will save the game before the game close.)

:bust_in_silhouette: Reply From: Lola

Hello,
you can use notifications for this.
Notifications are messages that gets propagated through objects on certain events.
There is a notification fired when the window manager provides a quit request (when yuo hit the X button): NOTIFICATION_WM_QUIT_REQUEST.

The following code can be used to execute something before the app gets closed by the user:

extends Node

func _notification(what: int) -> void:
    if what == NOTIFICATION_WM_QUIT_REQUEST:
        print("on quit")

but note that a built-in way of quiting the game (like a “quit” button which would trigger a SceneTree.quit() call) will not fire this notification so you would have to save the progress in this case too.

1 Like