Jump to content

  • Log In with Google      Sign In   
  • Create Account

Serializing a lua_State ?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 Ziphnor   Members   -  Reputation: 122

Like
Likes
Like

Posted 13 May 2003 - 10:47 AM

Im trying to plan how i will handle serialization(for saving / loading in my game), and all im missing is a neat way to store the state of the scripts running when saving. Im thinking it ought to be pretty simple, due to the stack based nature of lua. So what do i really need to do? I can write the stack itself to a file, and the just push everything back later when loading. But what more do i need to do(heap/instruction pointer perhaps)? Ive tried googling for this, but havent really come up with anything. Anyone have any experience with this? PS: im not using threads at all, the lua scripts runs and control returns to the C side when the script is finished, so none of the scripts will be running when script is to be saved from the native side. I assume this should makes things easier, serialization wise. Similar, the scripts dont have any type of pointers into the C side, they are only aware of some object handles, which will be the same when the game is restored. [edited by - ziphnor on May 13, 2003 6:19:42 PM] [edited by - ziphnor on May 13, 2003 6:28:30 PM]

Sponsor:

#2 Kylotan   Moderators   -  Reputation: 3338

Like
Likes
Like

Posted 14 May 2003 - 12:11 AM

I can't answer your exact question, but maybe this will help?
http://www.dekorte.com/Software/Lua/LuaPickle/

[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost
Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]


[edited by - Kylotan on May 14, 2003 7:12:17 AM]

#3 Ziphnor   Members   -  Reputation: 122

Like
Likes
Like

Posted 14 May 2003 - 05:21 AM

Thanks, but i already found that one, i also found this http://lua-users.org/lists/lua-l/2003-04/msg00339.html which, is a more general(ie harder) version of my problem, but no real solution is given.
I suppose i could check the source code for the lua_State and find out how its structured, but it seems likely that someone has already described this...
With my limited understanding of virtual stack machines it seems to me that simply storing the stack values(and possibly the heap, hell i dont even know if lua uses a heap? should be enough, if all the compiled code is also loaded again at load time. I mean im not trying to save it while its running so actually the instruction pointer etc. shouldnt be needed. Perhaps im missing something

[edited by - ziphnor on May 14, 2003 12:23:07 PM]

#4 spiffgq   Members   -  Reputation: 122

Like
Likes
Like

Posted 14 May 2003 - 05:32 AM

Did you post your question on the Lua mailing list? I would be interested to follow that discussion.

#5 Ziphnor   Members   -  Reputation: 122

Like
Likes
Like

Posted 14 May 2003 - 06:03 AM

Something tells me that i should probably research it a bit more myself. 5 minutes with the reference manual, and i realized i misunderstood how lua handles global variables for example. Apparently they are stored in a seperate table etc.
But when ive read the material a bit more thoroughly you can be sure that ill try asking

#6 LordShade   Members   -  Reputation: 212

Like
Likes
Like

Posted 14 May 2003 - 07:58 AM

Since your Lua scripts are not threaded, why would you need to save the stack? The stack should be empty when the script returns. Barring return variables that is.

Saving the states of the global variables I can understand.

[edited by - LordShade on May 14, 2003 2:59:02 PM]

#7 Ziphnor   Members   -  Reputation: 122

Like
Likes
Like

Posted 14 May 2003 - 09:03 AM

Yeah, sorry about that, i thought the globals etc was stored on the stack(due to how we handled globals in a micro-C language in a compiler course i had , my mistake.

So perhaps i could just walk through the global table, saving everything i meet recursively(thus including other tables etc) except for functions. Then when loading i first load the code chunk(s) again, and then restore the tables, i suppose that should work, shouldnt it? The only question is whether or not this correctly restores the functions(that is the function code+their enviroment).

[edited by - ziphnor on May 14, 2003 4:04:41 PM]

#8 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 15 May 2003 - 03:53 AM

You could organize it so that the data is separate from the program, and then just go through and store all the data.

All the functions would be loaded from a file when your program starts, and then loading a saved game would just set all the data to the saved values.

Going through and saving everything that isn''t a function sounds like it could work. Really you just need to figure out what needs saving.

#9 Ziphnor   Members   -  Reputation: 122

Like
Likes
Like

Posted 15 May 2003 - 08:44 AM

I tried a simple scenario where i ran a simple lua file stored all the global numbers and strings (name and values) in some vectors. Then i closed the lua_State, opened a new one reloaded the lua file and libs, and the reinserted the values. After that i ran another small lua script that tested the values/functions in the previous one, it worked fine. But again, this is such a simple situation, its much more interesting what happens with things like:

local counter = 0
function inc() counter++ end

According to normal lua standards(5.0) the counter can be accessed by the function but its not part of any table etc reachable from the global table. Perhaps its contained in the function( or should i say function closure) or something like that? I wish knew more about lua

EDIT: I just noticed that its possible to retrieve a table giving the enviroment of the function using:
void lua_getfenv (lua_State *L, int index);
int lua_setfenv (lua_State *L, int index);
With this it should be possible to properly store the state of functions, since we can now serialize their entire local enviroment.

[edited by - ziphnor on May 15, 2003 3:52:27 PM]




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS