Page 1 of 1

[Help] hump.timer executing functions without waiting

Posted: Thu May 28, 2015 12:57 am
by grimFandango
Hey, good day!

I just registered today, but I've been fiddling around with LÖVE and Lua this past week because I wanted to deliver an assignment using LÖVE, but I just found a dead end today.

A little background:
In a nutshell, I'm making a mini game for two players where P1 uses "Z" and P2 uses "M", and the one that presses their key the most wins the round, each player has three lifes. Each round is five seconds long, and after that, results get displayed for two seconds, and then it repeats until one of the players has 0 lifes.

One of the libraries I'm using is HUMP.Timer, but when the first timed function works, the one right after gets looped in delta time and stuck deleting lifes.

My pseudocode is something like this, I tried my best at making it short since the original code is in spanish at some parts:

Code: Select all

function love.update(dt)
	if gamestate == "fight" then
		checkLifes(p1Lifes,p2Lifes)
		love.keypressed()
	end
end


function love.draw()
	if gamestate == "fight" then
		drawthegui()
		Timer.add(5, function() gamestate = "results" end)
	elseif gamestate == "results" then
		drawthegui()
		winner = getWinner(p1Counter,p2Counter)
		love.graphics.print (winner,300,300)
		Timer.add(2, function() gamestate = "fight" end)
	end
end

function love.keypressed(key)
	if gamestate == "fight" then
		if key == "z"
			p1counter = p1counter + 1
		elseif key == "m"
			p2counter = p2counter + 1
		elseif key == "q" then
			love.event.push("quit")
		end
	end
end


function getWinner(p1Counter,p2Counter)
	if p1Counter > p2Counter then
		winner  = "p1"
		return winner
	elseif p2Counter > p1Counter then
		winner = "p2"
		return winner
	elseif p1Counter == p2Counter
		winner = "tie"
		return winner
	end
end
Any comment would be really helpful! I attached the .love as well but bear in mind my code is really messy and some of the functions/texts are in spanish!

Re: [Help] hump.timer executing functions without waiting

Posted: Thu May 28, 2015 7:51 pm
by bobbyjones
Ah grim fandango. I know off topic but your fan of those kind of games? I just wanted to point out because I finally recognize a game title. Lol

Re: [Help] hump.timer executing functions without waiting

Posted: Thu May 28, 2015 7:56 pm
by bobbyjones
Also your issue is that your adding a function to the timer every frame. Your goal is to do that once. So since you are adding most likely 60 functions to the timer a second it is causing the lives to run out in succesion. So as soon as you lose one you would most likely lose them all.

Re: [Help] hump.timer executing functions without waiting

Posted: Sun May 31, 2015 4:19 am
by grimFandango
bobbyjones wrote:Also your issue is that your adding a function to the timer every frame. Your goal is to do that once. So since you are adding most likely 60 functions to the timer a second it is causing the lives to run out in succesion. So as soon as you lose one you would most likely lose them all.
I was reading my code earlier today and you're right! What would be the best solution to fix it? Making a method where I call the timer functions there, and then call that method on the love.draw section?

Thanks for your response!


Also yeah, it's been a while since I've played it but yeah my nickname is because of that game! :awesome:

Re: [Help] hump.timer executing functions without waiting

Posted: Sun May 31, 2015 4:54 pm
by bobbyjones
You can use a boolean to make sure you set it once. Then just reset the boolean in the timer.

Re: [Help] hump.timer executing functions without waiting

Posted: Wed Jun 03, 2015 10:28 pm
by grimFandango
I've been trying to fix the issue but with no success and I can't find more HUMP.timer examples on the web. :death:

Re: [Help] hump.timer executing functions without waiting

Posted: Thu Jun 04, 2015 9:28 am
by Garmelon
Did you try something like bobby said?

Code: Select all

function love.draw()
   if gamestate == "fight" then
      drawthegui()
      if not timerset do
         Timer.add(5, function() gamestate = "results" timerset = false end)
      end
      timerset = true
   elseif gamestate == "results" then
      drawthegui()
      winner = getWinner(p1Counter,p2Counter)
      love.graphics.print (winner,300,300)
      if not timerset then
         Timer.add(2, function() gamestate = "fight" timerset = false end)
      end
      timerset = true
   end
end
Btw, I made a game like this myself, I didn't use timers though, and I used one game state for both the countdown and the actual playing state.
I'm not a designer so don't expect it to look any good. Or not confusing. xD