Page 1 of 1

love.filesystem - I am either stupid or wrong

Posted: Tue Apr 21, 2009 4:16 am
by Xcmd
I just don't get how opening files and reading them works in Love. I've tried the following:

Code: Select all

function load()
	myFile = love.filesystem.newFile("data.txt")
	love.filesystem.open(myFile)
	myString = love.filesystem.lines(myFile)
end

function update()

end

function draw()
	love.graphics.draw(mySring, 50, 50)
end

But I get an error about over-loaded functions in Draw. All I want to be able to do is read lines from a file at-will and understand what I'm doing and why. I know the examples include a line iterator, but this assumes several things that aren't true:

1) I'm only reading from a single source and that I already know the name of the data file I'm trying to read.
2) I want to read every single line from the file all at once.
3) I want to store the read lines in an array.

None of that is true for my project. So what am I not understanding here? Where am I falling down?

Re: love.filesystem - I am either stupid or wrong

Posted: Tue Apr 21, 2009 5:40 am
by bartbes
You missed that love.filesystem.lines returns an iterator, just as the one in the lua io lib does.
This iterator is then used to return lines, small example:

Code: Select all

it = love.filesystem.lines(someFile)
line1 = it()
line2 = it()
So, you could still use lines in the following fashion:

Code: Select all

myString = ""
for line in love.filesystem.lines(myFile) do
    myString = myString .. line .. "\n"
end
Or, easier:

Code: Select all

myString = love.filesystem.read(myFile)
(where the last one even keeps line returns exactly the way they are)

Re: love.filesystem - I am either stupid or wrong

Posted: Tue Apr 21, 2009 9:50 am
by Xcmd
I thought it might be something like that, but I never used Lua's own file io system so I had no idea.

Re: love.filesystem - I am either stupid or wrong

Posted: Wed Apr 29, 2009 8:13 pm
by ramenudle
For what it's worth...
Xcmd wrote:

Code: Select all

function draw()
	love.graphics.draw(mySring, 50, 50)
end
You have mySring instead of myString...dunno if that makes a bit of difference, but I thought I'd point it out anyway

Re: love.filesystem - I am either stupid or wrong

Posted: Thu Apr 30, 2009 2:36 am
by appleide

But I get an error about over-loaded functions in Draw
happens when you put the wrong type of parameters into the draw function. e.g nil. (see above post).

Re: love.filesystem - I am either stupid or wrong

Posted: Sat May 02, 2009 1:15 am
by Xcmd
So you're trying to say I'm BOTH stupid AND wrong? Lovely.

Re: love.filesystem - I am either stupid or wrong

Posted: Sat May 02, 2009 8:18 am
by bartbes
You should ask you questions better, like: "Why doesn't this work?", this will leave you with a bigger ego in the end. (yes, we're mean)

Re: love.filesystem - I am either stupid or wrong

Posted: Tue Jun 02, 2009 7:33 am
by Xcmd
Long time, no update, right? Anyway. So I made the following piece of code:

Code: Select all

function load()
	textFile = "myData.txt"
	fileHandler = love.filesystem.lines(textFile)
	stringOut = fileHandler()
end

function update(dt)
	
end

function draw()
	love.graphics.draw(stringOut, 50, 50)
end
When I run it (on Linux: Ubuntu 9.04 x86), it gives me a blank screen but no errors. But when I screw something up in the file on purpose (doesn't matter what) and get the Love error screen, then fix it and hit "restart", I see the line that's read from my file just fine. Am... am I doing something wrong or is this a Linux hiccup?

Re: love.filesystem - I am either stupid or wrong

Posted: Tue Jun 02, 2009 12:41 pm
by bartbes
You haven't set a font, add love.graphics.setFont(love.default_font) to your load function. Reason it works after an error is that the error screen sets a font for it to use.

Re: love.filesystem - I am either stupid or wrong

Posted: Tue Jun 02, 2009 1:40 pm
by Xcmd
bartbes wrote:You haven't set a font, add love.graphics.setFont(love.default_font) to your load function. Reason it works after an error is that the error screen sets a font for it to use.
Ah. One day I really will listen to myself when I tell me not to program late at night.