lua-users home
lua-l archive

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


> I'm making a game system, in which I require to save the 
> internal state 
> of the Lua virtual machine. For stuff like C++, this is 
> normally fairly 
> easy, since it's just data. However, in Lua, closures are a pain to 
> serialize. Has anyone ever had to do something similar, and if so can 
> you give me any hints as to this problem?

Hi Brian, I try to avoid this problem by keeping any state information
relevent to the game in C++ and use the scripting to just drive the whole
thing. The C++ objects then serialise themselves to restore any state
information, as you say. I can see how recreating the Lua state from this
could be a complicated pain though. Sorry I'm no use to you!

I noticed your email address is an academic one. I see you are part of the
Harmonia project which looks quite funky. I just wondered what sort of
system you are putting together at Berkeley. It may be none of my business,
in which case, sorry for being nosey :o). Always interested in new ideas.
Can you really do vocal programming? 

"Harmonia is an open, extensible framework for constructing interactive,
language-aware programming tools. An important goal of Harmonia research is
to support large-scale program development, by allowing users to manipulate
programs linguistically rather than textually. Programs can be edited and
transformed according to their structural and semantic components. They can
be represented in non-textual forms, such as graphs. They can be manipulated
using speech and drawing. High-level transformations can be created and
maintained."


> On a related note, I was wondering if the code contained in 
> luac should 
> mostly be moved into the lua core itself. Since we have the 
> ability to 
> load binary files from the core, I would think it would make 
> sense to be 
> able to dump them too. There would be some problems from user defined 
> structures, but I would imagine some sort of dump function in 
> the meta 
> table, and some callback in the loading function would 
> possibly make it 
> easier. Any input would be appreciated.

Another solution might be to serialise any Lua modifications as source text.
eg. if you create a closure, remember the function source string in a table
and you can recreate all the closures when you recreate the state.

-- ACHTUNG! Untested...
_fns = {} -- serialised source associated with fns
function create_fn(src)
  local fn = dostring("return "..src)
  _fns[fn] = src
  return fn
end

butt = Button{ pos={2,3}, onPressed = create_fn("function() beep(7) end") }

-- then when you serialise onPressed you can use the table address as a key
-- this isnt at all thorough but it might help :O)

Regards,
Nick