Lua/Love IO Problem

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
MV
Prole
Posts: 2
Joined: Tue Dec 21, 2010 8:12 pm

Lua/Love IO Problem

Post 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.
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Lua/Love IO Problem

Post 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
Last edited by tentus on Tue Dec 21, 2010 8:45 pm, edited 1 time in total.
Kurosuke needs beta testers
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Lua/Love IO Problem

Post by Robin »

  1. 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.
  2. 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.
Help us help you: attach a .love.
MV
Prole
Posts: 2
Joined: Tue Dec 21, 2010 8:12 pm

Re: Lua/Love IO Problem

Post 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.
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Lua/Love IO Problem

Post 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.
Kurosuke needs beta testers
User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Denver, CO
Contact:

Re: Lua/Love IO Problem

Post 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.
Post Reply

Who is online

Users browsing this forum: No registered users and 57 guests