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

Re: Problems with a simple loop

Post by yoyohoho »

4vZEROv wrote: Tue Apr 14, 2020 9:50 pm In the code you provide self.dy is only set to TEXT_SPEED when the gameState is "dead", is it intended ?
My thinking was that I might want TEXT_SPEED to be different (so I should declare it as a variable instead of a constant) depending on the gameState. So if I'm alive I want menu-stuff to show up slower or faster. Admittedly, it wasn't really thought through though as I just wanted my code to function to begin with.
User avatar
pgimeno
Party member
Posts: 3640
Joined: Sun Oct 18, 2015 2:58 pm

Re: Problems with a simple loop

Post by pgimeno »

yoyohoho wrote: Wed Apr 15, 2020 9:03 am 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?
The second part is correct. I'm not sure what you mean in the first part.

Anyway, Löve executes events in this order:

- First all input events (love.keypressed, love.mousemoved, etc.) presumably in the order they are generated.
- then the love.update event,
- then the love.draw event.

Then it repeats the sequence, 60 times a second (see (*) note above). The gory details are in love.run.

This means that if your render function is called from love.draw, it will be executed after the update function, assuming the update function is called from love.update.

So, the sequence will be:

- Process inputs if any
- Increment y if it is < 80 (per the update function)
- Display (per the render function)
- Start again

This sounds correct for your intended effect. Note however that the first time that the update event is called, the variable dy does not have the value that the render function sets it to. I'm not sure why you are using that, but then I haven't dug into what your code is doing.

Anyway, here's a proof of concept of that very same method, but used in a stand-alone program, demonstrating that it works:

Code: Select all

local y = 0
local gameOver = -50
local dy = 40

love.graphics.setNewFont(50)

function love.update(dt)
    if gameOver < 80 then
        gameOver = gameOver + dy * dt
    end
end

function love.draw()
    love.graphics.print("Game Over", 0, gameOver)
end
yoyohoho wrote: Wed Apr 15, 2020 9:03 am I'm going to find a debug tool so I can go through the code step by step.
It's not easy to debug Löve programs. One problem is that you can't see the stuff that is being drawn until the frame ends. Another obstacle is that tracing through love.run is not possible because the debugger lacks the source code of boot.lua (which is embedded in the Löve executable, but the debugger doesn't know how to find it). So you may need to place breakpoints at events. Another obstacle is that while running the debugger, the Löve code gets horribly slowed down (someone mentioned ~1fps recently). But for the concept, it may help.
yoyohoho
Prole
Posts: 14
Joined: Tue Apr 14, 2020 12:07 pm

Re: Problems with a simple loop

Post by yoyohoho »

pgimeno wrote: Wed Apr 15, 2020 1:47 pm
yoyohoho wrote: Wed Apr 15, 2020 9:03 am 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?
The second part is correct. I'm not sure what you mean in the first part.

Anyway, Löve executes events in this order:

- First all input events (love.keypressed, love.mousemoved, etc.) presumably in the order they are generated.
- then the love.update event,
- then the love.draw event.

Then it repeats the sequence, 60 times a second (see (*) note above). The gory details are in love.run.

This means that if your render function is called from love.draw, it will be executed after the update function, assuming the update function is called from love.update.

So, the sequence will be:

- Process inputs if any
- Increment y if it is < 80 (per the update function)
- Display (per the render function)
- Start again

This sounds correct for your intended effect. Note however that the first time that the update event is called, the variable dy does not have the value that the render function sets it to. I'm not sure why you are using that, but then I haven't dug into what your code is doing.

Anyway, here's a proof of concept of that very same method, but used in a stand-alone program, demonstrating that it works:

Code: Select all

local y = 0
local gameOver = -50
local dy = 40

love.graphics.setNewFont(50)

function love.update(dt)
    if gameOver < 80 then
        gameOver = gameOver + dy * dt
    end
end

function love.draw()
    love.graphics.print("Game Over", 0, gameOver)
end
yoyohoho wrote: Wed Apr 15, 2020 9:03 am I'm going to find a debug tool so I can go through the code step by step.
It's not easy to debug Löve programs. One problem is that you can't see the stuff that is being drawn until the frame ends. Another obstacle is that tracing through love.run is not possible because the debugger lacks the source code of boot.lua (which is embedded in the Löve executable, but the debugger doesn't know how to find it). So you may need to place breakpoints at events. Another obstacle is that while running the debugger, the Löve code gets horribly slowed down (someone mentioned ~1fps recently). But for the concept, it may help.
Hey man, thanks a ton for all of the information. Much appreciated as I finally got my text to work! :D Thank you again and have a good one!
Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests