Page 1 of 2

Support for saving/loading the Lua state?

Posted: Sun Nov 30, 2008 8:06 pm
by Kuromeku
For saved games? Unless anyone has any really good ways of doing this?

Re: Support for saving/loading the Lua state?

Posted: Sun Nov 30, 2008 10:07 pm
by surtic
I don't think there's a way to save or load a Lua state, but I'm not sure you have to go that far. If you can represent the state of the game using variables (i.e. you don't need to remember the state of the stack or the program counter) then you can just save and load the content of the variables.

You can do it in many ways. One example is here: http://lua-users.org/wiki/PersistentTables (note that it's for Lua 4, if you need help with that I can probably come up with a Lua 5.1 implementation).

Re: Support for saving/loading the Lua state?

Posted: Sun Nov 30, 2008 11:04 pm
by Kuromeku
Thanks for the link I'll look into this, but I'm talking about it's going to be difficult to save entity's and other variables which reference to an entity (each entity has a unique index).

Re: Support for saving/loading the Lua state?

Posted: Sun Nov 30, 2008 11:28 pm
by Mr. Strange
Structuring your data such that it is reasonable to save and load your game needs to be an early part of your planning. If you're having trouble, I think it would be best to restructure your program - it's the only way to deal with the save/load issue properly down the line.

--Mr. Strange

Re: Support for saving/loading the Lua state?

Posted: Mon Dec 01, 2008 12:10 am
by rude
It's possible to save the entire Lua VM to a file, but it wouldn't do us much good. Textures in OpenGL would vanish, sound data would not persist, etc.

Re: Support for saving/loading the Lua state?

Posted: Mon Dec 01, 2008 8:10 am
by mike
Also, think about how much space would be wasted. In a simple game where you only need to remember what level someone is on, then it would always be better to just save a single variable than to dump the whole VM. If you want to apply zen to programming, think of saving games in this fashion:

What is it, in essence, that defines this game session as a unique experience, trapped in the confines of a strict universe?

That should give you the variables you need to save/load games.

Re: Support for saving/loading the Lua state?

Posted: Mon Dec 01, 2008 10:32 am
by Kuromeku
Hey rude, please add this to Love :D

http://luaforge.net/projects/pluto/

http://luaforge.net/forum/forum.php?forum_id=1518 Pluto 2.0 (final) looks bloody amazing.

(or tell me how to compile it and use it as a module :P)

Re: Support for saving/loading the Lua state?

Posted: Mon Dec 01, 2008 9:20 pm
by surtic
Regarding saving the Lua state, I've found these posts (quite old):
http://lua-users.org/lists/lua-l/2000-07/msg00134.html
http://lua-users.org/lists/lua-l/2003-11/msg00003.html

Don't know if they are still relevant. In any case I wouldn't recommend saving the entire VM even if it's possible - that's what I call an overkill (like creating a core dump just to store a few variables).

Re: Support for saving/loading the Lua state?

Posted: Tue Dec 02, 2008 1:39 am
by Lord Tim
The way I set it up, a "save" file is just a single line of Lua code saved as text to a file, which returns a table that holds all the info for that level. Then you can just go dofile("levelname"), and you've got the level back.

Re: Support for saving/loading the Lua state?

Posted: Tue Dec 02, 2008 1:21 pm
by Kuromeku
The problem is, that then loads the level from the start.

I want it to load exactly how it was, entities with their exact position, angles, and any custom info set for them.

If Rude could make that addon I suggested a few posts up come with Love by default, that'd be awesome. (Or if someone could tell me how to compile it myself and include it with my game).