[Help] hump.timer executing functions without waiting

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.
Post Reply
grimFandango
Prole
Posts: 3
Joined: Wed May 27, 2015 11:56 pm

[Help] hump.timer executing functions without waiting

Post 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!
Attachments
buttonBashers.love
(2.66 MiB) Downloaded 150 times
bobbyjones
Party member
Posts: 730
Joined: Sat Apr 26, 2014 7:46 pm

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

Post 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
bobbyjones
Party member
Posts: 730
Joined: Sat Apr 26, 2014 7:46 pm

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

Post 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.
grimFandango
Prole
Posts: 3
Joined: Wed May 27, 2015 11:56 pm

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

Post 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:
bobbyjones
Party member
Posts: 730
Joined: Sat Apr 26, 2014 7:46 pm

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

Post by bobbyjones »

You can use a boolean to make sure you set it once. Then just reset the boolean in the timer.
grimFandango
Prole
Posts: 3
Joined: Wed May 27, 2015 11:56 pm

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

Post 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:
User avatar
Garmelon
Prole
Posts: 19
Joined: Tue Jun 02, 2015 5:25 pm

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

Post 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
Attachments
Button.love
(4.05 KiB) Downloaded 151 times
Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests