I just thought about sharing the way I’m detecting the game closing, and executing code before leaving the BGE.
Its useful if you have stuff to save before closing the game, for instance.
The idea is to watch a “canary” object, and trigger a list of callbacks that you would want to run on game quit.
(I’m not avoiding buffer overflows, but I’m montoring the state of a “canary object”, so it relates a bit !)
BGE_Canary.blend (469 KB)
The “quit.py” file:
from weakref import finalize from bge import logic import time # The function is just there to provide an entry point def init(controller): return ON_QUIT_CALLBACKS =  # This is the dispatcher function def quitEvent(): for callback in ON_QUIT_CALLBACKS: callback() # This is the function decorator def onQuit(callback): ''' Usage: >>> import quit >>> @quit.onQuit ... def myCallback(): ... # do stuff >>> ''' ON_QUIT_CALLBACKS.append(callback) return callback # The canary will be garbage collected class Canary(object): pass logic.canary = Canary() finalize(logic.canary, quitEvent)
Just use “quit.py” functions to register your own functions, like so:
import quit @quit.onQuit def myFunction(): doMyStuff() # ...
I’m using this method with BGEz, and I don’t know if many people knew about that little trick.
Have fun !