Problems with a simple loop

General discussion about LÖVE, Lua, game development, puns, and unicorns.
yoyohoho
Prole
Posts: 14
Joined: Tue Apr 14, 2020 12:07 pm

Problems with a simple loop

Post 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. :crazy:
Andlac028
Party member
Posts: 174
Joined: Fri Dec 14, 2018 2:27 pm
Location: Slovakia

Re: Problems with a simple loop

Post 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
yoyohoho
Prole
Posts: 14
Joined: Tue Apr 14, 2020 12:07 pm

Re: Problems with a simple loop

Post 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?
User avatar
CogentInvalid
Prole
Posts: 27
Joined: Sat Dec 14, 2013 12:15 am

Re: Problems with a simple loop

Post 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.
yoyohoho
Prole
Posts: 14
Joined: Tue Apr 14, 2020 12:07 pm

Re: Problems with a simple loop

Post 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. :cool:
Attachments
Menu.lua
(2.35 KiB) Downloaded 204 times
User avatar
4vZEROv
Party member
Posts: 126
Joined: Wed Jan 02, 2019 8:44 pm

Re: Problems with a simple loop

Post by 4vZEROv »

In the code you provide self.dy is only set to TEXT_SPEED when the gameState is "dead", is it intended ?
User avatar
pgimeno
Party member
Posts: 3656
Joined: Sun Oct 18, 2015 2:58 pm

Re: Problems with a simple loop

Post 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.
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Problems with a simple loop

Post 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.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
yoyohoho
Prole
Posts: 14
Joined: Tue Apr 14, 2020 12:07 pm

Re: Problems with a simple loop

Post 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.
yoyohoho
Prole
Posts: 14
Joined: Tue Apr 14, 2020 12:07 pm

Re: Problems with a simple loop

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

Who is online

Users browsing this forum: No registered users and 3 guests