Is there a way to Create and show a Timer?

I am trying to display a timer in game that runs non-stop until the game ends. And then display your final time. Is this possible?

Sorry, trying to make a game for the first time ever and literally know nothing. :slight_smile:

There are likely dozens of ways to pull this off. Maybe one simple one would be to use getCurrentTimeMilliseconds to get simply how long the game has been open for. Otherwise, maybe using a combo of resetElapsedTime when a new level is started and getElapsedTime to read.

You could then use that each frame to display the value on screen during the level, or just grab it at the end of the game to display a final time.

1 Like

Thanks for responding. I will give this a shot.

I haven't had any luck getting this to work. Not sure what i am doing wrong.

Try this out, with some simple math from here you can likely get to what you're looking for.

-- -- -- -- CONSTANTS -- -- -- -- 
local gfx <const> = playdate.graphics
local currentMilliseconds
local elapsedTime
local lapTime = 0

-- -- -- -- UPDATE -- -- -- -- 
function playdate.update()
	gfx.fillRect(0, 0, 400, 240)
	
	currentMilliseconds = playdate.getCurrentTimeMilliseconds()
	elapsedTime = playdate.getElapsedTime()
	
	gfx.setImageDrawMode("fillWhite")
	gfx.drawText("total time in ms since start: " .. currentMilliseconds, 30, 50)
	
	gfx.drawText("time in sec since elapsed time last reset: " .. elapsedTime, 30, 100)
	
	gfx.drawText("(press A to reset elapsed time)", 30, 150)
	
	gfx.drawText(lapTime, 30, 200)
end

-- -- -- -- INPUTS -- -- -- -- 
function playdate.AButtonDown()
	playdate.resetElapsedTime()
	
	lapTime = elapsedTime
end

1

2 Likes

Thanks a lot! I'm guessing this is for Lua? I will see if i can figure this out to work in Pulp Script. I haven't touched Lua yet. Thanks again!

I've moved this thread to the Pulp category.

It was in the SDK Get Help category.

I agree it's not as clear as it could be!

1 Like

Sorry Jeff, I shouldn't have assumed what tool you were using !

Oops. Sorry, i put it in the wrong spot.

My fault. I never said i was using Pulp Script and put it in the wrong spot. Sorry, first time posting.

Not at all! It needs to be clearer.

Here is my countdown timer in Pulp. It’s not exactly what you need but you can tweak it… put in reverse etc.

Attached as a zip, but inside it is just a .json you can import into Pulp.

Timer.zip (2.8 KB)

1 Like

Thanks! I will definitely try this out!

The file won't download. keeps giving me an error.

Hmm. That’s weird. I tried uploading the json directly but it said it wouldn’t accept that file type.

Here's a basic timer. This isn't the most elegant way to do it since the modulo function isn't available in Pulp, but this will work as a basic timer.

In the game script do:

on load do
	startTime = datetime.timestamp
end

on loop do
	//Get The Current Time
	currentTime = datetime.timestamp
	
	//Calculate how much time has passed since the game started
	elapsedTime = currentTime
	elapsedTime -= startTime
	
	//If more than 60sec have passed, increase the minute counter
	//And reset the seconds counter to 0
  if elapsedTime >= 60 then
    startTime = currentTime
    elapsedMin += 1
    elapsedTime = 0
  end
  
  elapsedSec = elapsedTime
  
end

If you want to display the timer on screen, in the player script do:

on draw do
	label "Timer: {2,0:elapsedMin}:{2,0:elapsedSec}" at 0,0
end

Thanks so much! Worked perfectly! Everyone here has been extremely helpful. Being a newbie, I need all the help i can get right now.

1 Like