while and repeat loops still cause errors

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.
Raskol'nikov
Prole
Posts: 9
Joined: Sun Feb 28, 2021 1:48 pm

while and repeat loops still cause errors

Post 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?
Attachments
problems.jpg
problems.jpg (51.94 KiB) Viewed 4344 times
User avatar
4vZEROv
Party member
Posts: 126
Joined: Wed Jan 02, 2019 8:44 pm

Re: while and repeat loops still cause errors

Post 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
Raskol'nikov
Prole
Posts: 9
Joined: Sun Feb 28, 2021 1:48 pm

Re: while and repeat loops still cause errors

Post 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.
User avatar
pgimeno
Party member
Posts: 3550
Joined: Sun Oct 18, 2015 2:58 pm

Re: while and repeat loops still cause errors

Post 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.
Last edited by pgimeno on Mon Mar 01, 2021 6:07 pm, edited 1 time in total.
Raskol'nikov
Prole
Posts: 9
Joined: Sun Feb 28, 2021 1:48 pm

Re: while and repeat loops still cause errors

Post 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?
User avatar
pgimeno
Party member
Posts: 3550
Joined: Sun Oct 18, 2015 2:58 pm

Re: while and repeat loops still cause errors

Post 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.
Raskol'nikov
Prole
Posts: 9
Joined: Sun Feb 28, 2021 1:48 pm

Re: while and repeat loops still cause errors

Post by Raskol'nikov »

So i was right, i finally got it. I just should not use
while and repeat loops
in love.update()
User avatar
pgimeno
Party member
Posts: 3550
Joined: Sun Oct 18, 2015 2:58 pm

Re: while and repeat loops still cause errors

Post 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
Raskol'nikov
Prole
Posts: 9
Joined: Sun Feb 28, 2021 1:48 pm

Re: while and repeat loops still cause errors

Post by Raskol'nikov »

Thanks!
User avatar
togFox
Party member
Posts: 779
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Re: while and repeat loops still cause errors

Post by togFox »

I use REPEAT UNTIL in love.update for some key strokes (PRESS SPACE TO CONTINUE) but you do have to be careful.
Current project:
https://togfox.itch.io/backyard-gridiron-manager
American football manager/sim game - build and manage a roster and win season after season
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 198 guests