TinyCircuits / TinyCircuits-Tiny-Game-Engine

MicroPython game engine for TinyCircuits Thumby Color
GNU General Public License v3.0
10 stars 2 forks source link

Ensure the save files go to the /Saves directory and not to the game directory. #91

Closed TPReal closed 3 months ago

TPReal commented 3 months ago

The main.py sets the saves directory in engine_save before launching the game, and subsequent calls to the engine_save API use that directory. A function engine_save.saves_dir() is also provided to allow an app to implement its own saving schema (i.e. manage the files without the use of the API).

TinyCircuits commented 3 months ago

Sorry this has taken so long to get to. I'm not sure we need to expose engine_save._init_saves_dir to Python, do you think any games would really care to set a different folder? Maybe we could just hardcode that path? What do you think?

Thanks for the PR!

TPReal commented 3 months ago

Well, it's not for the games to call this, it should just be called from /main.py, as it's stated in the docs. The problem is that the C code is not able to tell what's the currently running game. I did some research, and apparently it's almost impossible and discouraged to get the current python stacktrace from within C code. So C code is not able to tell what's the correct directory, which should be the same as the game directory, just with the /Saves at the beginning.

So the easy and I think reasonable solution is to let the C side know what's the directory from within main, where it is very easily accessible, as it's the main that runs the game.

Alternative approach would be to make engine_save a python module, then it would be able to figure out the current stack trace when calling the save function, and from that calculate the directory where the save files should be. But I think it would not be elegant at all.

TinyCircuits commented 3 months ago

That makes sense, thanks for explaining. This looks good to me and I gave it a quick test. Also, I'll make sure to take a look at why bytearrays are not being loaded from saves (as you mentioned in Discord).

Thanks!