Menu

Timers

g-dollar

Timers are an integral part of any game, and AGE provides flexibility to create many different use cases.

Timers

Timers are Game Objects, so they must go through the Install Pipeline to connect with the timer service (and any other services).

Properties

Every timer has the following properties:

  • Duration: in MS.
  • Continuous: if true, callback occurs on every time base tick. If false, callback occurs at the end of the duration.
  • Auto-repeat: if true, automatically reset the timer. if false, one-shot and unregister.
  • Register on install: if true, automatically register timer during the Install Pipeline. If false, GO will register later.

Timer Service

The timer service uses a time base tick (TBT) to control the passage of time on outstanding timers. A timer should make its duration a multiple of TBT. The TBT is the minimum time resolution of the service, and all timers are updated in batch each TBT period.

GOs opt-in to the timer service by implementing the IRequireTimerService interface. The Install Pipeline automatically calls the register() method and passes the GO.

The timer service uses the properties indicated in the timer GO to trigger the timer callback as specified. All timer callbacks are sent to the Game Cycle thread for execution.

Event Recycling

Since the timer service can generate a continuous stream of many of messages, it accepts "used" messages back for recycling, to avoid excessive object creation. The Game Cycle thread automatically recycles timer event messages back to the Timer thread.

Timer Events

The Game Cycle receives all timer events. Since the primary purpose of timer events is to modify the game state at regular intervals, timer events are always executed with the update lock active.

Timer Callback

The execute() method is where the work happens. If continuous this is called every time base tick, otherwise it is called on expiration. The execute() method has this signature:

  • delta: incremental amount of time since last call.
  • elapsed: current amount of time elapsed.
  • last: if true, this is the last call for this timer interval.
  • lc: the Locator service, to locate GOs.

Locator

Timer event callbacks do not directly reference any GOs; instead they are passed the Locator interface, so they can obtain GOs that they wish to read/write properties and/or invoke methods on.

Update Safety

Since all timer events are serialized, updates performed in a timer event callback can be considered atomic and isolated, i.e. another model update event cannot see "partial" updates of the game state.


Related

Wiki: Home

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.