love.filesystem - I am either stupid or wrong

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
Xcmd
Party member
Posts: 211
Joined: Fri Feb 13, 2009 10:45 pm

love.filesystem - I am either stupid or wrong

Post 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?
We don't borrow, we don't read, we don't rent, we don't lease, we take the minds!
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

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

Post 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)
User avatar
Xcmd
Party member
Posts: 211
Joined: Fri Feb 13, 2009 10:45 pm

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

Post 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.
We don't borrow, we don't read, we don't rent, we don't lease, we take the minds!
ramenudle
Prole
Posts: 1
Joined: Sat Apr 11, 2009 4:29 am
Location: Lorton, VA

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

Post 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
User avatar
appleide
Party member
Posts: 323
Joined: Fri Jun 27, 2008 2:50 pm

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

Post 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).
User avatar
Xcmd
Party member
Posts: 211
Joined: Fri Feb 13, 2009 10:45 pm

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

Post by Xcmd »

So you're trying to say I'm BOTH stupid AND wrong? Lovely.
We don't borrow, we don't read, we don't rent, we don't lease, we take the minds!
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

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

Post 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)
User avatar
Xcmd
Party member
Posts: 211
Joined: Fri Feb 13, 2009 10:45 pm

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

Post 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?
We don't borrow, we don't read, we don't rent, we don't lease, we take the minds!
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

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

Post 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.
User avatar
Xcmd
Party member
Posts: 211
Joined: Fri Feb 13, 2009 10:45 pm

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

Post 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.
We don't borrow, we don't read, we don't rent, we don't lease, we take the minds!
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests