commit 6f06d1bfd0297e7b1e966013197452c9119872a0
Author: Jon Bell <j.to.the.bell@gmail.com>
AuthorDate: Sat Jan 3 10:15:00 2026 -0700
Commit: Jon Bell <j.to.the.bell@gmail.com>
CommitDate: Sat Jan 3 10:15:00 2026 -0700
Update game loop and load to match zepto8 example better
---
source/p8GlobalLuaFunctions.h | 31 +++++++++++++++----------------
source/vm.cpp | 19 ++++++++++++++-----
2 files changed, 29 insertions(+), 21 deletions(-)
diff --git a/source/p8GlobalLuaFunctions.h b/source/p8GlobalLuaFunctions.h
index 25fcf93..8fd444d 100644
--- a/source/p8GlobalLuaFunctions.h
+++ b/source/p8GlobalLuaFunctions.h
@@ -22,6 +22,7 @@ __z8_load_code = load
--todo: see if we need the other z8 stuff
__z8_stopped = false
__z8_persist_delay = 0
+__z8_cart_running = false
function all(c)
if (c==nil or #c==0) return function() end
@@ -530,35 +531,30 @@ function __z8_reset_cartdata()
end
function __z8_run_cart(cart_code)
+ -- Glue code that manages the game loop after the cart's code runs.
local glue_code = [[--
if (_init) _init()
if _update or _update60 or _draw then
- local do_frame = true
while true do
- local should_draw = false
if _update60 then
_update_buttons()
_update60()
- should_draw = true
- elseif _update then
- if (do_frame) then
- _update_buttons()
- _update()
- should_draw = true
- end
- do_frame = not do_frame
else
+ yield() -- yield each other frame for 30fps
_update_buttons()
- should_draw = true
+ if (_update) _update()
+ end
+ if _draw then
+ _draw()
+ flip()
+ else
+ yield()
end
- if (_draw and should_draw) _draw()
- yield()
end
end
]]
__z8_loop = cocreate(function()
-
-- Memory should be reset to default by the VM before this call
-- First reload cart into memory
reload()
@@ -568,10 +564,10 @@ function __z8_run_cart(cart_code)
-- Load cart and run the user-provided functions. Note that if the
-- cart code returns before the end, our added code will not be
- -- executed, and nothing will work. This is also PICO-8’s behaviour.
+ -- executed, and nothing will work. This is also PICO-8's behaviour.
-- The code has to be appended as a string because the functions
-- may be stored in local variables.
- --__z8_load_code points to lua's load function https://www.lua.org/manual/5.2/manual.html#pdf-load
+ -- __z8_load_code points to lua's load function https://www.lua.org/manual/5.2/manual.html#pdf-load
if __cart_sandbox ~= nil then
__cart_sandbox = nil
@@ -587,6 +583,9 @@ function __z8_run_cart(cart_code)
error()
end
+ -- Mark that a cart is now running (used for pause menu logic)
+ __z8_cart_running = true
+
-- Run cart code
code()
end)
diff --git a/source/vm.cpp b/source/vm.cpp
index 0a83afb..8761087 100644
--- a/source/vm.cpp
+++ b/source/vm.cpp
@@ -1325,16 +1325,25 @@ void Vm::vm_extcmd(std::string cmd){
}
void Vm::vm_load(std::string filename, std::string breadcrumb, std::string param){
+ Logger_Write("vm_load: loading %s\n", filename.c_str());
+
_cartBreadcrumb = breadcrumb;
if (param.length() > 0) {
_cartParam = param;
}
- QueueCartChange(filename);
- LoadCart(_nextCartKey);
-
- //todo: don't call this here? probably need to change load to not clear out some memory?
- vm_run();
+ if (_currentCartdataKey.length() > 0) {
+ _host->saveCartData(_currentCartdataKey, getSerializedCartData());
+ }
+
+ _prevCartKey = CurrentCartFilename();
+
+ LoadCart(filename);
+
+ // Only run if cart loaded successfully
+ if (_loadedCart && !_loadedCart->LuaString.empty()) {
+ vm_run();
+ }
}
void Vm::vm_reset(){