High Score Help

Hello! I'm not good with math in coding, so I really could use a hand with this. I've made a little game where you try to get a fast time in a few short levels, and I'd like to add a section that shows the fastest time you've gotten for a level. But I'm not sure how to do this, at all. Thank you!

1 Like

I'd suggest starting small, and only store 1 best time for each level. Pulp doesn't have arrays, so you'd need to create 1 variable for each level you have, for example:

on load do
  fastest_time_level_1 = 0
  fastest_time_level_2 = 0
  fastest_time_level_3 = 0
end

Then you need to keep track of how fast the player was. We can use the in-builtdatetime.timestamp variable for this:

(when player starts a level, eg on enter):
  level_start_time = datetime.timestamp

and then later...

(when player finishes level):
  level_time = datetime.timestamp
  level_time -= level_start_time

This gives us the seconds between starting and finishing the level, so we then just compare that to the high score. eg on level 1:

if fastest_time_level_1 == 0 then
  // no fastest score yet, so store it anyway
  fastest_time_level_1 = level_time
elseif fastest_time_level_1 > level_time then
  fastest_time_level_1 = level_time
end

You can then show the level times elsewhere as needed, but they won't be saved between sessions. To do that, you'll need to store the fastest_time_level_* variables when they get updated, and restore them when the game loads (eg at the end of the original on load event).

The code could be neater, especially if you have lots of levels, but hope that points you in the right direction anyway.

3 Likes

scribe describes a good approach, but one thing you might consider is using event.frame instead of datetime.timestamp so that the level time doesn't include time passed while the Playdate is locked. If you do use event.frame remember that Pulp games run at 20 frames per second so you'll need to divide by 20 to convert to seconds!

4 Likes