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

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Kingdaro
Party member
Posts: 395
Joined: Sun Jul 18, 2010 3:08 am

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

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.

bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

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

Code: Select all

for line in file:lines() do
print(line)
end

prints all lines in the file file.

Kingdaro
Party member
Posts: 395
Joined: Sun Jul 18, 2010 3:08 am

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

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

Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

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

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.

Kingdaro
Party member
Posts: 395
Joined: Sun Jul 18, 2010 3:08 am

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

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


Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

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

OK, then what is the problem?

Kingdaro
Party member
Posts: 395
Joined: Sun Jul 18, 2010 3:08 am

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

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?

Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

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

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