Using the Lua C API 5.3, I would like to pause a script to be able to resume it later (potentially after a game state is loaded from disk). I do not believe co-routines/yielding can be safely used because I am building the state of local variables at the beginning of scripts and I want this solution to work after the app has fully restarted. Essentially, maintaining the original Lua state objects is not realistic.
Example script:
dothisfirst()
wait(10 seconds)
dothatlast()
Potential Solution #1: Wrapping the whole script in a co-routine; however, the entire Lua state would have to be preserved in some unique way. This is not useful in case the script wishes to be resumed at a later date, from a saved game state or something.
Potential Solution #2: I have thought about having the Lua script manipulated, so the portion before and after any script pauses turns into a Lua script that can be executable multiple times, where on the first run it does the first part, the second and so on runs would wait for the time specified, and the last run would do the last part. A global variable would be used to indicate the state of the machine.
Potential Solution #3: Instead of using luaL_dostring(), I could pre-process the script, process conditions/loops manually, and execute individual lines manually. This seems like the simplest and safest approach.
The big kicker is making sure the solution works with nested loops. And as stated, the solution needs to be able to resume after the app has restarted; in that the state can be saved and reloaded.
I guess I was hoping to get feedback from the community as to whether there's other solutions or if the community might have any suggestions approaching any of the above solutions.
Thank you!
lua_Stateto a previous point of execution. That is, you want to be able to save the executing state, continue executing, then reload the previous state as if the later execution had never happened. That's not a thing you can just do in Lua (or indeed most scripting languages). Not generally, at any rate.