[Help]Saving/Loading

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
buhb11
Citizen
Posts: 81
Joined: Wed Dec 26, 2012 8:59 pm

[Help]Saving/Loading

Post by buhb11 »

Hey Lovers,

I am working on my platformer game and i`d like to know a way for saving/loading my levels.I know i should do something with love.filsystem, and i`ve tried a couple of things but i can`t write and save a level correctly.
So let me explain you better,i have a variable that stores my current level and its name is "gameLevel" ,every time when i pass a level this variable increments +1 and it loads a new map.But now if the user exists the game i want to store the current "gameLevel" somewhere in my PC and when the user restarts the game it will resume.
If you can give me an exemple i`d be very happy.
Have a nice day :ultrahappy:
I found Love very enjoyable and nice!
User avatar
veethree
Inner party member
Posts: 875
Joined: Sat Dec 10, 2011 7:18 pm

Re: [Help]Saving/Loading

Post by veethree »

Coincidentally i was trying to figure this out for myself just yesterday. I'll attach what i came up with, It's capable of saving and loading a string, It might help you get started.

the way the demo works is you press "s" and it will add a random number to the file, Then on next run it should load that same file and display it.

Keep in mind this is probably not ideal as it's my very first experience with files in löve.

Also worth mentioning is it saves the file in this directory: "C:\Users\your_username\AppData\Roaming\LOVE\whatever_the_game_is_called"
User avatar
MadByte
Party member
Posts: 533
Joined: Fri May 03, 2013 6:42 pm
Location: Braunschweig, Germany

Re: [Help]Saving/Loading

Post by MadByte »

If you are willing to use a third party library I would suggest you try "TSerial" to serialize a table and
then reload it on start. It's pretty easy to use :

Save:

Code: Select all

love.filesystem.write( "level.lua", TSerial.pack(levelinfo) )
Load:

Code: Select all

love.filesystem.setIdentity( "YourGameName" ) -- Saving directory
if love.filesystem.exists( "level.lua" ) then
	levelinfo = TSerial.unpack( love.filesystem.read("level.lua") )
end
tip : with love.filesystem.read / write you don't need to open() / set file:write() or file:read() / close() the file, that will be done automatically.

EDIT: Here's a demo love on what I mean! ( number saves on quit ).
save_load file.love
User avatar
Lafolie
Inner party member
Posts: 809
Joined: Tue Apr 05, 2011 2:59 pm
Location: SR388
Contact:

Re: [Help]Saving/Loading

Post by Lafolie »

I wrote an easy-to-use saving script a while ago https://github.com/Lafolie/Jupiter.
Do you recognise when the world won't stop for you? Or when the days don't care what you've got to do? When the weight's too tough to lift up, what do you? Don't let them choose for you, that's on you.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 54 guests