• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
O-san

Save game

6 posts in this topic

Hello!

I'm developing a game which uses lua for scripting. I am at a point in development where I want to have save files/states and I am wondering what's the best solution. My scripts usually have a table where most of the variables reside, I use no coroutines in my scripts. Is it a good idea to somehow iterate through the lua stack and save the variables to file?

I have searched around a bit and found a library called pluto but I am not very confident that it will work with the latest lua build (5.2). Moreover it seems to have issues with 64bit systems.

Any thoughts on lua stack saving is greatly appreciated. Edited by O-san
0

Share this post


Link to post
Share on other sites
[quote name='O-san' timestamp='1339583282' post='4948775']
Hello!

I'm developing a game which uses lua for scripting. I am at a point in development where I want to have save files/states and I am wondering what's the best solution. My scripts usually have a local table where most of the variables reside, I use no coroutines in my scripts. Is it a good idea to somehow iterate through the lua stack and save the variables to file?

I have searched around a bit and found a library called pluto but I am not very confident that it will work with the latest lua build (5.2). Moreover it seems to have issues with 64bit systems.

Any thoughts on lua stack saving is greatly appreciated.
[/quote]
Exists your game state really only in lua ? What about references (user data) to external (i.e. C++/C) data ?

I use a simple [i]serialization=>deserialization=>post processing [/i]mechanism to save my game state (mostly lua) and reconnect it with engine related data (C++) after reloading it. An other consideration is, that you should take care of game related data which will change quite often during development. I.e. some item data or combat tables which changes frequently, saving this with the lua stack will render saved game useless, and you need saved games once you reach a late development stage.

An other approach, though more expensive, is to export/import your game state. This is similar to de-/serialization, but much more loosly (and error prone), think of a kind of simple script format which is needed to load your game and insert all entites etc. into the game (de-/serialization is more like an 1:1 mapping). This way frequent changes to the game or game related meta data can be managed really easily. Edited by Ashaman73
1

Share this post


Link to post
Share on other sites
[quote name='Ashaman73' timestamp='1339584875' post='4948779']
Exists your game state really only in lua ? What about references (user data) to external (i.e. C++/C) data ?
[/quote]

I got some of my functionality scripted, not all of it by any means. My NPCs can have a behaviour script associated with them. These scripts can read variables from the "engine" and also set new values to these variables in some cases (through lua_register C funcitons). An NPC might have unique variables too, only available in lua. Such as if the player has talked or interacted in someway with the NPC.

Could you describe your [i]serialization [/i]a bit, it sounds like something I am leaning towards.

I haven't written a game save system before so this is all quite new to me.
0

Share this post


Link to post
Share on other sites
The basic idea behind my serialization approach is, to assign an unique id to each object (=lua table) and use this unique id to save associations between objects. To archieve this, I've started to assign an unique id to each object at creation time:

[CODE]
-- global data
uid_counter = 0

uid_map = {}

createNewObject = function()
uid_counter = uid_counter + 1
local object = {}
object.uid = uid_counter
uid_map[uid_counter] = object

return object
end[/CODE]

Then ,when you write your data to a file, exchange any references to tables by its uid, mark each uid with a special char ,i.e. '#', to distinquish it from other variables.

[CODE]
serializeObject = function( _obj)
write("new_object={")

for key,value in pairs(_obj) do
-- is this a table ?
if type(value)=="table" then
-- is this a object ?
if value.uid~=nil then
-- save reference only
write(" "..key.."= [[#"..value.uid.."]],")
else
-- just a table, write values as table, ignore nested tables/objects
..
else
-- just normal attribute, save it according to it type
if type(value)=="string" then
write(" "..key.."= \""..value.."\",")
...
end
end
-- close object
write("}")
end
[/CODE]

We only create objects and hold them in a map which prevents garbage collection, either delete them directly(bad) or clean them up (i.e. just before saving game). Ensure that you really have a complete object net, that is, you need some kind of root object.
[CODE]cleanup = function()
local is_referenced = {}
for uid,object in pairs(uid_map) do
for key,value in pairs(object) do
if type(value)=="table" and value.uid~=nil then
is_referenced[value.uid] == true
end
end
end

-- remove all objects, which are no longer references and is not the root object
for uid,object in pairs(uid_map) do
if root_uid~=uid and is_referenced[value.uid]==nil then
-- remove
uid_map[uid] = nil
end
end
end
[/CODE]
Now we want to serialize the whole object data structure
[CODE]serializeAll = function()
-- clean up first
cleanup()

-- serialize them now
for uid,object in pairs(uid_map) do
serializeObject(object)
end
end
[/CODE]

You now should have a file like this one:
[CODE]
new_object =
{
uid = 23,
test = "Hello World",
attr = 10,
}
new_object =
{
uid = 25,
my_ref = [[#23]],
other_attr = "2928",
}
[/CODE]

To deserizialize it (aka loading), just write a new_object method and execute the doFile lua function.
[CODE]
new_object = function(_input)
-- save it to uid map
uid_map[_input.uid] = _input
end

-- once you have read it, you need to reconnect the data
postprocess = function()
local max_uid = 0
for uid,object in pairs(uid_map) do
for key,value in pairs(object) do
if type(value)=="string" and string.find(value,"#")~=nil then
-- extract uid (home work)
local ref_uid = ...
--reconnect with table
object[key]=uid_map[ref_uid]
end
end
max_uid = math.max(uid,max_uid)
end

-- init counter
uid_counter = max_uid
end
[/CODE]

That is the basic idea, it is really simple, but need some work:
1. You can compress uids to keep them in a certain range.
2. You can use fixed uid ranges which represent meta-data (i.e. combat tables etc.) and will not be serialized (though the will be automatically reconnected).
3. You can expand this to use C/C++ engine objects (just assign uids to these objects too and write a seraziation mechanism to write them back too).
4. Add some error checking. Edited by Ashaman73
2

Share this post


Link to post
Share on other sites
Thanks for the comprehensive reply! I think I got most of it but one thing that puzzles me is how the resulting file new_object={...}new_object=... can call the new_object method. The contents looks to me like a table construction and not a function call to new_object(input).
0

Share this post


Link to post
Share on other sites
Oopps, sorry, these are chunks, not tables. You can define a [i]chunk reader function [/i]anywhere and then use [i]dofile [/i]on a file including some chunks which will call the chunk reader function with the given data as table. Example:
[CODE]
-- declare chunk reader function, could be any function anywhere
new_chunk = function(_input)
print("New chunk")
for k,v in pairs(_input) do
print("== "..tostring(k).."="..tostring(v))
end
end

-- a chunk could be build like : <chunk reader function>{ ...content...}
new_chunk
{
first = "hello",
second = 21,
}
new_chunk
{
third = "world",
forth = 42,
}
[/CODE] Edited by Ashaman73
0

Share this post


Link to post
Share on other sites
Oops, now I see the omitted =, that makes it a chunk.. doh! The little things!

[s]I must be missing something, that does not compute for me. My code looks like...

savefile.txt:

[CODE]newObject=
{
id = 1,
}
newObject=
{
id = 2,
}[/CODE]

My script got these few lines:

[CODE]newObject= function(input)
print("New object")
for k,v in pairs(input) do
print("== "..tostring(k).."="..tostring(v))
end
end

dofile("savefile.txt")[/CODE]

As soon as I dofile("savefile.txt") my newObject function type becomes a table type and I am missing why it should not. Thanks for taking your time! [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img][/s] Edited by O-san
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0