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?
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
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
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?
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?
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.