## Need help making a save file

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
MikeCraftian
Prole
Posts: 2
Joined: Sat Feb 22, 2014 9:19 am

### Need help making a save file

I looked it up and everything just crashes
All I want to do is save a variable

Var = {}
var.number = 0 -- What I want to save

Saving the complete var wouldnt be negative though I lurked here googeled etc everything always comes with (String expected got nil)

Help?

Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

### Re: Need help making a save file

We can't really help you here if you don't upload a .love, because you've not given us enough info to work with.

veethree
Inner party member
Posts: 820
Joined: Sat Dec 10, 2011 7:18 pm

### Re: Need help making a save file

My flappy bird clone keeps track of the players high score by saving and loading a variable from a file. You can look through the code, it might help.

Alternatively, You could use a serialisation library such as Tserial.
Those basically turn tables into strings, and strings back into tables. So you could store your variable in a table, Serialise the table (tserial.pack()), Save the string to a file, Then to load it just turn it into a table again.(tserial.unpack())

Doctory
Party member
Posts: 441
Joined: Fri Dec 27, 2013 4:53 pm

### Re: Need help making a save file

You can use SLib!

Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

### Re: Need help making a save file

Instead of using Tserial, consider using Ser. It's very fast, and very stable, and I wrote it to get people to stop using Tserial.

I don't know enough about SLib to either recommend it or recommend against using it.

EDIT: I have looked at SLib and recommend against using it for the following reasons:
• It produces incorrect results.
1. When encountering cycles in a datastructure, SLib enters an infinite loop.
2. Related: if the serialised value contains multiple references to a single subtable, that subtable will be serialised separately for every reference. That is incorrect behaviour.
3. "Special" numbers (infinity, negative infinity and NaN) are not handled by SLib, making anything that contains one of those values unloadable.
4. The code that checks if string keys can use identifier syntax (key = ... instead of ["key"] = ...) is broken. It allows strings like "end", "1", "*#(@" and " " to be used as identifier keys, while they are quite clearly illegal.
5. If the second argument to slib.pack, called "drop", is a table value (which is always the case if you're using slib.save), then type errors, like if you use a coroutine as a table key or value, will be ignored, causing slib.pack({coroutine.create(function()end)}, {}) to return "}", for example.
• It is slow.
1. SLib uses string concatenation all over the place, which means a table containing N elements will take O(N^2) time, while using table.concat would only take O(N).
2. SLib creates a closure for every (sub)table serialised.
3. Using the benchmarking code from the serpent project, SLib is 2.37 times slower than Ser for serialising. I had to disable the de-serialising benchmarks, because SLib produced incorrect code that interrupted the benchmarking.
EDIT 2: Also, a list of some more minor things:
1. The whole thing's just atrociously indented.
2. It exports a global table.
3. The third argument to slib.pack is called indend, but in other places is always referred to as indent, which means two things:
1. SLib's output never has indentation and newlines, even if you ask for it.
2. The author apparently hasn't tested this library at all.
Last edited by Robin on Sat Feb 22, 2014 8:21 pm, edited 2 times in total.

veethree
Inner party member
Posts: 820
Joined: Sat Dec 10, 2011 7:18 pm

### Re: Need help making a save file

Robin wrote:Instead of using Tserial, consider using Ser. It's very fast, and very stable, and I wrote it to get people to stop using Tserial.

I don't know enough about SLib to either recommend it or recommend against using it.
What's wrong with Tserial?

Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

### Re: Need help making a save file

veethree wrote:What's wrong with Tserial?
It's been a while since I've last reviewed it, but IIRC, it's roughly the same list of problems I have found in SLib (I've updated my post above).

Hexenhammer
Party member
Posts: 175
Joined: Sun Feb 17, 2013 8:19 am

### Re: Need help making a save file

One of the things I love about Lua(JIT) is that I do not have to have the separation of core engine (e.g. C or C++), scripting (e.g. Lua or Python), and serialized data (e.g. XML, JSON, something custom) anymore. I used to develop in that traditional way, and it was so ugly. Translating between the different "worlds" adds so much unnecessary complexity and code. LuaJIT is fast enough that I can do everything in Lua... including save files.

You could serialize everything to Lua tables but I do not like that. Instead my save files are simply code which rebuilds the world state. My situation is more complex, I need to restore an entire game world, which means my saved game code is mostly calls to functions which do things like creating actors, setting their attributes etc. And of course I use locals. I also use delta encoding to keep the save files small without compression. However ignore all that stuff. If you just want to save some values in a simple way using that basic method, here is how you can do it:

Code: Select all

var = {}
var.number = 0 -- Initial value

var.number = 43 -- The value when the user pressed "save" (or whatever)

-- Saving said value to a file
file = io.open("savedGame.lua", "w")
file:write("var.number = ", var.number)
file:close()

-- Resetting to initial value, simulating the effect of a restart
var.number = 0

dofile("savedGame.lua") -- Yes, that's it!

print(var.number) -- Will print 43!


Doctory
Party member
Posts: 441
Joined: Fri Dec 27, 2013 4:53 pm

### Re: Need help making a save file

Hexenhammer wrote:-snip-

-important stuff-

Code: Select all

var = {}
var.number = 0 -- Initial value

var.number = 43 -- The value when the user pressed "save" (or whatever)

-- Saving said value to a file
file = io.open("savedGame.lua", "w")
file:write("var.number = ", var.number)
file:close()

-- Resetting to initial value, simulating the effect of a restart
var.number = 0

dofile("savedGame.lua") -- Yes, that's it!

print(var.number) -- Will print 43!

You sir, just solved my problems.

Ref
Party member
Posts: 690
Joined: Wed May 02, 2012 11:05 pm

### Re: Need help making a save file

Yes!
Trying to generalize your approach finely forced me to figure out how to get the name of a table as a string - something I was always struggling with.
This is the code I ended up with:

Code: Select all

function saveTable( saveto, tab )
local tabname = tableName( tab )
local file = io.open( saveto, 'w' )
file:write( tabname..' = {}\n' )
for I, j in pairs( tab ) do
file:write( tabname..'.'..tostring(i)..' = ', j )
file:write( '\n' )
end
file:close()
end

dofile( from )
end

function tableName( tab )	-- returns table name as a string
assert(type(tab)=='table', "table required")
local n, v
for n, v in pairs( getfenv() ) do
if v == tab then
return n
end
end
return nil
end

Thanks!

### Who is online

Users browsing this forum: No registered users and 41 guests