Skip to content

Commit ae6c1b4

Browse files
committed
Implement set_group() for systems
1 parent 446fbac commit ae6c1b4

File tree

11 files changed

+289
-2
lines changed

11 files changed

+289
-2
lines changed

distr/flecs.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75943,6 +75943,10 @@ ecs_entity_t flecs_run_system(
7594375943
qit.callback_ctx = system_data->callback_ctx;
7594475944
qit.run_ctx = system_data->run_ctx;
7594575945

75946+
if (system_data->group_id_set) {
75947+
ecs_iter_set_group(&qit, system_data->group_id);
75948+
}
75949+
7594675950
if (stage_count > 1 && system_data->multi_threaded) {
7594775951
wit = ecs_worker_iter(it, stage_index, stage_count);
7594875952
it = &wit;
@@ -76275,6 +76279,23 @@ const ecs_system_t* ecs_system_get(
7627576279
return flecs_poly_get(world, entity, ecs_system_t);
7627676280
}
7627776281

76282+
void ecs_system_set_group(
76283+
ecs_world_t *world,
76284+
ecs_entity_t system,
76285+
uint64_t group_id)
76286+
{
76287+
ecs_check(world != NULL, ECS_INVALID_PARAMETER, NULL);
76288+
ecs_check(system != 0, ECS_INVALID_PARAMETER, NULL);
76289+
76290+
ecs_system_t *system_data = flecs_poly_get(world, system, ecs_system_t);
76291+
ecs_check(system_data != NULL, ECS_INVALID_PARAMETER, NULL);
76292+
76293+
system_data->group_id = group_id;
76294+
system_data->group_id_set = true;
76295+
error:
76296+
return;
76297+
}
76298+
7627876299
void FlecsSystemImport(
7627976300
ecs_world_t *world)
7628076301
{

distr/flecs.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13231,6 +13231,12 @@ typedef struct ecs_system_t {
1323113231
/** System query */
1323213232
ecs_query_t *query;
1323313233

13234+
/** Query group to iterate */
13235+
uint64_t group_id;
13236+
13237+
/** True if a query group is configured */
13238+
bool group_id_set;
13239+
1323413240
/** Tick source associated with system */
1323513241
ecs_entity_t tick_source;
1323613242

@@ -13287,6 +13293,21 @@ const ecs_system_t* ecs_system_get(
1328713293
const ecs_world_t *world,
1328813294
ecs_entity_t system);
1328913295

13296+
/** Set query group for system.
13297+
* This operation configures a system created with a grouped query to only
13298+
* iterate results for the specified group id. The group filter is applied to
13299+
* both manual runs and pipeline execution.
13300+
*
13301+
* @param world The world.
13302+
* @param system The system.
13303+
* @param group_id The query group id to iterate.
13304+
*/
13305+
FLECS_API
13306+
void ecs_system_set_group(
13307+
ecs_world_t *world,
13308+
ecs_entity_t system,
13309+
uint64_t group_id);
13310+
1329013311
#ifndef FLECS_LEGACY
1329113312

1329213313
/** Forward declare a system. */
@@ -34477,6 +34498,17 @@ struct system final : entity
3447734498
return flecs::query<>(ecs_system_get(world_, id_)->query);
3447834499
}
3447934500

34501+
system& set_group(uint64_t group_id) {
34502+
ecs_system_set_group(world_, id_, group_id);
34503+
return *this;
34504+
}
34505+
34506+
template <typename Group>
34507+
system& set_group() {
34508+
ecs_system_set_group(world_, id_, _::type<Group>().id(world_));
34509+
return *this;
34510+
}
34511+
3448034512
system_runner_fluent run(ecs_ftime_t delta_time = 0.0f, void *param = nullptr) const {
3448134513
return system_runner_fluent(world_, id_, 0, 0, delta_time, param);
3448234514
}

include/flecs/addons/cpp/mixins/system/impl.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,17 @@ struct system final : entity
9090
return flecs::query<>(ecs_system_get(world_, id_)->query);
9191
}
9292

93+
system& set_group(uint64_t group_id) {
94+
ecs_system_set_group(world_, id_, group_id);
95+
return *this;
96+
}
97+
98+
template <typename Group>
99+
system& set_group() {
100+
ecs_system_set_group(world_, id_, _::type<Group>().id(world_));
101+
return *this;
102+
}
103+
93104
system_runner_fluent run(ecs_ftime_t delta_time = 0.0f, void *param = nullptr) const {
94105
return system_runner_fluent(world_, id_, 0, 0, delta_time, param);
95106
}

include/flecs/addons/system.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@ typedef struct ecs_system_t {
120120
/** System query */
121121
ecs_query_t *query;
122122

123+
/** Query group to iterate */
124+
uint64_t group_id;
125+
126+
/** True if a query group is configured */
127+
bool group_id_set;
128+
123129
/** Tick source associated with system */
124130
ecs_entity_t tick_source;
125131

@@ -176,6 +182,21 @@ const ecs_system_t* ecs_system_get(
176182
const ecs_world_t *world,
177183
ecs_entity_t system);
178184

185+
/** Set query group for system.
186+
* This operation configures a system created with a grouped query to only
187+
* iterate results for the specified group id. The group filter is applied to
188+
* both manual runs and pipeline execution.
189+
*
190+
* @param world The world.
191+
* @param system The system.
192+
* @param group_id The query group id to iterate.
193+
*/
194+
FLECS_API
195+
void ecs_system_set_group(
196+
ecs_world_t *world,
197+
ecs_entity_t system,
198+
uint64_t group_id);
199+
179200
#ifndef FLECS_LEGACY
180201

181202
/** Forward declare a system. */

src/addons/system/system.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ ecs_entity_t flecs_run_system(
9393
qit.callback_ctx = system_data->callback_ctx;
9494
qit.run_ctx = system_data->run_ctx;
9595

96+
if (system_data->group_id_set) {
97+
ecs_iter_set_group(&qit, system_data->group_id);
98+
}
99+
96100
if (stage_count > 1 && system_data->multi_threaded) {
97101
wit = ecs_worker_iter(it, stage_index, stage_count);
98102
it = &wit;
@@ -425,6 +429,23 @@ const ecs_system_t* ecs_system_get(
425429
return flecs_poly_get(world, entity, ecs_system_t);
426430
}
427431

432+
void ecs_system_set_group(
433+
ecs_world_t *world,
434+
ecs_entity_t system,
435+
uint64_t group_id)
436+
{
437+
ecs_check(world != NULL, ECS_INVALID_PARAMETER, NULL);
438+
ecs_check(system != 0, ECS_INVALID_PARAMETER, NULL);
439+
440+
ecs_system_t *system_data = flecs_poly_get(world, system, ecs_system_t);
441+
ecs_check(system_data != NULL, ECS_INVALID_PARAMETER, NULL);
442+
443+
system_data->group_id = group_id;
444+
system_data->group_id_set = true;
445+
error:
446+
return;
447+
}
448+
428449
void FlecsSystemImport(
429450
ecs_world_t *world)
430451
{

test/addons/project.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@
189189
"register_run_after_callback",
190190
"register_callback_after_run_ctx",
191191
"register_run_after_callback_ctx",
192+
"set_group",
192193
"run_w_query_next",
193194
"missing_callback"
194195
]

test/addons/src/SystemMisc.c

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1806,6 +1806,91 @@ static void Run_w_query_next(ecs_iter_t *it) {
18061806
run_invoked ++;
18071807
}
18081808

1809+
static
1810+
uint64_t SystemMisc_group_by_rel(
1811+
ecs_world_t *world,
1812+
ecs_table_t *table,
1813+
ecs_id_t id,
1814+
void *ctx)
1815+
{
1816+
(void)ctx;
1817+
1818+
ecs_id_t match;
1819+
if (ecs_search(world, table, ecs_pair(id, EcsWildcard), &match) != -1) {
1820+
return ECS_PAIR_SECOND(match);
1821+
}
1822+
1823+
return 0;
1824+
}
1825+
1826+
static
1827+
uint64_t system_misc_expected_group;
1828+
1829+
static
1830+
void SystemMisc_grouped_dummy(ecs_iter_t *it) {
1831+
test_uint(ecs_iter_get_group(it), system_misc_expected_group);
1832+
probe_iter(it);
1833+
}
1834+
1835+
static
1836+
void SystemMisc_reset_probe(Probe *ctx) {
1837+
ecs_os_zeromem(ctx);
1838+
}
1839+
1840+
void SystemMisc_set_group(void) {
1841+
ecs_world_t *world = ecs_init();
1842+
1843+
ecs_entity_t Rel = ecs_entity(world, {0});
1844+
ecs_entity_t TgtA = ecs_entity(world, {0});
1845+
ecs_entity_t TgtB = ecs_entity(world, {0});
1846+
ecs_entity_t TgtC = ecs_entity(world, {0});
1847+
ECS_TAG(world, Tag);
1848+
1849+
ecs_entity_t e1 = ecs_new_w_pair(world, Rel, TgtA);
1850+
ecs_entity_t e2 = ecs_new_w_pair(world, Rel, TgtB);
1851+
ecs_new_w_pair(world, Rel, TgtC);
1852+
ecs_entity_t e4 = ecs_new_w_pair(world, Rel, TgtA);
1853+
ecs_entity_t e5 = ecs_new_w_pair(world, Rel, TgtB);
1854+
ecs_entity_t e6 = ecs_new_w_pair(world, Rel, TgtC);
1855+
1856+
ecs_add(world, e4, Tag);
1857+
ecs_add(world, e5, Tag);
1858+
ecs_add(world, e6, Tag);
1859+
1860+
Probe ctx = {0};
1861+
ecs_set_ctx(world, &ctx, NULL);
1862+
1863+
ecs_entity_t system = ecs_system_init(world, &(ecs_system_desc_t){
1864+
.query.terms = {{ .id = ecs_pair(Rel, EcsWildcard) }},
1865+
.query.group_by = Rel,
1866+
.query.group_by_callback = SystemMisc_group_by_rel,
1867+
.callback = SystemMisc_grouped_dummy
1868+
});
1869+
test_assert(system != 0);
1870+
1871+
system_misc_expected_group = TgtB;
1872+
ecs_system_set_group(world, system, TgtB);
1873+
ecs_run(world, system, 0, NULL);
1874+
1875+
test_int(ctx.invoked, 2);
1876+
test_int(ctx.count, 2);
1877+
probe_has_entity(&ctx, e2);
1878+
probe_has_entity(&ctx, e5);
1879+
1880+
SystemMisc_reset_probe(&ctx);
1881+
1882+
system_misc_expected_group = TgtA;
1883+
ecs_system_set_group(world, system, TgtA);
1884+
ecs_run(world, system, 0, NULL);
1885+
1886+
test_int(ctx.invoked, 2);
1887+
test_int(ctx.count, 2);
1888+
probe_has_entity(&ctx, e1);
1889+
probe_has_entity(&ctx, e4);
1890+
1891+
ecs_fini(world);
1892+
}
1893+
18091894
void SystemMisc_run_w_query_next(void) {
18101895
ecs_world_t *world = ecs_init();
18111896

test/addons/src/main.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ void SystemMisc_register_callback_after_run(void);
181181
void SystemMisc_register_run_after_callback(void);
182182
void SystemMisc_register_callback_after_run_ctx(void);
183183
void SystemMisc_register_run_after_callback_ctx(void);
184+
void SystemMisc_set_group(void);
184185
void SystemMisc_run_w_query_next(void);
185186
void SystemMisc_missing_callback(void);
186187

@@ -1245,6 +1246,10 @@ bake_test_case SystemMisc_testcases[] = {
12451246
"register_run_after_callback_ctx",
12461247
SystemMisc_register_run_after_callback_ctx
12471248
},
1249+
{
1250+
"set_group",
1251+
SystemMisc_set_group
1252+
},
12481253
{
12491254
"run_w_query_next",
12501255
SystemMisc_run_w_query_next
@@ -2663,6 +2668,7 @@ const char* MultiThread_worker_kind_param[] = {"thread", "task"};
26632668
bake_test_param MultiThread_params[] = {
26642669
{"worker_kind", (char**)MultiThread_worker_kind_param, 2}
26652670
};
2671+
26662672
const char* MultiThreadStaging_worker_kind_param[] = {"thread", "task"};
26672673
bake_test_param MultiThreadStaging_params[] = {
26682674
{"worker_kind", (char**)MultiThreadStaging_worker_kind_param, 2}
@@ -2687,7 +2693,7 @@ static bake_test_suite suites[] = {
26872693
"SystemMisc",
26882694
NULL,
26892695
NULL,
2690-
71,
2696+
72,
26912697
SystemMisc_testcases
26922698
},
26932699
{

test/cpp/project.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,7 @@
675675
"register_twice_w_run",
676676
"register_twice_w_run_each",
677677
"register_twice_w_each_run",
678+
"set_group",
678679
"run_w_0_src_query"
679680
]
680681
}, {

0 commit comments

Comments
 (0)