Skip to main content
gamedev.net
🔒 Locked

Saving lua globals

Started by msamurai 3 replies 1.2k views
Original Post
msamurai
msamurai
Hello

I have integrated lua with my game. Everything works fine, but I can't find any easy way to keep persistent data in the scripts, ie: data that can be stored to/retrieved from a save file.

In the worst case I could of course register some functions like Set/GetVariable(name, value) and store the variables as strings engine-side, but that would make the scripts more difficult to maintain, not to mention ugly :).

Ideally what I would like to save is the global variables of my lua context. Does anyone know how this can be done using the lua C API?

Thank you

Michael
Sneftel
Sneftel
Global variables are stored in the global table, which can be iterated over like any other table. You can use something like this to write these values to a file, but you'll need to put in exclusions for the things you can't (and don't want to) save, like the builtin modules and functions. Alternatively, you can use Pluto, which can store a wider range of datatypes.
msamurai
msamurai
Thanks for the quick reply. Its very handly that all globals are in a table.

So the table's name is "global"?

Edit: I mean, would this be ok?

lua_getglobal(L, "global");

Thanks again :)
Sneftel
Sneftel
No, the global table is not named "global". See section 3 of the lua manual for how to access the global table directly.
msamurai
msamurai
Thanks again. Acually it was easy :)

In case anyone else is interested, I have posted the method to my blog:

http://cdry.wordpress.com/2010/06/26/how-to-save-global-variables-using-the-lua-c-api/

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.