Skip to content

Commit

Permalink
Randomize time value for systems with interval to reduce spiking beha…
Browse files Browse the repository at this point in the history
…vior
  • Loading branch information
SanderMertens committed Aug 29, 2023
1 parent aec0305 commit c4b01f9
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 3 deletions.
3 changes: 2 additions & 1 deletion flecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -35945,7 +35945,8 @@ ecs_entity_t ecs_set_interval(

timer = ecs_set(world, timer, EcsTimer, {
.timeout = interval,
.active = true
.active = true,
.time = ((ecs_ftime_t)rand() / (ecs_ftime_t)RAND_MAX) * interval
});

ecs_system_t *system_data = ecs_poly_get(world, timer, ecs_system_t);
Expand Down
2 changes: 2 additions & 0 deletions flecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,8 @@ extern "C" {
#pragma clang diagnostic ignored "-Wenum-constexpr-conversion"
/* Very difficult to workaround this warning in C, especially for an ECS. */
#pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
/* This warning gets thrown when trying to cast pointer returned from dlproc */
#pragma clang diagnostic ignored "-Wcast-function-type-strict"
#elif defined(ECS_TARGET_GNU)
#ifndef __cplusplus
#pragma GCC diagnostic ignored "-Wdeclaration-after-statement"
Expand Down
3 changes: 2 additions & 1 deletion src/addons/timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ ecs_entity_t ecs_set_interval(

timer = ecs_set(world, timer, EcsTimer, {
.timeout = interval,
.active = true
.active = true,
.time = ((ecs_ftime_t)rand() / (ecs_ftime_t)RAND_MAX) * interval
});

ecs_system_t *system_data = ecs_poly_get(world, timer, ecs_system_t);
Expand Down
3 changes: 3 additions & 0 deletions test/cpp_api/src/System.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2043,6 +2043,9 @@ void System_interval_tick_source(void) {

flecs::timer t = ecs.timer().interval(2.1);

flecs::Timer *timer = t.get_mut<flecs::Timer>();
timer->time = 0;

int32_t sys_a_invoked = 0, sys_b_invoked = 0;

ecs.system()
Expand Down
3 changes: 3 additions & 0 deletions test/custom_builds/c/timer/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ int main(int argc, char *argv[]) {
});
assert(s != 0);

EcsTimer *timer = ecs_get_mut(world, s, EcsTimer);
timer->time = 0;

ecs_entity_t e = ecs_new_id(world);
ecs_set(world, e, Position, {10, 20});
ecs_set(world, e, Velocity, {1, 2});
Expand Down
5 changes: 4 additions & 1 deletion test/custom_builds/cpp/timer/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ struct Velocity {
int main(int, char *[]) {
flecs::world ecs;

ecs.system<Position, const Velocity>()
flecs::entity s = ecs.system<Position, const Velocity>()
.interval(2.9f)
.each([](Position& p, const Velocity& v) {
p.x += v.x;
p.y += v.y;
});

flecs::Timer *t = s.get_mut<flecs::Timer>();
t->time = 0;

auto e = ecs.entity()
.set<Position>({10, 20})
.set<Velocity>({1, 2});
Expand Down

0 comments on commit c4b01f9

Please sign in to comment.