Page 1 of 1
Lua/Love IO Problem
Posted: Tue Dec 21, 2010 8:30 pm
by MV
Very simple problem,
Code: Select all
local f = assert(io.open("Test.txt", "r"))
print(f:read("*all"))
f:close()
f = assert(io.open("Test.txt", "w"))
f:write("New Stuff")
f:close()
My project includes some rudimentary unlockables and highscores that I want to save in a text file, and modify whenever I please.
I can read information from a text file easily, but when I overwrite the same file it does something weird.
The file in the working directory stays the same, unchanged, but the program is now reading the data of which I saved previously. I tried this using both the Love filesystem and Lua's but both have the same problem.
How do I make it modify the text file in the directory directly?
Sorry, but I'm new to both Lua and Love and I couldn't find a similar problem anywhere.
Re: Lua/Love IO Problem
Posted: Tue Dec 21, 2010 8:43 pm
by tentus
This is a slight variation of what I use in my game to save settings, player scores, etc. prepSaveData() does exactly what it sounds like: prepare data to be saved. This code assumes that what you are writing can utterly replace what currently exists.
Code: Select all
function saveStuff()
local saveData = prepSaveData()
file = love.filesystem.newFile("save.txt")
file:open('w')
file:write(saveData)
file:close()
end
Re: Lua/Love IO Problem
Posted: Tue Dec 21, 2010 8:44 pm
by Robin
- Use love.filesystem. Period. That io stuff is still in LÖVE, but please don't use it. In fact, in SELÖVE it would be an error.
- Games never change files in the working directory, because it might not be writeable (because it could be a .love or a in folder you can't write to). Enter the write directory (which is explained in the wiki entry I linked to at the top). You will find the changed file there. (Don't forget to set the game identity first.)
Unfortunately, as I said, it is not possible to change files in the game directory, but the write directory is handled transparently, so in the end it all works out.
Re: Lua/Love IO Problem
Posted: Tue Dec 21, 2010 9:20 pm
by MV
Thanks, that clears things up.
The Wiki says that I'm not allowed to set the location of the save directory. This seems somewhat inconvenient but I'll figure a way around it.
Re: Lua/Love IO Problem
Posted: Tue Dec 21, 2010 9:45 pm
by tentus
You sort of are:
Code: Select all
love.filesystem.setIdentity("yourgame")
This lets you control the name of your folder, inside of the Love folder. It's not as nice as being in the same folder as the game itself, but it works ok.
Re: Lua/Love IO Problem
Posted: Tue Dec 21, 2010 11:06 pm
by TechnoCat
I made a very rudimentary example of how to save files and load files here
https://bitbucket.org/dannyfritz/pindie ... /SaveLoad/
I'm setting the save direction in the conf.lua though, not by using setIdentity. However, conf.lua probably uses setIdentity internally. Although I'm pretty sure for most cases, setting t.identity in conf.lua is the proper way to do it.