puddleglumm [Lexaloffle Blog Feed]https://www.lexaloffle.com/bbs/?uid=67458 Implementing replay functionality in PICO-8 <p>Hello friends! I'm attempting to implement replay functionality in PICO-8. (e.g. re-watching your last race in a racing game) I started by changing my update function, so that instead of invoking game actions directly, they are added to a command queue. Imagine something like this:</p> <div> <div class=scrollable_with_touch style="width:100%; max-width:800px; overflow:auto; margin-bottom:12px"> <table style="width:100%" cellspacing=0 cellpadding=0> <tr><td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> <td background=/gfx/code_bg0.png> <div style="font-family : courier; color: #000000; display:absolute; padding-left:10px; padding-top:4px; padding-bottom:4px; "> <pre>function move_player(x, y) -- move ZIG for great justice end function update_game() if btnp(➡️) then add_cmd(move_player, {1, 0}) end -- other input handling here process_commands() end</pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <p>An entry in the queue is a function, parameters (passed as a table), and the frame that the command was added (e.g. 23). At the end of the update function I execute everything in the queue that is scheduled for the current frame. <code>process_commands</code> uses an index to track where the next command is, and increments the index whenever it executes one. To replay a game, all I have to do is reset my game state and command queue the index to 1, and run my game engine against the already-populated command queue. So far so good, easy enough to implement and it's working fine. </p> <p>The next thing I thought was how I could use this to run a demo mode for the game. I play the game, I somehow persist the command queue, and use it to drive the demo mode. So now I need to persist this command queue outside of the Lua runtime. My first thought was looking into serialization of data in Lua, but if I understand Lua correctly I'd be serializing the same functions over and over, and it looks like I don't have access to string.dump anyways.</p> <p>My best idea now is creating a table that keys the string value of my function names on the various game action functions, and add some code to <code>add_cmd</code> to actually build a string of Lua code that will re-create my table. Seems kinda derpy but it would work, and does require me to change the name table if I change or add game actions. </p> <p>Another alternative would be to record the raw input data for every frame and re-write my input handling code so that exactly where the input data is coming from is abstracted. Then my data is just a bunch of booleans that are easily serializable, but I hate this because now I'm tracking way more data than I care about, and it makes handling actual user input while the replay is running less straightforward.</p> <p>How would you approach implementing replay functionality in PICO-8?</p> <p>I'm a lifelong programmer but very new to game development, feel free to criticize / correct as you see fit, you won't hurt my feelings! I love learning.</p> https://www.lexaloffle.com/bbs/?tid=54919 https://www.lexaloffle.com/bbs/?tid=54919 Mon, 06 Nov 2023 18:46:58 UTC