Support for saving/loading the Lua state?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
Kuromeku
Party member
Posts: 166
Joined: Sun Jul 20, 2008 5:45 pm

Support for saving/loading the Lua state?

Post by Kuromeku »

For saved games? Unless anyone has any really good ways of doing this?
surtic
Citizen
Posts: 74
Joined: Sat Jul 12, 2008 12:18 am

Re: Support for saving/loading the Lua state?

Post 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).
User avatar
Kuromeku
Party member
Posts: 166
Joined: Sun Jul 20, 2008 5:45 pm

Re: Support for saving/loading the Lua state?

Post 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).
Mr. Strange
Party member
Posts: 101
Joined: Mon Aug 11, 2008 5:19 am

Re: Support for saving/loading the Lua state?

Post 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
User avatar
rude
Administrator
Posts: 1052
Joined: Mon Feb 04, 2008 3:58 pm
Location: Oslo, Norway

Re: Support for saving/loading the Lua state?

Post 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.
User avatar
mike
Administrator
Posts: 364
Joined: Mon Feb 04, 2008 5:24 pm

Re: Support for saving/loading the Lua state?

Post 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.
Now posting IN STEREO (where available)
User avatar
Kuromeku
Party member
Posts: 166
Joined: Sun Jul 20, 2008 5:45 pm

Re: Support for saving/loading the Lua state?

Post 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)
surtic
Citizen
Posts: 74
Joined: Sat Jul 12, 2008 12:18 am

Re: Support for saving/loading the Lua state?

Post 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).
User avatar
Lord Tim
Prole
Posts: 30
Joined: Sun Jul 20, 2008 4:07 am

Re: Support for saving/loading the Lua state?

Post 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.
User avatar
Kuromeku
Party member
Posts: 166
Joined: Sun Jul 20, 2008 5:45 pm

Re: Support for saving/loading the Lua state?

Post 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).
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 66 guests