sanjiv wrote:
but I haven't been using "love.filesystem.write" correctly. A literal example would help. To avoid confusion, might an example be given using the following details?
file to store and load info from
boxList.txt --or should this be a .lua file?
info
listOfBoxes={
{a1,b1,c1,d1},
{a2,b2,c2,d2},
{a3,b3,c3,d3}
}
how info will be used in game
drawBoxes(listOfBoxes) --some function that will draw 3 boxes using the 3 lists in listOfBoxes
updateBoxes(listOfBoxes) --some other funtion might make them move or something
Well,
love.filesystem.write actually writes contents into a file...But it is done through a protected way. Remember,
love.filesystem can access to specific places and only them.
To have it working properly, first set the folder using
love.filesystem.setIdentity. You can call this function in love.load callback.
Code: Select all
love.filesystem.setIdentity("MyGame")
This will create a
folder named "
MyGame" in you user folder. Depending on the OS, that folder will have different pathes:
Windows XP: C:\Documents and Settings\user\Application Data\Love\ or %appdata%\Love\MyGame
Windows Vista and 7: C:\Users\user\AppData\Roaming\LOVE\MyGame or %appdata%\Love\MyGame
Linux: $XDG_DATA_HOME/love/MyGame or ~/.local/share/love/MyGame
mac: /Users/user/Library/Application Support/LOVE/MyGame
The you can write/read anything inside this folder using
love.filesystem.write or
love.filesystem.read.
Now, concerning data output , i'd rather recommend to tweak a bit
TheBurrito's code. Simply iterates using ipairs.
Code: Select all
local function save(myTable)
local str = "{"
for k,v in ipairs(myTable) do
if type(v)=="table" then str = str..save(v)..','
else str = str..v..","
end
end
return str.."}"
end
This assumes that keys in table are
alwaysnumeric ... It seems to be the case.
Although it would have be simple to use
TSerial library. You would have get the same results no matter what the input table was.
Using it should be quite simple.File extension doesn't matter, Lua will just execute the piece of code inside. But I'll prefer saving data a way one can load it back using return, instead of setting it as var in the global environment. It is more safe, to me...So
Code: Select all
function love.load()
love.filesystem.setIdentity("myGame/")
--bla bla bla
end
function love.draw()
-- bla bla bla
--saving
love.filesystem.write('data.txt', 'return '..save(listOfBoxes),'all')
---bla bla bla
-- loading back
local listOfBoxes = love.filesystem.load('data.txt')()
--- bla bla bla
end
PS: Ima party member now... Walkin' on the street with my new lafreak