Lysa  0.0
Lysa 3D Engine
Timer Class Reference

Detailed Description

One-shot or repeating countdown timer.

When autoStart is true the timer starts immediately on construction. Each engine physics tick, Timers::update() advances all registered timers. When the wait time elapses the C++ callback (and/or the Lua callback) is invoked; a one-shot timer then stops while a repeating timer resets itself.

Public Typedefs

using Callback = std::function< void ()>
 

Public Member Functions

 Timer (float waitTime, bool oneShot=true, bool autoStart=false, const Callback &callback=nullptr)
 
bool update (double deltaTime)
 
void start ()
 
void stop ()
 
bool isStopped () const
 
float getWaitTime () const
 
void setWaitTime (const float t)
 
bool isOneShot () const
 
void setOneShot (const bool v)
 
virtual ~Timer ()
 

Protected Member Functions

virtual void fireCallback ()
 

Protected Attributes

float waitTime
 
bool oneShot
 
bool stopped {true}
 
double elapsed {0.0}
 
Callback callback
 

Member Typedef Documentation

using Callback = std::function<void()>

Callback type for C++ callers.

Constructor & Destructor Documentation

Timer ( float  waitTime,
bool  oneShot = true,
bool  autoStart = false,
const Callback callback = nullptr 
)

Creates a timer.

Parameters
waitTimeDelay in seconds before the callback fires (must be > 0).
oneShotIf true the timer stops after the first timeout. If false it repeats indefinitely until stop() is called.
autoStartIf true start() is called immediately in the constructor.
callbackOptional C++ callback invoked on each timeout. Can be set later for Lua.
virtual ~Timer ( )
virtual

Member Function Documentation

virtual void fireCallback ( )
protectedvirtual
float getWaitTime ( ) const
inline

Returns the configured wait time in seconds.

bool isOneShot ( ) const
inline

Returns true for one-shot mode, false for repeating.

bool isStopped ( ) const
inline

Returns true if the timer is neither running nor waiting to fire.

void setOneShot ( const bool  v)
inline

Enables or disables one-shot mode. Takes effect on the next start().

void setWaitTime ( const float  t)
inline

Sets a new wait time. Takes effect on the next start().

void start ( )

Starts or restarts the timer.

void stop ( )

Stops the timer without firing the callback.

bool update ( double  deltaTime)

Advances the timer by deltaTime seconds. Fires the callback(s) when the wait time is reached. Called automatically by Timers::update().

Returns
true if the timer has stopped (one-shot finished or stop() called).

Member Data Documentation

Callback callback
protected
double elapsed {0.0}
protected
bool oneShot
protected
bool stopped {true}
protected
float waitTime
protected