How to properly notify and deal with an accepted application close request?

Godot Version

v4.2.1.stable.arch_linux

Question

I am attempting to do clean-up before my application is closed by the user.

Currently I have a singleton autoload node for handling the application clean-up before closing. It sets auto_accept_quit to false in its _ready function. This allows me to catch NOTIFICATION_WM_CLOSE_REQUEST in its _notification override. I would like to propagate a custom notification from this node such as NOTIFICATION_WM_CLOSE_ACCEPTED. This way other nodes in the tree could perform their clean-up if they receive this custom notification. After the custom notification is propagated I can then quit the application.

My concern with this approach is that I am unsure of what range of values Godot provides for user defined notifications (if any).

My questions are:
Is it within Godot’s defined behavior to propagate custom notifications?

If yes, then does Godot have a reserved range I can use that guarantees the value I choose will not be overridden later?

If not, then would you please provide suggestions/examples on how I could run clean-up code before the application is confirmed to close?

Took a look through the source for Node and found this comment: godot/scene/main/node.h at b09f793f564a6c95dc76acc654b390e68441bd01 · godotengine/godot · GitHub.
It states we can add custom notifications as long as they don’t clash with other notifications.

It’s not super convenient having that there is no single place where all the engine defined notification codes, but there is more than enough range in an int, so a clash would be unlikely anyway.

Godot’s updates do have the ability to introduce new notifications, which could clash with your own if you are not careful. Example: NOTIFICATION_MOUSE_ENTER_SELF from Control.

For now I’ll just add my custom notifications using a very high number unlikely to be defined by Godot anytime soon.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.