Page 1 of 1

How to use (file):lines()?

Posted: Sun Jul 18, 2010 7:45 pm
by Kingdaro
I went to the wiki to learn how to read a file line by line, but I came across this:

iterator = File:lines()

returns function(?) iterator

I'm sitting here confused wondering what the heck this means. How would you go through all the lines in the file using this err...function? I've never really worked with functions that return functions, so help would be appreciated, thanks.

Re: How to use (file):lines()?

Posted: Sun Jul 18, 2010 7:54 pm
by bartbes

Code: Select all

for line in file:lines() do
    print(line)
end
prints all lines in the file file.

Re: How to use (file):lines()?

Posted: Sun Jul 18, 2010 8:06 pm
by Kingdaro
bartbes wrote:

Code: Select all

for line in file:lines() do
    print(line)
end
prints all lines in the file file.
If there was a rep system, I'd rep you. Thank you!

But how do I make it so it only does it ONCE? It keeps doing it over and over >.<

Re: How to use (file):lines()?

Posted: Sun Jul 18, 2010 8:40 pm
by Robin
Kingdaro wrote:But how do I make it so it only does it ONCE? It keeps doing it over and over >.<
Don't put it in love.update(), but put it in love.load().

EDIT: love.update() is called once per frame.

Re: How to use (file):lines()?

Posted: Sun Jul 18, 2010 8:43 pm
by Kingdaro
I did, technecally, put it in love.load. Here's what I did, in reality.

Code: Select all

function love.load()
file = love.filesystem.newFile("song.txt")
file:open("r")
generate()
end

function generate()
for i in file:lines() do
for v=1, 4 do
if string.sub(i,v,v) == "1" then
body = love.physics.newBody(world,(v-1)*64,0,0,0)
notes[#notes+1] = body
end
end
end
end

Re: How to use (file):lines()?

Posted: Sun Jul 18, 2010 9:26 pm
by Robin
OK, then what is the problem?

Re: How to use (file):lines()?

Posted: Sun Jul 18, 2010 10:11 pm
by Kingdaro
Robin wrote:OK, then what is the problem?
I send the command once, it fires it repeatedly.

EDIT: Never mind, fixed. But how do I make it wait, say half a second, between line readings?

Re: How to use (file):lines()?

Posted: Mon Jul 19, 2010 9:59 am
by Robin
Kingdaro wrote:EDIT: Never mind, fixed. But how do I make it wait, say half a second, between line readings?
Then you need to do... well, the easiest way to do that would be to load the file before, and just change a variable each half a second:

Code: Select all

function love.load()
   -- file:lines() -> you put them in table Lines
   lineIndex = 0
   timeX = 0
end

function love.update(dt)
    timeX = timeX + dt
    if timeX > .5 then
        timeX = 0
        lineIndex = lineIndex + 1
    end
end