Page 1 of 1

Making Savegames

Posted: Sun Oct 04, 2015 4:34 pm
by CanadianGamer
Can Anyone help me I'm trying to make a game that saves both the settings and the Highscores.

Re: Making Savegames

Posted: Sun Oct 04, 2015 4:48 pm
by Jack5500
There are a few libraries that can help you with that:
https://github.com/JanWerder/awesome-lo ... ialization

Re: Making Savegames

Posted: Sun Oct 04, 2015 4:53 pm
by CanadianGamer
Thank You

Re: Making Savegames

Posted: Wed Oct 07, 2015 1:13 am
by _Juice
The method I use is a library to turn tables into JSON files and save that. You can open the file and then turn the JSON into a table and use it.

Re: Making Savegames

Posted: Wed Oct 07, 2015 3:19 am
by Alexar
i wrote an easy method, just copy the table you need into file as lua script. the output is string, you can print it to test.

Code: Select all

function table.save(tab,name)
    name=name or "test"
    local output="local "..name.."=\n"
    local function ergodic(target,time)
        time=time+1
        output=output.."{\n"
        for k,v in pairs(target) do
            output=output .. string.rep("\t",time)
            if type(v)=="table" then
                if type(k)=="number" then
                    output=output.."["..k.."]".."="
                elseif type(k)=="string" then
                    output=output.."[\""..k.."\"]="
                end 
                ergodic(v,time)
                output=output .. string.rep("\t",time)
                output=output.."},\n"
            elseif type(v)=="string" then
                if type(k)=="number" then
                    output=output.."["..k.."]".."=\""..v.."\",\n"
                elseif type(k)=="string" then
                    output=output.."[\""..k.."\"]=\""..v.."\",\n"
                end 
            elseif type(v)=="number" then
                if type(k)=="number" then
                    output=output.."["..k.."]".."="..v..",\n"
                elseif type(k)=="string" then
                    output=output.."[\""..k.."\"]="..v..",\n"
                end 
            end
        end
    end
    ergodic(tab,0)
    output=output.."}\n return "..name
    return output 
end

Re: Making Savegames

Posted: Wed Oct 07, 2015 12:50 pm
by Robin
Alexar wrote:i wrote an easy method, just copy the table you need into file as lua script. the output is string, you can print it to test.
Note that that method doesn't deal with cycles or some other types of data: strings containing ", nan, inf, keys that aren't strings or numbers, values that aren't tables, strings or numbers (like booleans), ...

There are plenty of good serialisation libraries available, I recommend using one of those (especially binser or one of my libraries: Ser, Lady or Smallfolk).

Re: Making Savegames

Posted: Thu Oct 08, 2015 12:49 am
by Alexar
Robin wrote: Note that that method doesn't deal with cycles or some other types of data: strings containing ", nan, inf, keys that aren't strings or numbers, values that aren't tables, strings or numbers (like booleans), ...

There are plenty of good serialisation libraries available, I recommend using one of those (especially binser or one of my libraries: Ser, Lady or Smallfolk).
yeah, I forgot boolean. i still use binser or bintable in my own project. but this is an easy way to convert table to readable string.