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.
Can't make a file to save game data
-
- Prole
- Posts: 5
- Joined: Sun May 12, 2013 6:41 am
Re: Can't make a file to save game data
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.
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
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
My game called Hat Cat and the Obvious Crimes Against the Fundamental Laws of Physics is out now!
-
- Prole
- Posts: 5
- Joined: Sun May 12, 2013 6:41 am
Re: Can't make a file to save game data
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
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
arw. :/ that easy ... But my attempt is good practice... !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" ) )
-
- Prole
- Posts: 5
- Joined: Sun May 12, 2013 6:41 am
Re: Can't make a file to save game data
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
If you don't change anything then it will save in the %appdata% directory. ( windows )
i.e C:/users/user/appdata/LOVE/YourGameName/
i.e C:/users/user/appdata/LOVE/YourGameName/
-
- Prole
- Posts: 5
- Joined: Sun May 12, 2013 6:41 am
Re: Can't make a file to save game data
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?
- severedskullz
- Prole
- Posts: 36
- Joined: Thu May 30, 2013 1:55 am
Re: Can't make a file to save game data
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.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?
Code: Select all
for k, v in pairs (YOUR_TABLE) do
print("Key: " .. k .. " / Value: " .. v)
end
Who is online
Users browsing this forum: Google [Bot] and 4 guests