How to use (file):lines()?

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
User avatar
Kingdaro
Party member
Posts: 395
Joined: Sun Jul 18, 2010 3:08 am

How to use (file):lines()?

Post 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.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

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

Post by bartbes »

Code: Select all

for line in file:lines() do
    print(line)
end
prints all lines in the file file.
User avatar
Kingdaro
Party member
Posts: 395
Joined: Sun Jul 18, 2010 3:08 am

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

Post 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 >.<
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

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

Post 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.
Help us help you: attach a .love.
User avatar
Kingdaro
Party member
Posts: 395
Joined: Sun Jul 18, 2010 3:08 am

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

Post 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
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

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

Post by Robin »

OK, then what is the problem?
Help us help you: attach a .love.
User avatar
Kingdaro
Party member
Posts: 395
Joined: Sun Jul 18, 2010 3:08 am

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

Post 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?
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

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

Post 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
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 57 guests