Possible to relaunch the current game from Lua?

My game has several save slots, and when returning to the initial screen to pick a different slot and start fresh, I COULD do lots of manual cleanup... but what I really want is just to reboot the game again as if I had exited it.

(NOT delete all the saved data, just launch fresh.)

Can this be done (in Lua)?

1 Like

Take a look at

https://sdk.play.date/inside-playdate/#_pdz_files

1 Like

To my knowledge, there is no straightforward function to relaunch a game.

You can call playdate.file.run("main") to re-run the main script but the global variables will not be reset. If you initialised properly your variable in your scripts that should be fine but you miss an initialisation that might create some unexpected bugs.

In theory you can give a new global variable (the second parameter) but you need to copy all the essential global variables (like playdate, print, etc.) however I do not know if that will release properly the memory used.

1 Like

Thanks all!

I put in a feature request for a simple reboot (with choice of same game vs. just exiting to launcher). Occasionally useful.

1 Like

Yeah, I think this would be really useful! When we had the old allocator that suffered from memory fragmentation I was going to add something like this so you could restart from a clean slate but wound up fixing the allocator again. Just recently while testing Lua performance I noticed that its random hash seed has a noticeable impact, so I went ahead and added a playdate.restart() function so that I can do multiple passes of a test with a new seed each time without having to quit and restart the test manually. It works pretty well, only problem I've hit so far is it crashes when you open the menu--must be a global pointer referencing the old lua state that needs to get cleared out. The way I have it working now is you call it with an optional string:

    playdate.restart(message)

and then message is available on the next round at playdate.argv[2]. So, for example, your main.lua could just be (totally untested code here :warning:)

    playdate.file.run(playdate.argv[2] or "menu")

then menu.lua would have your top-level menu and when you select an item you'd call, say, playdate.restart("level1") to run level1.lua in a freshly-initialized environment. If you need to move any additional state between modules you could use the datastore functions. (There's no hard limit to how big message can be, but I wouldn't push it; if it's too big the strdup() will fail and you'll restart with an empty argv[2].)

Keep an eye out for this in 1.14!

4 Likes

Very cool! Thanks Dave.

Is this random hash user controllable? Or is it baked into Lua? What's it used for?

(heads off to Google)

It's baked into Lua. Its purpose, as far as I can tell, is so that you don't wind up with persistent hash collisions that might degrade performance in unexpected ways. Instead you get unexpected but varying performance. :stuck_out_tongue_winking_eye:

1 Like

That sounds great! Thanks!