One frustration I’ve often had is that inside the embedded player, the python shell is maintained. This means that if you put something in bge.logic.GlobalDict, it will not be removed when the game quits, but only when the next game starts.
Couple that with the fact that theres no built in method to run a function when the game ends, and you can end up (particularly with multi-threaded applications) with it being very awkward to tidy up resources at the end of a game.
The solution? The RunOnPowerOff class:
class RunOnPowerOff(object):
'''Runs a function when the scenario ends. Put it in a game object
property'''
def __init__(self, function):
self.function = function
def __del__(self):
self.function()
How to use it:
Store it in a game object’s property. Make sure no variables ever reference it. Load it with a function:
Why it works?
Python’s builtin del works well, but if you have circular references then python doesn’t call del. So by having a minute class that you never refer to in code - and thus the only reference will be from an in-game object, you know that del will be called reliably.
Be aware that many BGE things are in a weird state when the end_game function is called. So don’t try to delete in-game objects, remove scenes or so on.
I wonder if this is the source of some trouble I’ve been having with the texture class. If I create, link or load a texture then the game will crash on exit if I don’t del it before exiting.
I wonder if this is the source of some trouble I’ve been having with the texture class. If I create, link or load a texture then the game will crash on exit if I don’t del it before exiting.
Create a sample file and submit a bug report…
But yeah, there is pain in dynamic loading and crashes.
Custom mainloop scripts have several limitations - particularly when you jump through 5-10 different blend files as part of your game’s usual operation…