Timer Manager
The timer manager object provides library support for calling functions with a time interval i.e. waiting one second before calling a function. It is loaded in all game modes and provides similar but not identical functionality in each.
It is rare for game scripts to need direct access to the timer manager. The timer manager is automatically created as the script libraries are loaded, and both the battle_manager
and campaign_manager
provide pass-through functions to the timer manager which game scripts typically use instead of calling the timer manager directly. For the campaign these functions are documented in the Timer Callbacks
section of this documentation, and in battle they are documented here: Timer Callbacks
.
Direct access to the timer manager might be more useful for frontend scripts, but they are rarer in themselves. The core function core:get_tm
can be used to get a handle to the timer manager after it has been created.
Loaded in Campaign | |
Loaded in Battle | |
Loaded in Frontend |
The campaign construction function timer_manager:new_campaign
is automatically called by the campaign_manager
during the loading sequence (when the WorldCreated
model event is received), so there should be no need for client scripts to call it.
The mechanics of the underlying campaign model timing system is very different to the equivalent system in battle. As such, the campaign constructor provides versions of timer_manager:callback
, timer_manager:repeat_callback
and timer_manager:remove_callback
which are different from those provided in battle. The usage of the campaign version of these functions is the same as for battle, with the key difference that model time in campaign is expressed in seconds in campaign and milliseconds in battle. Real Timers
use milliseconds in all game modes.
During the end-turn sequence the update rate of the campaign model can accelerate wildly. This will cause a function registered to be called after five seconds to happen near-instantly during the end turn sequence, for example. To ameliorate this effect, the timer manager will automatically check the real world time once the interval has completed. If the real world time is less than would be expected then the callback is retried repeatedly until the real world interval has elapsed. This behaviour is only active in singleplayer mode as it would cause desyncs in multiplayer.
-
timer_manager:new_campaign(game_interface
game interface)
-
Creates and returns a timer manager in campaign. This function should be supplied a
game_interface
, which requires that it is created when theNewSession
script event is received (or later). The timer manager is automatically created by thecampaign_manager
so there should be no need for game scripts to call this function.Parameters:
1
game_interface
game interface
Returns:
timer managertimer_manager
defined in ../../warhammer/working_data/script/_lib/lib_timer_manager.lua, line 62
The battle-specific constructor timer_manager:new_battle
is automatically called by the battle_manager
when it is created, so there should be no need for client scripts to call it.
The mechanics of the underlying battle model timing system is very different to the equivalent system in campaign. Therefore, the battle constructor provides versions of timer_manager:callback
, timer_manager:repeat_callback
and timer_manager:remove_callback
which are different from those provided in campaign. The usage of these functions is the same in battle and campaign, with the key difference that model time in campaign is expressed in seconds in campaign and milliseconds in battle. Real Timers
use milliseconds in all game modes.
-
timer_manager:new_battle(
battle interfacebattle
)
-
Creates and returns a timer manager in battle. This function should be supplied a
battle
game object. The timer manager is automatically created by thebattle_manager
so there should be no need for game scripts to call this function.Parameters:
1
battle interface
Returns:
timer managertimer_manager
defined in ../../warhammer/working_data/script/_lib/lib_timer_manager.lua, line 241
The frontend-specific constructor timer_manager:new_frontend
is automatically called in lib_header.lua
as it's loaded, so there should be no need for client scripts to call it.
There is no game model to provide timer functionality in the frontend, so only the real timer is available. The frontend-specific constructor creates timer_manager:callback
, timer_manager:repeat_callback
and timer_manager:remove_callback
functions but these remap to timer_manager:real_callback
, timer_manager:repeat_real_callback
and timer_manager:remove_callback
respectively.
-
timer_manager:new_frontend()
-
Creates and returns a timer manager in frontend.
Returns:
timer managertimer_manager
defined in ../../warhammer/working_data/script/_lib/lib_timer_manager.lua, line 400
Model timers are synchronised to the game model in campaign and battle. They are the main timers used by game scripts. Both the campaign manager and battle manager provide pass-through interfaces to the functionality documented here - see Timer Callbacks
(campaign) and Timer Callbacks
(battle). It is intended that game scripts call these pass-through functions rather than the timer manager itself.
When a callback is created with an interval of one second, then the callback function would be called as part of a model update where at least one second of model time has elapsed. This means:
- Model timers are confined to the resolution of the game model update, which is currently between 100ms and 200ms. A model timer callback with an interval of 250ms, for example, would not be called until the following model tick, at 300-400ms after the callback was created.
- Model updates will speed up and slow down in battle if the player changes their battle speed, and also speed up a lot during the end turn sequence in battle. Model updates are also stopped when the game is paused. For these reasons a model timer callback of one second cannot be relied on to trigger after one second has passed in the real world. See
Real Timers
for callback functions linked to a timer synchronised to real time. - As model updates are synchronised between all clients in a multiplayer game, so too are model timers.
- There is no game model present in the frontend, so only real timers are available. The functions
timer_manager:callback
,timer_manager:repeat_callback
andtimer_manager:remove_callback
are available in the frontend but will redirect to the real timer equivalents.
It is strongly recommended that game scripts rely on model timers as their primary mode of script timing, and only use real timers in certain specific situations, usually to do with UI manipulation.
-
timer_manager:callback(
callbackfunction
,
intervalnumber
, [
namestring
])
-
Adds a model callback to be called after the supplied interval has elapsed.
Parameters:
1
Callback to call.
2
Interval after which to call the callback. This should be in milliseconds in battle and in the frontend, and in seconds in campaign.
3
optional, default value=nil
Callback name, by which it may be later removed with
timer_manager:remove_callback
. If omitted the callback may not be cancelled.Returns:
nil
defined in ../../warhammer/working_data/script/_lib/lib_timer_manager.lua, line 456
-
timer_manager:repeat_callback(
callbackfunction
,
intervalnumber
, [
namestring
])
-
Adds a repeating model callback to be called each time the supplied interval elapses.
Parameters:
1
Callback to call.
2
Interval after which to call the callback. This should be in milliseconds in battle and in the frontend, and in seconds in campaign.
3
optional, default value=nil
Callback name, by which it may be later removed with
timer_manager:remove_callback
. If omitted the repeating callback may not be cancelled.Returns:
nil
defined in ../../warhammer/working_data/script/_lib/lib_timer_manager.lua, line 464
-
timer_manager:remove_callback(
namestring
)
-
Removes a real callback previously added with
timer_manager:callback
ortimer_manager:repeat_callback
by name. All callbacks with a name matching that supplied will be cancelled and removed.Parameters:
1
Name of callback to remove.
Returns:
nil
defined in ../../warhammer/working_data/script/_lib/lib_timer_manager.lua, line 471
Real timers are synchronised to user-interface updates rather than game model updates, like Model Timers
. In practice, this means:
- Real timers continue to tick and will call their callback even if model updates are paused (e.g. the game is paused in battle).
- Real timers do not change speed with the model (in battle, and also in campaign during the end-turn sequence).
- Real timers can have a resolution greater than a model tick (100-200ms).
- Real timers have a variable resolution - as the frame rate changes, so does the frequency with which real timers get checked.
- Real timers work in the frontend where no game model is present.
- Synchronicity between different clients in a multiplayer game cannot be guaranteed (and is highly unlikely to be achieved).
- Due to the lack of synchronisation between the real timer and the model, the latter may not be in a suitable state to be queried when a real timer is called.
It is strongly recommended that game scripts rely on model timers as their primary mode of script timing, and only use real timers in certain specific situations, usually to do with UI manipulation.
-
timer_manager:real_callback(
callbackfunction
,
intervalnumber
, [
namestring
])
-
Adds a real callback to be called after the supplied interval has elapsed.
Parameters:
1
Callback to call.
2
Interval after which to call the callback. This should be in milliseconds, regardless of game mode.
3
optional, default value=nil
Callback name, by which it may be later removed with
timer_manager:remove_real_callback
. If omitted the callback may not be cancelled.Returns:
nil
defined in ../../warhammer/working_data/script/_lib/lib_timer_manager.lua, line 547
-
timer_manager:repeat_real_callback(
callbackfunction
,
intervalnumber
, [
namestring
])
-
Adds a repeating real callback to be called each time the supplied interval elapses.
Parameters:
1
Callback to call.
2
Repeating interval after which to call the callback. This should be in milliseconds, regardless of game mode.
3
optional, default value=nil
Callback name, by which it may be later removed with
timer_manager:remove_real_callback
. If omitted the repeating callback may not be cancelled.Returns:
nil
defined in ../../warhammer/working_data/script/_lib/lib_timer_manager.lua, line 565
-
timer_manager:remove_real_callback(
namestring
)
-
Removes a real callback previously added with
timer_manager:real_callback
ortimer_manager:repeat_real_callback
by name. All callbacks with a name matching that supplied will be cancelled and removed.Parameters:
1
Name of callback to remove.
Returns:
nil
defined in ../../warhammer/working_data/script/_lib/lib_timer_manager.lua, line 579