Page 1 of 2
while and repeat loops still cause errors
Posted: Mon Mar 01, 2021 5:45 pm
by Raskol'nikov
This time I wrote a simple little code but problems still occure
Code: Select all
function love.load()
g = 0
end
function love.update()
repeat g = g + 1
function love.draw()
love.graphics.print(g, 100, 100)
end
until g >= 1000
end
When i start the program it prints the g scaling but starting from 1000.
if i change 'g >=1000' to 'g == 1000' the game crashes
is the problem in my pc or while and repeat loops work differently in LOVE?
Re: while and repeat loops still cause errors
Posted: Mon Mar 01, 2021 5:53 pm
by 4vZEROv
Code: Select all
function love.load()
g = 0
end
function love.update()
repeat g = g + 1
until g >= 1000
end
function love.draw()
love.graphics.print(g, 100, 100)
end
Re: while and repeat loops still cause errors
Posted: Mon Mar 01, 2021 5:56 pm
by Raskol'nikov
i thought of one idea: the 'g = g + 1' occurs every time the love.update() does and only then it sees the 'until' so it is adding 1 to g endlessly but in that case i have even more questions.
Re: while and repeat loops still cause errors
Posted: Mon Mar 01, 2021 5:57 pm
by pgimeno
Functions, and in particular love.draw and other events, work differently in Löve than what you seem to think.
You are defining love.draw inside the loop, and you are doing it 1000 times. But defining it 1000 times doesn't mean it will be called 1000 times. You only need to define it once, and you usually don't do that inside another event.
Löve will automatically loop endlessly and you don't need to define your own loop for that to happen; it will first read the inputs, then call love.update if you have defined it, then clear the screen and call love.draw if you have defined it, and then start again, in an endless loop until you close the window or otherwise force it to terminate.
So, what you need to do is define the events in such a way that they do what you expect your program to do. In your case, you seem to want to print a number that increases from 1 to 1000 and then stays in 1000. Here is one way to do that:
Code: Select all
function love.load()
g = 0
end
function love.update(dt)
if g < 1000 then
g = g + 1
end
end
function love.draw()
love.graphics.print(g, 100, 100)
end
This will print every frame a different number, until 1000 frames are displayed, in which case the counter will never be increased again.
Re: while and repeat loops still cause errors
Posted: Mon Mar 01, 2021 6:03 pm
by Raskol'nikov
pgimeno wrote: ↑Mon Mar 01, 2021 5:57 pm
Functions, and in particular love.draw and other events, work differently in Löve than what you seem to think.
You are defining love.draw inside the loop, and you are doing it 1000 times. But defining it 1000 times doesn't mean it will be called 1000 times. You only need to define it once, and you usually don't do that inside another event.
Löve will automatically loop endlessly and you don't need to define your own loop for that to happen; it will first read the inputs, then call love.update if you have defined it, then clear the screen and call love.draw if you have defined it, and then start again, in an endless loop until you close the window or otherwise force it to terminate.
So, what you need to do is define the events in such a way that they do what you expect your program to do. In your case, you seem to want to print a number that increases from 1 to 1000 and then stays in 1000. Here is how you do that:
Code: Select all
function love.load()
g = 0
end
function love.update(dt)
if g < 1000 then
g = g + 1
end
end
function love.draw()
love.graphics.print(g, 100, 100)
end
This will print every frame a different number, until 1000 frames are displayed, in which case the counter will never be increased again.
But why I can't use while and repeat loops for that?
Re: while and repeat loops still cause errors
Posted: Mon Mar 01, 2021 6:10 pm
by pgimeno
You can, but if you want your program to keep reading inputs and responding to the OS, you need to use Löve's ways.
For example, this will do something similar to what you expect:
Code: Select all
function love.load()
g = 0
repeat
g = g + 1
love.graphics.clear()
love.graphics.print(g, 100, 100)
love.graphics.present()
until g >= 1000
end
-- once the loop finishes, love.draw will have a chance to run,
-- and then it will display the last value of g:
function love.draw()
love.graphics.print(g, 100, 100)
end
However, while it loops, your program will not respond to things like closing the window. You need to let Löve read and process the inputs every frame.
Re: while and repeat loops still cause errors
Posted: Mon Mar 01, 2021 6:20 pm
by Raskol'nikov
So i was right, i finally got it. I just should not use
while and repeat loops
in love.update()
Re: while and repeat loops still cause errors
Posted: Mon Mar 01, 2021 6:26 pm
by pgimeno
You don't use them for displaying different frames, for sure. They have other uses, but they have to complete within one frame.
Nevertheless, I have written a tool that allows you to use 'for' loops and draw to the screen in a way similar to how you do it in some other languages, and more similar to how you seemed to expect things would work.
https://love2d.org/forums/viewtopic.php?f=5&t=87262
Re: while and repeat loops still cause errors
Posted: Mon Mar 01, 2021 6:44 pm
by Raskol'nikov
Thanks!
Re: while and repeat loops still cause errors
Posted: Tue Mar 02, 2021 10:45 am
by togFox
I use REPEAT UNTIL in love.update for some key strokes (PRESS SPACE TO CONTINUE) but you do have to be careful.