Page 1 of 2
Problems with a simple loop
Posted: Tue Apr 14, 2020 12:20 pm
by yoyohoho
Hello people!
This will be my first time posting on this forum, so if I've missed anything just let me know.
I am very new to LÖVE and lua, but I'm getting the grasp of it pretty quickly.
I am trying to print a 'GAME OVER' text that comes in from the top of the screen and then stops at the middle of screen.
This is my proposed code:
Code: Select all
TEXT_SPEED = 1
function render()
while y < 80 do
love.graphics.print("GAMEOVER", middleOfXAxis, y)
dy = TEXT_SPEED
end
end
function update(dt)
y = y + dy * dt
end
This results in my game crashing, probably because of an endless loop, but I can't see why as the *y* should reach 80 pretty fast and then just stop, right?
I hope this makes sense. Obviously this isn't all of my code and I've changed some of the variables, but I hope you get the gist of it.
Re: Problems with a simple loop
Posted: Tue Apr 14, 2020 2:28 pm
by Andlac028
Hi and welcome to the forums,
in function render, you are using while loop and it don't have the end, because of y is not changing in time - its still the same value = its still < 80. Try to replace it with if y<80 do ... end
Re: Problems with a simple loop
Posted: Tue Apr 14, 2020 2:52 pm
by yoyohoho
Thank you very much.
I understand, but if it's not in a loop it doesn't iterate back over the if-statement, does it?
EDIT: While glancing i realized that surely y should change in the update function?
Re: Problems with a simple loop
Posted: Tue Apr 14, 2020 3:18 pm
by CogentInvalid
The update and render functions don't run at the same time - the render function has to finish for update to be called, and vice versa. Because render contains an infinite loop, the game freezes and never updates again.
To fix it, you can change the while loop to an if statement and put that if statement around the increment in the update function. Update gets called every frame, so y will increment every frame, until y is greater than 80, at which point the contents of the if statement will stop running.
Re: Problems with a simple loop
Posted: Tue Apr 14, 2020 7:02 pm
by yoyohoho
Hi, thank you for your reply.
I understand the first bit, but I'm a little unsure about the second. So I changed the
while statement to an
if statement and moved it into the update function as such:
Code: Select all
TEXT_SPEED = 1
function render()
love.graphics.print("GAMEOVER", middleOfXAxis, y)
dy = TEXT_SPEED
end
function update(dt)
if y < 80 then
y = y + dy * dt
end
end
y is set to 0 by the way. However, while it does set
dy to 1 (obviously) it does not update
y and stays at 0. While looking at it I see why
y doesn't update as GAME OVER has already been printed. Did I miss anything specific? I've included my spaghetti code below but I think the example encompasses what I mean just fine.
Re: Problems with a simple loop
Posted: Tue Apr 14, 2020 9:50 pm
by 4vZEROv
In the code you provide self.dy is only set to TEXT_SPEED when the gameState is "dead", is it intended ?
Re: Problems with a simple loop
Posted: Tue Apr 14, 2020 11:40 pm
by pgimeno
Events are not multi-tasking, therefore if execution enters an event, no other event will execute until it finishes; that's why the OP's code gets stuck in an infinite loop. What you do instead is do one step and then return and wait for the next frame to do the next step. Remember that Löve will execute your events in sequence 60 times a second(*), so you have to plan your code with that in mind.
In the code there's this fragment:
Code: Select all
if gameOver < 80 then
gameover = gameover + self.dy * dt
end
Note you use gameOver (camelCase) first, then gameover (lowercase).
⎽⎽⎽⎽⎽
(*) or whatever your monitor's frame rate is, assuming you have VSync on.
Re: Problems with a simple loop
Posted: Wed Apr 15, 2020 4:39 am
by zorg
We're also assuming that the render and update functions are in fact named love.draw and love.update, or at least are called from those functions.
Re: Problems with a simple loop
Posted: Wed Apr 15, 2020 9:03 am
by yoyohoho
pgimeno wrote: ↑Tue Apr 14, 2020 11:40 pm
Events are not multi-tasking, therefore if execution enters an event, no other event will execute until it finishes; that's why the OP's code gets stuck in an infinite loop. What you do instead is do one step and then return and wait for the next frame to do the next step. Remember that Löve will execute your events in sequence 60 times a second(*), so you have to plan your code with that in mind.
In the code there's this fragment:
Code: Select all
if gameOver < 80 then
gameover = gameover + self.dy * dt
end
Note you use gameOver (camelCase) first, then gameover (lowercase).
⎽⎽⎽⎽⎽
(*) or whatever your monitor's frame rate is, assuming you have VSync on.
Awesome! Thank you so much. Does that also mean that once execution finishes the
render function it moves on to the
update function where it finishes the
if statement until
gameOver is >= 80 or does it only increment
gameOver once and then not again until execution goes through the code again the next frame?
I'm going to find a debug tool so I can go through the code step by step.
Re: Problems with a simple loop
Posted: Wed Apr 15, 2020 9:05 am
by yoyohoho
zorg wrote: ↑Wed Apr 15, 2020 4:39 am
We're also assuming that the render and update functions are in fact named love.draw and love.update, or at least are called from those functions.
Thank you for the reply.
That makes a lot of sense. I call the functions in my
main.lua file.