Skip to content

Commit 0b0631f

Browse files
committed
Randomize time value for systems with interval to reduce spiking behavior
1 parent aec0305 commit 0b0631f

File tree

5 files changed

+13
-3
lines changed

5 files changed

+13
-3
lines changed

flecs.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35945,7 +35945,8 @@ ecs_entity_t ecs_set_interval(
3594535945

3594635946
timer = ecs_set(world, timer, EcsTimer, {
3594735947
.timeout = interval,
35948-
.active = true
35948+
.active = true,
35949+
.time = ((ecs_ftime_t)rand() / (ecs_ftime_t)RAND_MAX) * interval
3594935950
});
3595035951

3595135952
ecs_system_t *system_data = ecs_poly_get(world, timer, ecs_system_t);

flecs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,8 @@ extern "C" {
565565
#pragma clang diagnostic ignored "-Wenum-constexpr-conversion"
566566
/* Very difficult to workaround this warning in C, especially for an ECS. */
567567
#pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
568+
/* This warning gets thrown when trying to cast pointer returned from dlproc */
569+
#pragma clang diagnostic ignored "-Wcast-function-type-strict"
568570
#elif defined(ECS_TARGET_GNU)
569571
#ifndef __cplusplus
570572
#pragma GCC diagnostic ignored "-Wdeclaration-after-statement"

src/addons/timer.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,8 @@ ecs_entity_t ecs_set_interval(
153153

154154
timer = ecs_set(world, timer, EcsTimer, {
155155
.timeout = interval,
156-
.active = true
156+
.active = true,
157+
.time = ((ecs_ftime_t)rand() / (ecs_ftime_t)RAND_MAX) * interval
157158
});
158159

159160
ecs_system_t *system_data = ecs_poly_get(world, timer, ecs_system_t);

test/custom_builds/c/timer/src/main.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ int main(int argc, char *argv[]) {
2828
});
2929
assert(s != 0);
3030

31+
EcsTimer *timer = ecs_get_mut(world, s, EcsTimer);
32+
timer->time = 0;
33+
3134
ecs_entity_t e = ecs_new_id(world);
3235
ecs_set(world, e, Position, {10, 20});
3336
ecs_set(world, e, Velocity, {1, 2});

test/custom_builds/cpp/timer/src/main.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@ struct Velocity {
1212
int main(int, char *[]) {
1313
flecs::world ecs;
1414

15-
ecs.system<Position, const Velocity>()
15+
flecs::entity s = ecs.system<Position, const Velocity>()
1616
.interval(2.9f)
1717
.each([](Position& p, const Velocity& v) {
1818
p.x += v.x;
1919
p.y += v.y;
2020
});
2121

22+
flecs::Timer *t = s.get_mut<flecs::Timer>();
23+
t->time = 0;
24+
2225
auto e = ecs.entity()
2326
.set<Position>({10, 20})
2427
.set<Velocity>({1, 2});

0 commit comments

Comments
 (0)