MrxTimer
Module: mrxtimer.lua
Overview
The MrxTimer module is responsible for managing countdown and stopwatch timers in the game’s user interface. It provides functionality to start, pause, resume, stop, and display timer values on the HUD (Heads-Up Display). The timer can be configured with various properties such as start time, stop time, step size, and whether to use tenths of a second.
Inheritance
- Inherits from:
none— base/utility module - Imports:
MrxUtil,MrxGuiInterface
Instance pattern
Not per-uGuid — same class-factory pattern used elsewhere in resident/: Create(mModule, self) is self = self or {}; setmetatable(self, {__index = mModule}); return self, no tInstance registry. It tracks the following key fields:
nStartTime: Starting time of the timer (default30). IfnStartTime > nStopTime, the timer counts down (_bCountdownis set); otherwise it counts up.nStopTime: Time the timer stops at (default0).nStep: Seconds per tick / increment-decrement size (default1). Also theEvent.TimerRelativeinterval.bUseTenths: Whether to display tenths of a second (defaultfalse).nWarning: Threshold at which the display turns[red]and warning sounds/tWarnCallbacksfire (default5).iTray: HUDHud.ObjectiveTrayslot the timer text is written to (default1).bPlaySounds: Whether to play the timer sound cues (defaulttrue)._iCurrentTime: The current time value of the timer._bCountdown: Indicates whether the timer is counting down or up._TimerEvent: The event handle for the timer’s update callback.sLabel: An optional label text displayed with the timer.tWarnCallbacks: Callback functions triggered when the timer reaches the warning time.tDoneCallbacks: Callback functions triggered when the timer reaches its stop time.
Functions
Create(mModule, self)
Creates a new per-instance table for the timer using the module’s prototype. Initializes default values for various fields if they are not provided.
Start(self)
Initializes the timer with the starting time and sets up the update event to call _Update at regular intervals. Plays a start sound if enabled.
Display(self)
Updates the HUD display of the timer based on the current time value, applying color changes for warnings and appending any optional label text.
Pause(self)
Pauses the timer by deleting the update event.
Resume(self)
Resumes the timer by setting up the update event again.
AddTime(self, iTime)
Adds a specified amount of time to the current timer value and updates the display.
Stop(self)
Stops the timer by clearing the HUD slot and deleting the update event. Plays an end sound if enabled.
_Update(self)
The internal function that updates the timer’s current time, checks for warning conditions, triggers callbacks, and updates the display. Also handles the transition to stop time and plays alert sounds as needed.
GetTime(self)
Returns the current time value of the timer.
SetTime(self, iNewTime)
Sets a new time value for the timer and updates the display.
_CallCallbacks(t)
A helper function that calls all registered callbacks in a table with optional arguments.
Events
- The only
Event.*used isEvent.CreatePersistent(Event.TimerRelative, {self.nStep}, self._Update, {self})— a persistent timer that fires_UpdateeverynStepseconds. Created inStart/Resume, torn down withEvent.DeleteinPause/Stop. NoEvent.Createsubscriptions to game events; thetWarnCallbacks/tDoneCallbacks“callbacks” are plain Lua function tables the module calls directly, not engine events.
Notes for modders
- Sound cues fired (all via
Sound.CueSound(0, ...), gated onbPlaySounds):"ui_HUD_Timer_Start"onStart;"ui_HUD_Timer_Increment"when the display crosses a minute/10s/1s boundary (thenIncrementAlertstep in_Update);"ui_HUD_Timer_Alert"when crossingnWarning;"ui_HUD_Timer_End"when reachingnStopTime. SetbPlaySounds = falsefor a silent timer. - The set-up pattern: build a table of the fields above, pass it to
Create, registertWarnCallbacks/tDoneCallbacks(each an array of{fn, args}pairs —_CallCallbacksruns them viaMrxUtil.CallWithOptionalArgs), then callStart. UseAddTime/SetTimeto adjust mid-run. - Countdown vs. count-up is inferred, not a flag — it’s decided in
Startpurely fromnStartTime > nStopTime. To count up, setnStartTime < nStopTime. Displaycolors the text[red]once inside the warning band and prependssLabelif set — the timer is written toHud.ObjectiveTrayslotiTray, so two timers need differentiTrayvalues or they overwrite each other.