Page 1 of 1
Can't make a file to save game data
Posted: Sun Jun 02, 2013 7:56 am
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.
Re: Can't make a file to save game data
Posted: Sun Jun 02, 2013 8:39 am
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
Re: Can't make a file to save game data
Posted: Sun Jun 02, 2013 8:41 am
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
Re: Can't make a file to save game data
Posted: Sun Jun 02, 2013 8:42 am
by JamesWillson
Thanks a lot!! Yeh, I'm new to the forums. I'll do that next time.
Thanks again!!
Re: Can't make a file to save game data
Posted: Sun Jun 02, 2013 8:44 am
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" ) )
Re: Can't make a file to save game data
Posted: Sun Jun 02, 2013 8:49 am
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... !
Re: Can't make a file to save game data
Posted: Sun Jun 02, 2013 8:50 am
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?
Re: Can't make a file to save game data
Posted: Sun Jun 02, 2013 8:57 am
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/
Re: Can't make a file to save game data
Posted: Sun Jun 02, 2013 11:15 am
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?
Re: Can't make a file to save game data
Posted: Sun Jun 02, 2013 2:16 pm
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?
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