Skip to content
Flecs v4.1
timer.h
Go to the documentation of this file.
1/**
2 * @file addons/timer.h
3 * @brief Timer module.
4 *
5 * Timers can be used to trigger actions at periodic or one-shot intervals. They
6 * are typically used together with systems and pipelines.
7 */
8
9#ifdef FLECS_TIMER
10
11/**
12 * @defgroup c_addons_timer Timer
13 * @ingroup c_addons
14 * Run systems at a time interval.
15 *
16 * @{
17 */
18
19#ifndef FLECS_MODULE
20#define FLECS_MODULE
21#endif
22
23#ifndef FLECS_PIPELINE
24#define FLECS_PIPELINE
25#endif
26
27#ifndef FLECS_TIMER_H
28#define FLECS_TIMER_H
29
30#ifdef __cplusplus
31extern "C" {
32#endif
33
34/** Component used for one-shot and interval timer functionality. */
35typedef struct EcsTimer {
36 ecs_ftime_t timeout; /**< Timer timeout period. */
37 ecs_ftime_t time; /**< Incrementing time value. */
38 ecs_ftime_t overshoot; /**< Used to correct returned interval time. */
39 int32_t fired_count; /**< Number of times ticked. */
40 bool active; /**< Is the timer active or not. */
41 bool single_shot; /**< Is this a single-shot timer. */
43
44/** Apply a rate filter to a tick source. */
45typedef struct EcsRateFilter {
46 ecs_entity_t src; /**< Source of the rate filter. */
47 int32_t rate; /**< Rate of the rate filter. */
48 int32_t tick_count; /**< Number of times the rate filter ticked. */
49 ecs_ftime_t time_elapsed; /**< Time elapsed since last tick. */
51
52
53/** Set timer timeout.
54 * This operation executes any systems associated with the timer after the
55 * specified timeout value. If the entity contains an existing timer, the
56 * timeout value will be reset. The timer can be started and stopped with
57 * ecs_start_timer() and ecs_stop_timer().
58 *
59 * The timer is synchronous, and is incremented each frame by delta_time.
60 *
61 * The tick_source entity will be a tick source after this operation. Tick
62 * sources can be read by getting the EcsTickSource component. If the tick
63 * source ticked this frame, the 'tick' member will be true. When the tick
64 * source is a system, the system will tick when the timer ticks.
65 *
66 * @param world The world.
67 * @param tick_source The timer for which to set the timeout (0 to create one).
68 * @param timeout The timeout value.
69 * @return The timer entity.
70 */
71FLECS_API
73 ecs_world_t *world,
74 ecs_entity_t tick_source,
75 ecs_ftime_t timeout);
76
77/** Get current timeout value for the specified timer.
78 * This operation returns the value set by ecs_set_timeout(). If no timer is
79 * active for this entity, the operation returns 0.
80 *
81 * After the timeout expires the EcsTimer component is removed from the entity.
82 * This means that if ecs_get_timeout() is invoked after the timer is expired, the
83 * operation will return 0.
84 *
85 * @param world The world.
86 * @param tick_source The timer.
87 * @return The current timeout value, or 0 if no timer is active.
88 */
89FLECS_API
91 const ecs_world_t *world,
92 ecs_entity_t tick_source);
93
94/** Set timer interval.
95 * This operation will continuously invoke systems associated with the timer
96 * after the interval period expires. If the entity contains an existing timer,
97 * the interval value will be reset.
98 *
99 * The timer is synchronous, and is incremented each frame by delta_time.
100 *
101 * The tick_source entity will be a tick source after this operation. Tick
102 * sources can be read by getting the EcsTickSource component. If the tick
103 * source ticked this frame, the 'tick' member will be true. When the tick
104 * source is a system, the system will tick when the timer ticks.
105 *
106 * @param world The world.
107 * @param tick_source The timer for which to set the interval (0 to create one).
108 * @param interval The interval value.
109 * @return The timer entity.
110 */
111FLECS_API
113 ecs_world_t *world,
114 ecs_entity_t tick_source,
115 ecs_ftime_t interval);
116
117/** Get current interval value for the specified timer.
118 * This operation returns the value set by ecs_set_interval(). If the entity is
119 * not a timer, the operation will return 0.
120 *
121 * @param world The world.
122 * @param tick_source The timer for which to get the interval.
123 * @return The current interval value, or 0 if no timer is active.
124 */
125FLECS_API
127 const ecs_world_t *world,
128 ecs_entity_t tick_source);
129
130/** Start timer.
131 * This operation resets the timer and starts it with the specified timeout.
132 *
133 * @param world The world.
134 * @param tick_source The timer to start.
135 */
136FLECS_API
138 ecs_world_t *world,
139 ecs_entity_t tick_source);
140
141/** Stop timer.
142 * This operation stops a timer from triggering.
143 *
144 * @param world The world.
145 * @param tick_source The timer to stop.
146 */
147FLECS_API
149 ecs_world_t *world,
150 ecs_entity_t tick_source);
151
152/** Reset time value of timer to 0.
153 * This operation resets the timer value to 0.
154 *
155 * @param world The world.
156 * @param tick_source The timer to reset.
157 */
158FLECS_API
160 ecs_world_t *world,
161 ecs_entity_t tick_source);
162
163/** Enable randomizing initial time value of timers.
164 * Initializes timers with a random time value, which can improve scheduling as
165 * systems/timers for the same interval don't all happen on the same tick.
166 *
167 * @param world The world.
168 */
169FLECS_API
171 ecs_world_t *world);
172
173/** Set rate filter.
174 * This operation initializes a rate filter. Rate filters sample tick sources
175 * and tick at a configurable multiple. A rate filter is a tick source itself,
176 * which means that rate filters can be chained.
177 *
178 * Rate filters enable deterministic system execution which cannot be achieved
179 * with interval timers alone. For example, if timer A has interval 2.0 and
180 * timer B has interval 4.0, it is not guaranteed that B will tick at exactly
181 * twice the multiple of A. This is partly due to the nondeterministic nature of
182 * timers, and partly due to floating-point rounding errors.
183 *
184 * Rate filters can be combined with timers (or other rate filters) to ensure
185 * that a system ticks at an exact multiple of a tick source (which can be
186 * another system). If a rate filter is created with a rate of 1, it will tick
187 * at the exact same time as its source.
188 *
189 * If no tick source is provided, the rate filter will use the frame tick as
190 * source, which corresponds with the number of times ecs_progress() is called.
191 *
192 * The tick_source entity will be a tick source after this operation. Tick
193 * sources can be read by getting the EcsTickSource component. If the tick
194 * source ticked this frame, the 'tick' member will be true. When the tick
195 * source is a system, the system will tick when the timer ticks.
196 *
197 * @param world The world.
198 * @param tick_source The rate filter entity (0 to create one).
199 * @param rate The rate to apply.
200 * @param source The tick source (0 to use frames).
201 * @return The rate filter entity.
202 */
203FLECS_API
205 ecs_world_t *world,
206 ecs_entity_t tick_source,
207 int32_t rate,
208 ecs_entity_t source);
209
210/** Assign tick source to system.
211 * Systems can be their own tick source, which can be any of the tick sources
212 * (one-shot timers, interval timers, and rate filters). However, in some cases it
213 * must be guaranteed that different systems tick on the exact same frame.
214 *
215 * This cannot be guaranteed by giving two systems the same interval/rate filter
216 * as it is possible that one system is (for example) disabled, which would
217 * cause the systems to go out of sync. To provide these guarantees, systems
218 * must use the same tick source, which is what this operation enables.
219 *
220 * When two systems share the same tick source, it is guaranteed that they tick
221 * in the same frame. The provided tick source can be any entity that is a tick
222 * source, including another system. If the provided entity is not a tick source
223 * the system will not be run.
224 *
225 * To disassociate a tick source from a system, use 0 for the tick_source
226 * parameter.
227 *
228 * @param world The world.
229 * @param system The system to associate with the timer.
230 * @param tick_source The tick source to associate with the system.
231 */
232FLECS_API
234 ecs_world_t *world,
235 ecs_entity_t system,
236 ecs_entity_t tick_source);
237
238
239////////////////////////////////////////////////////////////////////////////////
240//// Module
241////////////////////////////////////////////////////////////////////////////////
242
243/** Timer module import function.
244 * Usage:
245 * @code
246 * ECS_IMPORT(world, FlecsTimer)
247 * @endcode
248 *
249 * @param world The world.
250 */
251FLECS_API
253 ecs_world_t *world);
254
255#ifdef __cplusplus
256}
257#endif
258
259#endif
260
261/** @} */
262
263#endif
FLECS_API ecs_ftime_t ecs_get_timeout(const ecs_world_t *world, ecs_entity_t tick_source)
Get current timeout value for the specified timer.
FLECS_API void ecs_reset_timer(ecs_world_t *world, ecs_entity_t tick_source)
Reset time value of timer to 0.
FLECS_API ecs_entity_t ecs_set_interval(ecs_world_t *world, ecs_entity_t tick_source, ecs_ftime_t interval)
Set timer interval.
FLECS_API ecs_ftime_t ecs_get_interval(const ecs_world_t *world, ecs_entity_t tick_source)
Get current interval value for the specified timer.
FLECS_API void ecs_set_tick_source(ecs_world_t *world, ecs_entity_t system, ecs_entity_t tick_source)
Assign tick source to system.
FLECS_API ecs_entity_t ecs_set_rate(ecs_world_t *world, ecs_entity_t tick_source, int32_t rate, ecs_entity_t source)
Set rate filter.
FLECS_API void FlecsTimerImport(ecs_world_t *world)
Timer module import function.
FLECS_API void ecs_start_timer(ecs_world_t *world, ecs_entity_t tick_source)
Start timer.
FLECS_API void ecs_randomize_timers(ecs_world_t *world)
Enable randomizing initial time value of timers.
FLECS_API ecs_entity_t ecs_set_timeout(ecs_world_t *world, ecs_entity_t tick_source, ecs_ftime_t timeout)
Set timer timeout.
FLECS_API void ecs_stop_timer(ecs_world_t *world, ecs_entity_t tick_source)
Stop timer.
ecs_id_t ecs_entity_t
An entity identifier.
Definition flecs.h:394
struct ecs_world_t ecs_world_t
A world is the container for all ECS data and supporting features.
Definition flecs.h:438
#define ecs_ftime_t
Customizable precision for scalar time values.
Definition flecs.h:59
Apply a rate filter to a tick source.
Definition timer.h:45
int32_t rate
Rate of the rate filter.
Definition timer.h:47
int32_t tick_count
Number of times the rate filter ticked.
Definition timer.h:48
ecs_ftime_t time_elapsed
Time elapsed since last tick.
Definition timer.h:49
ecs_entity_t src
Source of the rate filter.
Definition timer.h:46
Component used for one-shot and interval timer functionality.
Definition timer.h:35
ecs_ftime_t time
Incrementing time value.
Definition timer.h:37
bool active
Is the timer active or not.
Definition timer.h:40
bool single_shot
Is this a single-shot timer.
Definition timer.h:41
ecs_ftime_t overshoot
Used to correct returned interval time.
Definition timer.h:38
int32_t fired_count
Number of times ticked.
Definition timer.h:39
ecs_ftime_t timeout
Timer timeout period.
Definition timer.h:36