Save Game/Load Game

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.
User avatar
NemoVonFish
Prole
Posts: 18
Joined: Sun Feb 24, 2013 7:48 am

Save Game/Load Game

Post by NemoVonFish »

First time posting ever, as you may be able to guess from the number over there -->

I am having trouble understanding the whole filesystem thing, the sheer number of file and fileData and newFile and newFileData has me all confused and I don't know which is relevant for what I want to do :?.

Alright, so, the goal is to save the contents of a bunch of variables (currently they're stored in a table, such as hero.gold, hero.exp, hero.level, hero.name etc) to a file (i'm assuming it will be .txt?) when the Save button is clicked, then load those variables when the Load button is clicked

My problem is that I can't find any tutorials or guides showing what I want to do that I can understand, as they're all beyond my four days of experience :cry:.

If someone could take the time to write a very simple variable saving/loading example i'd very much appreciate it.
Zeliarden
Party member
Posts: 139
Joined: Tue Feb 28, 2012 4:40 pm

Re: Save Game/Load Game

Post by Zeliarden »

Yo!
get the table.show function from http://lua-users.org/wiki/TableSerialization

Code: Select all

 hero = {}
 hero.gold = 328
 hero.exp = 212
 hero.level = 3
 hero.name = "Zel"

--save
success = love.filesystem.write( "test.lua", table.show(hero, "loadedhero")) -- puts the hero table as lua code table named loadedhero

--load
chunk = love.filesystem.load( "test.lua" )
chunk()  -- this runs the code -> makes a table named loadedhero

print(loadedhero.level)
print(loadedhero.name)
User avatar
NemoVonFish
Prole
Posts: 18
Joined: Sun Feb 24, 2013 7:48 am

Re: Save Game/Load Game

Post by NemoVonFish »

Would changing

Code: Select all

love.filesystem.write("test.lua",
to

Code: Select all

love.filesystem.write(hero.name..".lua",
name the file after hero.name? Or is that just going to confuse the crap out of the function?

Also, if we change

Code: Select all

table.show(hero, "loadedhero"))
to

Code: Select all

table.show(hero, "hero"))
would that, when loaded, overwrite all the hero. variables?
Zeliarden
Party member
Posts: 139
Joined: Tue Feb 28, 2012 4:40 pm

Re: Save Game/Load Game

Post by Zeliarden »

you could do it like this

Code: Select all

savename = hero.name .. ".sav"
success = love.filesystem.write( savename, table.show(hero, "hero"))
chunk = love.filesystem.load( savename )
chunk()
would save as hero name .sav and overwrite the hero table when loaded
looking around a bit https://love2d.org/wiki/TLTools is probably better to use for saving than table.show
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: Save Game/Load Game

Post by Taehl »

If you want to use Tserial, your code would look like this:

Code: Select all

local savename = hero.name .. ".sav"
-- save hero table to file
love.filesystem.write( savename, Tserial.pack(hero) )
-- load hero table from file
hero = Tserial.unpack( love.filesystem.read( savename ) )
Tserial is nice because it converts tables to Lua script, so it'll be very easy to read and edit your save-games. If you want to output in "human-readable mode", with newlines and proper indentation, set the third parameter to true, like Tserial.pack(hero, false, true).

Note that Tserial can't serialize userdata like graphics and physics objects. If you try, it'll throw an error to remind you of this. But if you want it to just skip the userdata and keep going, make the second parameter true, like Tserial.pack(hero, true).
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
User avatar
NemoVonFish
Prole
Posts: 18
Joined: Sun Feb 24, 2013 7:48 am

Re: Save Game/Load Game

Post by NemoVonFish »

Taehl wrote:

Code: Select all

local savename = hero.name .. ".sav"
-- save hero table to file
love.filesystem.write( savename, Tserial.pack(hero) )
-- load hero table from file
hero = Tserial.unpack( love.filesystem.read( savename ) )
Works perfectly! However (there's always a problem ;_;) It bluescreens if you try to load a file that doesn't exist, is there any way to handle this better than crashing and burning?

*EDIT*
Uh oh, also, I have no idea where it saves the .sav file, doing a search for *.sav does nothing, and watching the save folder doesn't show any files while the program is running.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Save Game/Load Game

Post by Robin »

You can do a check with love.filesystem.exists first.

Also: they are stored in the save directory.
Help us help you: attach a .love.
User avatar
Lafolie
Inner party member
Posts: 809
Joined: Tue Apr 05, 2011 2:59 pm
Location: SR388
Contact:

Re: Save Game/Load Game

Post by Lafolie »

You can also execute a file loaded with love.filesystem immediately after loading it.

Code: Select all

file = love.filesystem.load("path.lua")()
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.
User avatar
NemoVonFish
Prole
Posts: 18
Joined: Sun Feb 24, 2013 7:48 am

Re: Save Game/Load Game

Post by NemoVonFish »

I have that classic problem: It works, but I have no idea why. You can save your game, but I can't find where it saves it. For example; one of my test save games is called "Butts" (don't judge me) I can't find a file called Butts, I can't find any files that contain the word Butts, even though according to

Code: Select all

hero.name = "(entered string, which in this case is "Butts")"
hero.savename = hero.name..".sav"
love.filesystem.write(hero.savename, Tserial.pack(hero, false, true))
The save file should be called "Butts.sav" in the /(Name of the folder containing main.lua)/save/, correct?
User avatar
NemoVonFish
Prole
Posts: 18
Joined: Sun Feb 24, 2013 7:48 am

Re: Save Game/Load Game

Post by NemoVonFish »

Oop, found it, strangely enough exactly where the Wiki said it would be:
love2d.org/wiki/love.filesystem wrote:Windows Vista and 7: C:\Users\user\AppData\Roaming\LOVE or %appdata%\LOVE\
Is there any way to get it to save in a different location? Like create a folder in its directory called save, and plonk it in there? Or are we venturing dangerously close to having files outside the .exe?
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 2 guests