Game Time gives the server an in-game clock and calendar, as opposed to the Time, Date
and DateTime classes, which deal with the real world.
The game calendar is the Gregorian one, with months of their usual lengths, leap years and
weekdays, for the years from 1 to 9999. A moment of game time is a timestamp: the number of
game seconds since midnight of 1 January 1970, negative for the moments before it. It has
no time zone. GameTime:to_timestamp builds one out of a date, GameTime:get_date takes
one apart, and GameTime:parse and GameTime:to_string convert between a timestamp and
text such as '2020-01-01 08:00'.
The server owns the clock. It starts at the date of the 'gametime_start' config, advances
one game minute every 'gametime_minute_length' real seconds and is saved with the rest of
the persistent data, so it carries on after a restart or a change of map. While the server
is empty and hibernates the clock stands still. With the 'gametime_real_time' config the
clock shows the real date and time of the server instead; the game time that was saved
is kept, and the clock goes back to it when the config is turned off. Staff set the clock
with the SetTime command, code with GameTime:set.
The clock is networked as the moment it was last set together with its speed, in the
'gametime' networked global, and every client works the current time out by itself, so
nothing is sent while the clock just runs. GameTime:now, GameTime:get_date and the
format functions therefore work on the server and the client alike.
-- 'Wednesday, 1 January 2020, 08:00'
local text = GameTime:format()
-- Close the shops for the night.
function MyPlugin:GameHourPassed(current, previous)
if current.hour == 22 then
self:close_shops()
end
end
The GameMinutePassed, GameHourPassed, GameDayPassed, GameMonthPassed and
GameYearPassed hooks run on both realms as the clock rolls over, and GameTimeSet runs
when the clock is set to another moment.
Players see the time and the date in the top right corner of the tab menu. With the
Settings plugin loaded they can hide that clock and switch it to the 12-hour format; the
ShouldDrawGameTime hook lets a schema hide it, and the PaintGameTimeClock theme hook
lets a theme draw it.
Summary
Hooks
GameTimeSet.Methods
GameTime:save has written for the schema.ShouldDrawGameTime hook does not object.GameTime:parse takes.Hooks
Implements hook AddTabMenuItems
Puts the clock at the right end of the button bar of the tab menu.
Parameters
- menu Panel the tab menu
GameTime:GlobalNetVarChanged(key, old_value, new_value)
client plugins/gametime/plugin/cl_hooks.lua:52Implements hook GlobalNetVarChanged
Notices that the server has set the game clock to another moment, which is when the
serial of the networked clock changes: makes the rollover hooks start over from the new
time and runs GameTimeSet. A clock that arrives for the first time, changes its speed or
is corrected keeps its serial.
Parameters
- key String name of the networked global that has changed
- old_value Any previous value, the previous clock in the case of the game clock
- new_value Any new value, the new clock in the case of the game clock
Implements hook LazyTick
Checks eight times a second whether the game clock has rolled over.
Implements hook LoadData
Starts the game clock when the persistent data is loaded: at the real time of the server with the 'gametime_real_time' config, otherwise at the time that was saved, or at the 'gametime_start' config if nothing has been saved yet.
Implements hook OnConfigSet
Applies the changes of the 'gametime_minute_length' and 'gametime_real_time' configs to
the running clock. A new minute length changes the speed from the current moment on.
Turning the real time on saves the game time and moves the clock to the real time of the
server. Turning it off moves the clock back to the game time that was saved, the same
moment it would start from after a restart, or to the 'gametime_start' config if nothing
has been saved. Both count as setting the clock and run GameTimeSet. The new value is
compared with the running clock rather than with the old value of the config, because a
code refresh puts the configs back to their defaults without telling the plugin.
Parameters
- key String config key
- _old_value Any value the config had
- new_value Any value the config is about to get
Implements hook OneSecond
Keeps a clock that follows the real time of the server on that time, by correcting it once it is two seconds or more away. That happens when the server has been hibernating or when its own clock has been changed. A correction that takes the clock back by more than a minute counts as setting it, so that the rollover hooks start over.
Implements hook RegisterClientSettings
Registers the settings of the clock with the Settings plugin: 'gametime_clock', which shows or hides the clock of the tab menu, and 'gametime_twelve_hour', which switches the game time to the 12-hour format. Only called when the Settings plugin is loaded.
Implements hook SaveData
Saves the game time with the rest of the persistent data.
Methods
Internal
This function is internal. It is not part of the public API and may change without notice.
Compares the game time with the last minute that was seen and runs the hooks of the
minute, hour, day, month and year that have begun since. Every hook runs once per check,
no matter how much game time has gone by, and only as the clock moves forward: setting it
to another moment runs GameTimeSet instead. The client starts checking once the local
player has been initialized.
Returns the amount of days in a month of the game calendar.
Parameters
- year Number year, which decides the length of February
- month Number month, from 1 to 12
Returns
- Number amount of days, nil if there is no such month
Formats the date and the time of day of a timestamp, such as 'Wednesday, 1 January 2020, 08:00'.
Parameters
- timestamp Number optional, defaults to
GameTime:now()game seconds since midnight of 1 January 1970 - twelve_hour Boolean optional, defaults to
GameTime:uses_twelve_hour()use the 12-hour clock - lang String optional, defaults to
nillanguage code, the current language by default
Returns
- String
See also
Formats the date of a timestamp, such as 'Wednesday, 1 January 2020', or '01/01/2020' in the short form. The order and the punctuation come from the language.
Parameters
- timestamp Number optional, defaults to
GameTime:now()game seconds since midnight of 1 January 1970 - short Boolean optional, defaults to
falsewrite the date with numbers only - lang String optional, defaults to
nillanguage code, the current language by default
Returns
- String
Formats the time of day of a timestamp, such as '17:05' or '5:05 PM'.
Parameters
- timestamp Number optional, defaults to
GameTime:now()game seconds since midnight of 1 January 1970 - twelve_hour Boolean optional, defaults to
GameTime:uses_twelve_hour()use the 12-hour clock - lang String optional, defaults to
nillanguage code, the current language by default
Returns
- String
Returns the clock as the server has networked it: the moment it was last set to and how fast it runs since.
Returns
- Map the clock with the fields: base (Number timestamp the clock was set to), anchor (Number CurTime() at which it was set), rate (Number game seconds that pass in a real second), real (Boolean whether it follows the real time of the server) and serial (Number that grows every time the clock is set to another moment); nil while the server has not started the clock or the client has not received it
Takes a timestamp apart into a date and a time of day of the game calendar.
local date = GameTime:get_date()
if date.weekday >= 6 then
print('It is the weekend, and '..date.hour..' o\'clock.')
end
Parameters
- timestamp Number optional, defaults to
GameTime:now()game seconds since midnight of 1 January 1970
Returns
- Map the date with the fields: year, month (1 to 12), day (of the month), hour (0 to 23), minute, second (whole, 0 to 59), weekday (1 for Monday to 7 for Sunday) and timestamp (the timestamp that was taken apart)
See also
Returns the current day of the month of the game calendar.
Returns
- Number
Returns the current hour of the game clock.
Returns
- Number hour, from 0 to 23
Returns the current minute of the game clock.
Returns
- Number minute, from 0 to 59
Returns the current month of the game calendar.
Returns
- Number month, from 1 to 12
Returns the name of a month the way it is written in a date.
Parameters
- month Number month, from 1 to 12
- lang String optional, defaults to
nillanguage code, the current language by default
Returns
- String translated name, an empty string if there is no such month
Returns how fast the game clock runs.
Returns
- Number game seconds that pass in a real second; 1 while the clock is not ready
Returns the real date and time of the server, in its own time zone, as a timestamp of the game calendar.
Returns
- Number game seconds since midnight of 1 January 1970
Returns the moment the game clock starts from when no game time has been saved: the 'gametime_start' config, or 08:00 of 1 January 2020 if the config cannot be read as a date.
Returns
- Number game seconds since midnight of 1 January 1970
Returns the current day of the week of the game calendar.
Returns
- Number 1 for Monday to 7 for Sunday
Returns the name of a day of the week.
Parameters
- weekday Number 1 for Monday to 7 for Sunday
- lang String optional, defaults to
nillanguage code, the current language by default
Returns
- String translated name, an empty string if there is no such day
Returns the current year of the game calendar.
Returns
- Number
Checks whether a year of the game calendar is a leap year.
Parameters
- year Number
Returns
- Boolean
Checks whether the clock is running: on the server once the saved data has been loaded, on the client once the clock has arrived from the server.
Returns
- Boolean
Checks whether the game clock follows the real date and time of the server, which is what the 'gametime_real_time' config makes it do.
Returns
- Boolean
GameTime:is_valid_date(year, month, day, hour, minute, second)
shared plugins/gametime/plugin/sh_plugin.lua:143Checks whether the numbers make up a date and a time of day that exist in the game calendar: whole numbers, a year from 1 to 9999, a month from 1 to 12, a day that the month has, an hour from 0 to 23 and a minute and a second from 0 to 59.
Parameters
- year Number
- month Number
- day Number
- hour Number optional, defaults to
0 - minute Number optional, defaults to
0 - second Number optional, defaults to
0
Returns
- Boolean
Reads the game time that GameTime:save has written for the schema.
Returns
- Number game seconds since midnight of 1 January 1970, or nil if nothing has been saved yet or the saved date cannot be read
Returns the current game time.
-- The game time in two game hours.
local closing_time = GameTime:now() + 2 * 60 * 60
Returns
- Number game seconds since midnight of 1 January 1970, with a fraction; 0 while the clock is not ready
See also
Reads a timestamp from text: a date as 'YYYY-MM-DD', a time of day as 'HH:MM' or 'HH:MM:SS', or a date followed by a time of day. What the text leaves out is taken from another moment: a date alone keeps its hour and minute, a time of day alone its date.
GameTime:parse('2020-01-01 08:00')
-- Half past nine in the evening of the current game day.
GameTime:parse('21:30')
-- The same time of day as now, on another date.
GameTime:parse('2021-06-15')
Parameters
- text String text to read
- relative_to Number optional, defaults to
GameTime:now()timestamp that fills in what the text leaves out
Returns
- Number game seconds since midnight of 1 January 1970, or nil if the text is not a date or a time that exists in the game calendar
See also
Internal
This function is internal. It is not part of the public API and may change without notice.
Works out the timestamp that a clock shows at the moment.
Parameters
- clock Map clock, as returned by
GameTime:get_clock
Returns
- Number game seconds since midnight of 1 January 1970
Writes the current game time to the data of the schema, as a date and a time of day. Nothing is written while the clock follows the real time of the server, so the game time that was saved before stays there for when the 'gametime_real_time' config is turned off again.
Sets the game clock and saves the new time. Refused while the clock follows the real time of the server.
-- Skip to the next morning.
local date = GameTime:get_date()
GameTime:set(GameTime:to_timestamp(date.year, date.month, date.day + 1, 8))
-- Set the clock from text.
local timestamp = GameTime:parse('2020-01-01 08:00')
if timestamp then
GameTime:set(timestamp)
end
Parameters
- timestamp Number game seconds since midnight of 1 January 1970, within the years from 1 to 9999
Returns
- Boolean whether the clock has been set
- String language phrase of the error if it has not: 'error.gametime.not_ready', 'error.gametime.real_time' or 'error.gametime.invalid'
See also
GameTime:set_clock(timestamp, rate, real_time, is_jump)
server internal plugins/gametime/plugin/sv_plugin.lua:64Internal
This function is internal. It is not part of the public API and may change without notice.
Puts the clock at a timestamp from this moment on and networks it to everyone. A jump
makes the rollover hooks start over from the new time and runs GameTimeSet; without it
the hooks carry on as if the clock had simply run on.
Parameters
- timestamp Number game seconds since midnight of 1 January 1970 that the clock shows from now on
- rate Number game seconds that pass in a real second
- real_time Boolean whether the clock follows the real time of the server
- is_jump Boolean optional, defaults to
falsethe clock is being set to another moment, as opposed to being started, changing its speed or being corrected
Checks whether the clock of the tab menu should be drawn: the game clock has arrived from
the server, the player has not turned the 'gametime_clock' setting off and the
ShouldDrawGameTime hook does not object.
Returns
- Boolean
Writes a timestamp as 'YYYY-MM-DD HH:MM', which reads the same in every language and is
what GameTime:parse takes.
Parameters
- timestamp Number optional, defaults to
GameTime:now()game seconds since midnight of 1 January 1970
Returns
- String
GameTime:to_timestamp(year, month, day, hour, minute, second)
shared plugins/gametime/plugin/sh_plugin.lua:176Converts a date and a time of day of the game calendar to a timestamp. The numbers are
not checked: use GameTime:is_valid_date on anything that comes from a player. A day,
hour, minute or second past its usual range runs on into the next month, day, hour or
minute, which makes for simple date arithmetic; the month has to be from 1 to 12.
-- Noon of 24 December 2020.
local timestamp = GameTime:to_timestamp(2020, 12, 24, 12)
-- Eight in the morning of the next game day, also on the last day of a month.
local date = GameTime:get_date()
local morning = GameTime:to_timestamp(date.year, date.month, date.day + 1, 8)
Parameters
- year Number year
- month Number optional, defaults to
1month, from 1 to 12 - day Number optional, defaults to
1day of the month - hour Number optional, defaults to
0hour, from 0 to 23 - minute Number optional, defaults to
0minute, from 0 to 59 - second Number optional, defaults to
0second, from 0 to 59
Returns
- Number game seconds since midnight of 1 January 1970
See also
Checks whether times are shown with a 12-hour clock by default. On the client that is the 'gametime_twelve_hour' setting of the player, which exists when the Settings plugin is loaded. The server always uses the 24-hour clock.
Returns
- Boolean