Page 1 of 1

Saving[Please helpDD:]

Posted: Tue Jan 24, 2012 5:03 am
by baconhawka7x
I have no clue how to save the players wave, health, and what weapons are unlocked(x_unlocked = true(x = "pistol""shotgun"etc.."). If someone could help me with it I'd really appreciate it.


EDIT:

So I'm now starting to attempt implementing saving into my game. And the first thing I noticed is, that if I update my game, then the save file will change too. So is there a way I can put the save file somewhere else out of the game, and then load it from there?(If so, where should I put it? Because the only obstacle that I'm seeing is if someones on a mac, and I save it lets say, in application support, then it probably wouldn't save on a windows or linuxr, because the file path is probably completely different)

also, is there a way that I can clear the save file?
I have these lines of code..

Code: Select all

if key == "l" then
		if love.filesystem.isFile("save.lua") then
			love.filesystem.load("save.lua")()
		end
	end
	if key == "o" then
		if love.filesystem.isFile("save.lua") then
			save:open('w')
			save:write("steve.x = "..steve.x)
			save:close()
		else
			save = love.filesystem.newFile('save.lua')
			save:open('w')
			save:write("steve.x = "..steve.x.." camera.x = "..camera.x)
			save:close()
		end

	end
but I noticed that the first save was the only save that took effect. I'm guessing that's because it was just writing something like..

Code: Select all

steve.x = 5 steve.x = 16 steve.x = 60
because I never cleared the file, I just added on to it.
So is there a way to clear the file?(not delete the file itself. Just everything inside of it.)

Re: Saving.

Posted: Tue Jan 24, 2012 5:34 am
by The Burrito
I assume you mean between sessions.
You'll probably want to use love.filesystem.newFile() to generate a file, and you can load it using love.filesystem.load(). So you might have something that looks like this to create a save:

Code: Select all

savefile = love.filesystem.newFile('save.lua')
savefile:open('w')
savefile:write('savedthing = ' .. variable)
savefile:close()
To load it all you have to do is:

Code: Select all

love.filesystem.load('save.lua')()
and you can use love.filesystem.isFile( 'save.lua' ) to check to see if there already is a file.

Then just reference savedthing (or whatever you wrote in the file) to get your data back. If you have a bunch of separate things to save you can call file:write() multiple times and it will just tack onto the end of the file. Google should be able to tell you anything you need to know about specific string manipulation stuff.

This is the easiest method I know of to make saves, and it's good for testing because you can manually write your own custom save files.

Re: Saving.

Posted: Tue Jan 24, 2012 8:34 am
by Robin
And if you want to store tables (which you will want), you need to use a table serialization library. One of these or something like this is probably right.

Re: Saving.

Posted: Wed Mar 28, 2012 4:03 pm
by Golan2781
If you just want to save a simple (nested) variable-value table, this solution is pretty lightweight. It works perfectly for storing maps so it should be enough for regular player data as well.

Re: Saving[Please helpDD:]

Posted: Thu Mar 29, 2012 3:23 pm
by meoiswa
Writing a file is without a doubt your best option, it will allow you to store data between programs with ease. As mentioned before, you can make and read your own file (Something simple like a line for each variable with its value) like I used to do on my java classes projects. However, if you want to store more data, like entire tables, with ease and without having to tweak the code every time you add something new, you should try using a serialization library, plus some encoding if you want to avoid players editing their savefiles with ease.
In shameless self-promotion :oops:, maybe you could give my serialize.lua a test? It's pretty straightforward and it would be nice to have some feedback on it :ultrahappy: