From 921d5e6e42fdb4bf16963f5e474594e0ed539457 Mon Sep 17 00:00:00 2001 From: Phil Hagelberg Date: Sun, 2 Jun 2024 01:05:03 -0700 Subject: [PATCH] Implement "resume reload" command. (#2609) * Implement "resume reload" command. When resuming, you can pass "reload" as the argument, which causes the cart's code to be reloaded before resuming the game. This allows for a much smoother iterative development style where you can pause the game, make changes, and see the effect of those changes without restarting the game. It does require some special handling in the cart to support saving state to a global and avoiding that state being reinitialized if it's already set; for instance: state = state or {x=25, y=100, shots={}} This implements it in a way that will be supported by any script that already supports eval. * Don't try to eval in fennel if the VM hasn't been initialized. --- src/api/fennel.c | 4 ++++ src/studio/screens/console.c | 17 +++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/api/fennel.c b/src/api/fennel.c index 433c18f89..44cc3a4f4 100644 --- a/src/api/fennel.c +++ b/src/api/fennel.c @@ -165,6 +165,10 @@ static void evalFennel(tic_mem* tic, const char* code) { tic_core* core = (tic_core*)tic; lua_State* fennel = core->currentVM; + /* if we proceed with an uninitialized VM it will segfault; however */ + /* it could be better just to initialize here when needed instead! */ + if (!fennel) return; + lua_settop(fennel, 0); if (luaL_loadbuffer(fennel, execute_fennel_src, strlen(execute_fennel_src), "execute_fennel") != LUA_OK) diff --git a/src/studio/screens/console.c b/src/studio/screens/console.c index 09efb37be..53f8ad04c 100644 --- a/src/studio/screens/console.c +++ b/src/studio/screens/console.c @@ -2667,6 +2667,23 @@ static void onRunCommand(Console* console) static void onResumeCommand(Console* console) { + if(console->desc->count) + { + const char* param = console->desc->params->key; + + if(strcmp(param, "reload") == 0) + { + const tic_script* script_config = tic_get_script(console->tic); + if (script_config->eval) + { + script_config->eval(console->tic, console->tic->cart.code.data); + } + else + { + printError(console, "eval not implemented for the script"); + } + } + } commandDone(console); resumeGame(console->studio);