Author Topic: chronometer.lua: Measure the time played  (Read 464 times)

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1008
    • View Profile
chronometer.lua: Measure the time played
« on: October 26, 2016, 03:24:58 pm »
This chronometer script allows to know how much time a game was played.

How to use it

- Copy the chronometer.lua script below to the scripts folder of your project.
- Just do require("scripts/chronometer") at some point, for example in your game_manager script.

That's it! Your game objects will now have two additional functions: game:get_time_played() (to get the value in seconds) and game:get_time_played_string() (to get a formatted string).
Then it is up to you to display the time in a dialog or in a menu of your choice.

Prerequisites

The multi_events.lua script is necessary for the chronometer to work.

The code!

Script: scripts/chronometer.lua
License: GPL v3
Author: Christopho
URL: https://github.com/solarus-games/zelda-olb-se/blob/dev/data/scripts/chronometer.lua
Code: Lua
  1. -- Adds chronometer features to games.
  2. -- The following functions are provided:
  3. -- - game:get_time_played():            Returns the game time in seconds.
  4. -- - game:get_time_played_string():     Returns a string representation of the game time.
  5.  
  6. -- Usage:
  7. -- require("scripts/chronometer")
  8.  
  9. require("scripts/multi_events")
  10.  
  11. -- Measure the time played.
  12. local function initialize_chronometer_features(game)
  13.  
  14.   -- Returns the game time in seconds.
  15.   function game:get_time_played()
  16.     local milliseconds = game:get_value("time_played") or 0
  17.     local total_seconds = math.floor(milliseconds / 1000)
  18.     return total_seconds
  19.   end
  20.  
  21.   -- Returns a string representation of the game time.
  22.   function game:get_time_played_string()
  23.     local total_seconds = game:get_time_played()
  24.     local seconds = total_seconds % 60
  25.     local total_minutes = math.floor(total_seconds / 60)
  26.     local minutes = total_minutes % 60
  27.     local total_hours = math.floor(total_minutes / 60)
  28.     local time_string = string.format("%02d:%02d:%02d", total_hours, minutes, seconds)
  29.     return time_string
  30.   end
  31.  
  32.   local timer = sol.timer.start(game, 100, function()
  33.     local time = game:get_value("time_played") or 0
  34.     time = time + 100
  35.     game:set_value("time_played", time)
  36.     return true  -- Repeat the timer.
  37.   end)
  38.   timer:set_suspended_with_map(false)
  39. end
  40.  
  41. -- Set up chronometer features on any game that starts.
  42. local game_meta = sol.main.get_metatable("game")
  43. game_meta:register_event("on_started", initialize_chronometer_features)
  44.  
  45. return true
  46.  
« Last Edit: April 25, 2017, 10:21:28 am by Christopho »

MetalZelda

  • Sr. Member
  • ****
  • Posts: 479
    • View Profile
Re: chronometer.lua: Measure the time played
« Reply #1 on: October 26, 2016, 03:53:23 pm »
This is useful for speed run and achievements, i'll take it
Thanks