lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Thursday 06 May 2004 17:07, Krzysztof Jakubowski wrote:
> Hi,
> I am new here. I need to serialize lua_state. I was searching mailing
> archives, but all posts were about serializing tables, globals, etc. There
> is also lua_dump or luaU_dump, but it doesn't solve my problem becouse I
> need to save "current instruction pointer".
>
> I would like to break execution of script (using lua_yield), save the whole
> state and exit the game. Later I would like to load the state and resume
> (lua_resume) the execution of script. Is is possible?

If I really felt the need to implement logic as a single function rather than 
a state machine, I might start thinking about preprocessing the Lua code, 
e.g. to turn this:

    function my_game()
        local x = do_stuff()
        %YIELDPOINT%
        return do_more_stuff(x)
    end

Into this:

    function my_game()
        local x = do_stuff()
        return my_yield(42, x)
    end

    continuations[42] = function(x)
         return do_more_stuff(x)
    end

The simplicity of the Lua grammar makes that not too hard to do (and with care 
you could even avoid screwing up the line numbers for debugging). Provided 
you aren't doing anything funky with function environments, it means that you 
can just save the global table and any locals using standard methods. Doing 
it that way is a lot less fragile than messing about with the Lua internals.

The state machine is the better option though, even if it seems like more 
marginal work. Really.

-- Jamie Webb