Can't make a file to save game data

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
JamesWillson
Prole
Posts: 5
Joined: Sun May 12, 2013 6:41 am

Can't make a file to save game data

Post by JamesWillson »

Hello there, and thank you for taking the time to read this.

I'm having a slight problem, I'm trying to save game data, (variables) to a file, but I can't get it to work! I've tried love.file.system.write("save.lua", coins) but it just dosn't work! Is it not saving it to the right place? Or am I doing something wrong? Is it the best way to save game data?

Thanks very much. :)
User avatar
MadByte
Party member
Posts: 533
Joined: Fri May 03, 2013 6:42 pm
Location: Braunschweig, Germany

Re: Can't make a file to save game data

Post by MadByte »

Next time try to post problems in the "Support & Devlopment" area.

I save files by serialize tables.
You will need a Serialization library or write it on your own. I use TSerial.

Then do something like this.

Code: Select all


function saveOptions()
  local file
  file = love.filesystem.newFile( "config.txt" )
  file:open("w")
  file:write( TSerial.pack( options ) )
  file:close()
end


function loadOptions()
  
-- Load the options file 
  local file
  love.filesystem.setIdentity( "nameOfYourGame" )
  if love.filesystem.exists( "config.txt" ) then
    file = love.filesystem.newFile( "config.txt" )
    file:open("r")
    options = TSerial.unpack( file:read() )
    file:close()
  end
end
User avatar
T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

Re: Can't make a file to save game data

Post by T-Bone »

Depends on what "coins" is. You can only write strings to files, because you're writing text files. So you need a way to translate your variables to strings and back again. There are some good libraries for doing this to tables. I personally prefer to write valid lua code due to my fetish for code that writes code, but it's not really good practice :P
JamesWillson
Prole
Posts: 5
Joined: Sun May 12, 2013 6:41 am

Re: Can't make a file to save game data

Post by JamesWillson »

Thanks a lot!! Yeh, I'm new to the forums. I'll do that next time. :) Thanks again!! :D
Zeliarden
Party member
Posts: 139
Joined: Tue Feb 28, 2012 4:40 pm

Re: Can't make a file to save game data

Post by Zeliarden »

or use love filesystem... ;)

Code: Select all

--saves
love.filesystem.write( "save.lua", Tserial.pack(coins) )
-- loads
coins = Tserial.unpack( love.filesystem.read( "save.lua"  ) )
User avatar
MadByte
Party member
Posts: 533
Joined: Fri May 03, 2013 6:42 pm
Location: Braunschweig, Germany

Re: Can't make a file to save game data

Post by MadByte »

Zeliarden wrote:or use love filesystem... ;)

Code: Select all

--saves
love.filesystem.write( "save.lua", Tserial.pack(coins) )
-- loads
coins = Tserial.unpack( love.filesystem.read( "save.lua"  ) )
arw. :/ that easy ... But my attempt is good practice... ! :D
JamesWillson
Prole
Posts: 5
Joined: Sun May 12, 2013 6:41 am

Re: Can't make a file to save game data

Post by JamesWillson »

Thanks very much for all of your quick responses. This is great, however I'm still not sure where it is saving? Is it in the game directory or the love directory?
User avatar
MadByte
Party member
Posts: 533
Joined: Fri May 03, 2013 6:42 pm
Location: Braunschweig, Germany

Re: Can't make a file to save game data

Post by MadByte »

If you don't change anything then it will save in the %appdata% directory. ( windows )
i.e C:/users/user/appdata/LOVE/YourGameName/
JamesWillson
Prole
Posts: 5
Joined: Sun May 12, 2013 6:41 am

Re: Can't make a file to save game data

Post by JamesWillson »

And sorry if I'm bugging anyone, but when trying to say print the values in the table, how would I go about that? I've tried doing options.coins or options[1] am I way off? :P
User avatar
severedskullz
Prole
Posts: 36
Joined: Thu May 30, 2013 1:55 am

Re: Can't make a file to save game data

Post by severedskullz »

JamesWillson wrote:And sorry if I'm bugging anyone, but when trying to say print the values in the table, how would I go about that? I've tried doing options.coins or options[1] am I way off? :P
You would most likely want to use the "for pairs" loop. Assuming it is just a 1 level table. If its a multilevel table you would have to iterate through each sub-table.

Code: Select all

for k, v in pairs (YOUR_TABLE) do
	print("Key: " .. k .. " / Value: " .. v)
end
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 55 guests