Lua utility module Timer

Basic timer - after a specified number of seconds, the specified thing happens.

Example usage:

local Timer = require("Timer")

-- This will be called when the timer runs out
LevelFuncs.FinishTimer = function(healthWhenStarted, victoryMessage)
    -- Open a door, display a message, make an explosion... whatever you wish
    DoSomething(healthWhenStarted, victoryMessage)
end

-- This function triggers the timer
LevelFuncs.TriggerTimer = function(obj)
    local myTimer = Timer.Create("my_timer",
        5.0,
        false,
        {minutes = false, seconds = true, deciseconds = true},
        "FinishTimer",
        Lara:GetHP(),
        "Well done!")
    myTimer:Start()
end

LevelFuncs.OnControlPhase = function(dt)
    Timer.UpdateAll(dt)
end

Functions

Create(name, totalTime, loop, timerFormat, func[, ...]) Create (but do not start) a new timer.
Get(name) Get a timer by its name.
UpdateAll(dt) Update all active timers.
myTimer:SetFunction(func[, ...]) Give the timer a new function and args
myTimer:Start() Begin or unpause a timer.
myTimer:Stop() Stop the timer.
myTimer:IsActive() Get whether or not the timer is active
myTimer:SetPaused(p) Pause or unpause the timer.
myTimer:IsPaused() Get whether or not the timer is paused
myTimer:GetRemainingTime() Get the remaining time for a timer.
myTimer:SetRemainingTime(remainingTime) Set the remaining time for a timer
myTimer:GetRemainingTime() Get the total time for a timer.
myTimer:SetTotalTime(totalTime) Set the total time for a timer
myTimer:SetLooping(looping) Set whether or not the timer loops


Functions

Create(name, totalTime, loop, timerFormat, func[, ...])
Create (but do not start) a new timer.

You have the option of displaying the remaining time on the clock. Timer format details:

-- deciseconds are 1/10th of a second

-- mins:secs
local myTimeFormat1 = {minutes = true, seconds = true, deciseconds = false}

-- also mins:secs
local myTimeFormat2 = {minutes = true, seconds = true}

-- secs:decisecs
local myTimeFormat3 = {seconds = true, deciseconds = true}

-- secs; also what is printed if you pass true instead of a table
local myTimeFormat4 = {seconds = true}

At any given time, only one timer can show its countdown.

Use this sparingly; in the classics, timed challenges did not have visible countdowns. For shorter timers, the gameplay benefit from showing the remaining time might not be necessary, and could interfere with the atmosphere of the level.

Parameters:

  • name string A label to give this timer; used to retrieve the timer later
  • totalTime number The duration of the timer, in seconds
  • loop bool if true, the timer will start again immediately after the time has elapsed
  • timerFormat table or bool If a table is given, the remaining time will be shown as a string, formatted according to the values in the table. If true, the remaining seconds, rounded up, will show at the bottom of the screen. If false, the remaining time will not be shown on screen.
  • func string The name of the LevelFunc member to call when the time is upssss
  • ... a variable number of arguments with which the above function will be called (optional)

Returns:

    The timer in its paused state
Get(name)
Get a timer by its name.

Parameters:

  • name string The label that was given to the timer when it was created

Returns:

    The timer
UpdateAll(dt)
Update all active timers. Should be called in LevelFuncs.OnControlPhase

Parameters:

  • dt number The time in seconds since the last frame
myTimer:SetFunction(func[, ...])
Give the timer a new function and args

Parameters:

  • func string The name of the LevelFunc member to call when the time is up
  • ... a variable number of arguments with which the above function will be called (optional)
myTimer:Start()
Begin or unpause a timer. If showing the remaining time on-screen, its color will be set to white.
myTimer:Stop()
Stop the timer.
myTimer:IsActive()
Get whether or not the timer is active

Returns:

    true if the timer is active, false if otherwise
myTimer:SetPaused(p)
Pause or unpause the timer. If showing the remaining time on-screen, its color will be set to yellow (paused) or white (unpaused).

Parameters:

  • p bool if true, the timer will be paused; if false, it would be unpaused
myTimer:IsPaused()
Get whether or not the timer is paused

Returns:

    true if the timer is paused, false if otherwise
myTimer:GetRemainingTime()
Get the remaining time for a timer.

Returns:

    the time in seconds remaining on the clock
myTimer:SetRemainingTime(remainingTime)
Set the remaining time for a timer

Parameters:

  • remainingTime number the new time remaining for the timer
myTimer:GetRemainingTime()
Get the total time for a timer. This is the amount of time the timer will start with, as well as when starting a new loop

Returns:

    the timer's total time
myTimer:SetTotalTime(totalTime)
Set the total time for a timer

Parameters:

  • totalTime number timer's new total time
myTimer:SetLooping(looping)
Set whether or not the timer loops

Parameters:

  • looping bool whether or not the timer loops
generated by LDoc 1.4.6 Last updated 2022-08-11 22:43:52