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
script is necessary for the chronometer to work.The code!
License: GPL v3
-- Adds chronometer features to games.
-- The following functions are provided:
-- - game:get_time_played(): Returns the game time in seconds.
-- - game:get_time_played_string(): Returns a string representation of the game time.
-- Measure the time played.
local function initialize_chronometer_features(game)
-- Returns the game time in seconds.
local milliseconds = game:get_value("time_played") or 0
local total_seconds = math.floor(milliseconds / 1000)
-- Returns a string representation of the game time.
local total_seconds = game:get_time_played()
local seconds = total_seconds % 60
local total_minutes = math.floor(total_seconds / 60)
local minutes = total_minutes % 60
local total_hours = math.floor(total_minutes / 60)
local time_string = string.format("%02d:%02d:%02d", total_hours, minutes, seconds)
local timer = sol.timer.start(game, 100, function()
local time = game:get_time_played()
time = time + 100
return true -- Repeat the timer.
-- Set up chronometer features on any game that starts.
local game_meta = sol.main.get_metatable("game")