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
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) Select
-- 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.
-- Usage:
-- require("scripts/chronometer")
require("scripts/multi_events")
-- Measure the time played.
local function initialize_chronometer_features(game)
-- Returns the game time in seconds.
function game:get_time_played()
local milliseconds = game:get_value("time_played") or 0
local total_seconds = math.floor(milliseconds / 1000)
return total_seconds
end
-- Returns a string representation of the game time.
function game:get_time_played_string()
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)
return time_string
end
local timer = sol.timer.start(game, 100, function()
local time = game:get_value("time_played") or 0
time = time + 100
game:set_value("time_played", time)
return true -- Repeat the timer.
end)
timer:set_suspended_with_map(false)
end
-- Set up chronometer features on any game that starts.
local game_meta = sol.main.get_metatable("game")
game_meta:register_event("on_started", initialize_chronometer_features)
return true