Saving[Please helpDD:]

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
baconhawka7x
Party member
Posts: 491
Joined: Mon Nov 21, 2011 7:05 am
Location: Oregon, USA
Contact:

Saving[Please helpDD:]

Post 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.)
Last edited by baconhawka7x on Wed Mar 28, 2012 10:19 pm, edited 4 times in total.
User avatar
The Burrito
Party member
Posts: 153
Joined: Mon Sep 21, 2009 12:14 am
Contact:

Re: Saving.

Post 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.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Saving.

Post 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.
Help us help you: attach a .love.
User avatar
Golan2781
Prole
Posts: 8
Joined: Sat Jan 07, 2012 9:13 pm

Re: Saving.

Post 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.
meoiswa
Prole
Posts: 48
Joined: Wed Mar 14, 2012 8:13 pm
Location: Venezuela

Re: Saving[Please helpDD:]

Post 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:
Write the ö of Löve with Alt+numPad(148) or Alt+numPad(153)
Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests