Skip to content

Commit

Permalink
Change field index to start from 0
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderMertens committed Apr 5, 2024
1 parent c9b77cb commit bb8ffca
Show file tree
Hide file tree
Showing 134 changed files with 9,904 additions and 9,855 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ typedef struct {
} Position, Velocity;

void Move(ecs_iter_t *it) {
Position *p = ecs_field(it, Position, 1);
Velocity *v = ecs_field(it, Velocity, 2);
Position *p = ecs_field(it, Position, 0);
Velocity *v = ecs_field(it, Velocity, 1);

for (int i = 0; i < it->count; i++) {
p[i].x += v[i].x;
Expand Down
6 changes: 3 additions & 3 deletions docs/Manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ typedef struct Velocity {
// System names ('Move') use PascalCase. API types use snake_case_t
void Move(ecs_iter_t *it) {
// Functions use snake_case
Position *p = ecs_field(it, Position, 1);
Velocity *v = ecs_field(it, Velocity, 2);
Position *p = ecs_field(it, Position, 0);
Velocity *v = ecs_field(it, Velocity, 1);

for (int i = 0; i < it->count; i++) {
p[i].x += v[i].x;
Expand Down Expand Up @@ -710,7 +710,7 @@ The implementation of the observer looks similar to a system:
```c
void AddPosition(ecs_iter_t *it) {
Position *p = ecs_field(it, Position, 1);
Position *p = ecs_field(it, Position, 0);
for (int i = 0; i < it->count; i++) {
p[i].x = 10;
Expand Down
90 changes: 45 additions & 45 deletions docs/Queries.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ Flecs has different query types, which are optimized for different kinds of use

ecs_iter_t it = ecs_query_iter(world, f);
while (ecs_query_next(&it)) {
Position *p = ecs_field(&it, Position, 1);
Velocity *v = ecs_field(&it, Velocity, 2);
Position *p = ecs_field(&it, Position, 0);
Velocity *v = ecs_field(&it, Velocity, 1);

for (int i = 0; i < it.count; i ++) {
p[i].x += v[i].x;
Expand Down Expand Up @@ -98,8 +98,8 @@ ecs_query_t *q = ecs_query(world, {

ecs_iter_t it = ecs_query_iter(world, q);
while (ecs_query_next(&it)) {
Position *p = ecs_field(&it, Position, 1);
Velocity *v = ecs_field(&it, Velocity, 2);
Position *p = ecs_field(&it, Position, 0);
Velocity *v = ecs_field(&it, Velocity, 1);

for (int i = 0; i < it.count; i ++) {
p[i].x += v[i].x;
Expand Down Expand Up @@ -129,8 +129,8 @@ ecs_query_impl_t *r = ecs_query(world, {

ecs_iter_t it = ecs_query_iter(world, r);
while (ecs_query_next(&it)) {
Position *p = ecs_field(&it, Position, 1);
Velocity *v = ecs_field(&it, Velocity, 2);
Position *p = ecs_field(&it, Position, 0);
Velocity *v = ecs_field(&it, Velocity, 1);

for (int i = 0; i < it.count; i ++) {
p[i].x += v[i].x;
Expand Down Expand Up @@ -287,8 +287,8 @@ ecs_iter_t it = ecs_query_iter(world, f);

// Outer loop: matching tables
while (ecs_query_next(&it)) {
Position *p = ecs_field(&it, Position, 1); // 1st term
Velocity *v = ecs_field(&it, Velocity, 2); // 2nd term
Position *p = ecs_field(&it, Position, 0); // 1st term
Velocity *v = ecs_field(&it, Velocity, 1); // 2nd term

// Inner loop: entities in table
for (int i = 0; i < it.count; i ++) {
Expand Down Expand Up @@ -336,7 +336,7 @@ auto f = world.query_builder<Position>()
f.each([](flecs::iter& it, size_t index, Position& p) {
flecs::entity e = it.entity(index);
std::cout << e.name() << ": "
<< it.id(2).str() // prints pair
<< it.id(1).str() // prints pair
<< std::endl;
});
```
Expand Down Expand Up @@ -404,8 +404,8 @@ The component arguments may be omitted, and can be obtained from the iterator ob
auto f = world.query<Position, const Velocity>();

f.iter([](flecs::iter& it) {
auto p = it.field<Position>(1);
auto v = it.field<const Velocity>(2);
auto p = it.field<Position>(0);
auto v = it.field<const Velocity>(1);

for (auto i : it) {
p[i].x += v[i].x;
Expand All @@ -420,8 +420,8 @@ This can be combined with an untyped variant of the `field` method to access com
auto f = world.query<Position, const Velocity>();

f.iter([](flecs::iter& it) {
void *ptr = it.field(1);
flecs::id type_id = it.id(1);
void *ptr = it.field(0);
flecs::id type_id = it.id(0);

// ...
});
Expand Down Expand Up @@ -810,7 +810,7 @@ ecs_query_t *f = ecs_query(world, {
ecs_iter_t it = ecs_query_iter(world, f);
while (ecs_query_next(&it)) {
ecs_id_t id = ecs_field_id(&it, 1);
ecs_id_t id = ecs_field_id(&it, 0);
ecs_entity_t second = ecs_pair_second(world, id);
for (int i = 0; i < it.count; i ++) {
Expand Down Expand Up @@ -899,7 +899,7 @@ flecs::query<> f = world.query_builder()
.build();

f.each([](flecs::iter& it, size_t index) {
flecs::entity second = it.pair(1).second();
flecs::entity second = it.pair(0).second();
flecs::entity e = it.entity(index);

std::cout << "entity " << e.name()
Expand Down Expand Up @@ -1011,10 +1011,10 @@ flecs::query<> f = world.query_builder()
.build();

f.iter([](flecs::iter& it) {
auto p = it.field<Position>(1); // OK
auto p = it.field<const Position>(1); // OK
auto v = it.field<const Velocity>(2); // OK
auto v = it.field<Velocity>(2); // Throws assert
auto p = it.field<Position>(0); // OK
auto p = it.field<const Position>(0); // OK
auto v = it.field<const Velocity>(1); // OK
auto v = it.field<Velocity>(1); // Throws assert
});
```

Expand Down Expand Up @@ -1140,10 +1140,10 @@ ecs_query_t *f = ecs_query(world, {

ecs_iter_t it = ecs_query_iter(world, f);
while (ecs_query_next(&it)) {
Position *p = ecs_field(&it, Position, 1);
Mass *m = ecs_field(&it, Mass, 3); // not 4, because of the Or expression
Position *p = ecs_field(&it, Position, 0);
Mass *m = ecs_field(&it, Mass, 2); // not 4, because of the Or expression

ecs_id_t vs_id = ecs_field_id(&it, 2);
ecs_id_t vs_id = ecs_field_id(&it, 1);
if (vs_id == ecs_id(Velocity)) {
// We can only use ecs_field if the field type is the same for all results,
// but we can get the table column directly.
Expand All @@ -1169,10 +1169,10 @@ flecs::query<> f = world.query_builder()
.build();

f.iter([&](flecs::iter& it) {
auto p = it.field<Position>(1);
auto v = it.field<Mass>(3); // not 4, because of the Or expression
auto p = it.field<Position>(0);
auto v = it.field<Mass>(2); // not 4, because of the Or expression

flecs::id vs_id = it.id(2);
flecs::id vs_id = it.id(1);
if (vs_id == world.id<Velocity>()) {
// We can only use ecs_field if the field type is the same for all results,
// but we can use range() to get the table column directly.
Expand Down Expand Up @@ -1280,9 +1280,9 @@ ecs_query_t *f = ecs_query(world, {

ecs_iter_t it = ecs_query_iter(world, f);
while (ecs_query_next(&it)) {
Position *p = ecs_field(&it, Position, 1);
if (ecs_field_is_set(&it, 2)) {
Velocity *v = ecs_field(&it, Velocity, 2);
Position *p = ecs_field(&it, Position, 0);
if (ecs_field_is_set(&it, 1)) {
Velocity *v = ecs_field(&it, Velocity, 1);
// iterate as usual
} else {
// iterate as usual
Expand All @@ -1300,10 +1300,10 @@ flecs::query<> f = world.query_builder()
.build();

f.iter([&](flecs::iter& it) {
auto p = it.field<Position>(1);
auto p = it.field<Position>(0);

if (it.is_set(2)) {
auto v = it.field<Velocity>(2);
if (it.is_set(1)) {
auto v = it.field<Velocity>(1);
// iterate as usual
} else if (vs_id == world.id<Speed>()) {
// iterate as usual
Expand Down Expand Up @@ -1559,9 +1559,9 @@ ecs_query_t *f = ecs_query(world, {

ecs_iter_t it = ecs_query_iter(world, f);
while (ecs_query_next(&it)) {
Position *p = ecs_field(&it, Position, 1);
Velocity *v = ecs_field(&it, Velocity, 2);
SimTime *st = ecs_field(&it, SimTime, 3);
Position *p = ecs_field(&it, Position, 0);
Velocity *v = ecs_field(&it, Velocity, 1);
SimTime *st = ecs_field(&it, SimTime, 2);

for (int i = 0; i < it.count; i ++) {
p[i].x += v[i].x * st[i].value;
Expand Down Expand Up @@ -1598,8 +1598,8 @@ ecs_query_t *f = ecs_query(world, {
});

while (ecs_query_next(&it)) {
ecs_entity_t src_1 = ecs_field_src(&it, 1); // Returns 0, meaning entity is stored in it.entities
ecs_entity_t src_2 = ecs_field_src(&it, 2); // Returns Game
ecs_entity_t src_1 = ecs_field_src(&it, 0); // Returns 0, meaning entity is stored in it.entities
ecs_entity_t src_2 = ecs_field_src(&it, 1); // Returns Game

for (int i = 0; i < it.count; i ++) {
printf("$This = %s, src_2 = %s\n",
Expand All @@ -1625,9 +1625,9 @@ flecs::query<> f = world.query_builder()
.build();

f.iter([](flecs::iter& it) {
auto p = it.field<Position>(1);
auto v = it.field<Velocity>(2);
auto st = it.field<SimTime>(3);
auto p = it.field<Position>(0);
auto v = it.field<Velocity>(1);
auto st = it.field<SimTime>(2);

for (auto i : it) {
p[i].x += v[i].x * st[i].value;
Expand Down Expand Up @@ -2209,10 +2209,10 @@ ecs_query_t *f = ecs_query(world, {
ecs_iter_t it = ecs_query_iter(world, &it);
while (ecs_query_next(&it)) {
// Fetch components as usual
Position *p = ecs_field(&it, Position, 1);
Mass *m = ecs_field(&it, Mass, 2);
Position *p = ecs_field(&it, Position, 0);
Mass *m = ecs_field(&it, Mass, 1);

if (ecs_field_is_self(&it, 2)) {
if (ecs_field_is_self(&it, 1)) {
// Mass is matched on self, access as array
for (int i = 0; i < it.count; i ++) {
p[i].x += 1.0 / m[i].value;
Expand Down Expand Up @@ -2243,7 +2243,7 @@ flecs::query<Position, Mass> f = world.query_builder<Position, Mass>()
.build();

f.iter([](flecs::iter& it, Position *p, Mass *v) {
if (it.is_self(2)) {
if (it.is_self(1)) {
// Mass is matched on self, access as array
for (auto i : it) {
p[i].x += 1.0 / m[i].value;
Expand Down Expand Up @@ -3074,8 +3074,8 @@ ecs_add(world, e, Velocity);
// Iterate the tables in the cache
ecs_iter_t it = ecs_query_iter(world, q);
while (ecs_query_next(&it)) {
Position *p = ecs_field(&it, Position, 1);
Velocity *v = ecs_field(&it, Velocity, 2);
Position *p = ecs_field(&it, Position, 0);
Velocity *v = ecs_field(&it, Velocity, 1);

for (int i = 0; i < it.count; i ++) {
p[i].x += v[i].x;
Expand Down
10 changes: 5 additions & 5 deletions docs/Quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -795,8 +795,8 @@ ecs_query_t *q = ecs_query(world, {

ecs_iter_t it = ecs_query_iter(world, q);
while (ecs_query_next(&it)) {
Position *p = ecs_field(&it, Position, 1);
Position *p_parent = ecs_field(&it, Position, 2);
Position *p = ecs_field(&it, Position, 0);
Position *p_parent = ecs_field(&it, Position, 1);
for (int i = 0; i < it.count; i++) {
// Do the thing
}
Expand Down Expand Up @@ -1116,7 +1116,7 @@ ecs_query_t *f = ecs_query_init(world, &(ecs_query_desc_t){
ecs_iter_t it = ecs_query_iter(world, f);
while (ecs_query_next(&it)) {
// Each type has its own set of component arrays
Position *p = ecs_field(&it, Position, 1);
Position *p = ecs_field(&it, Position, 0);

// Iterate all entities for the type
for (int i = 0; i < it.count; i++) {
Expand Down Expand Up @@ -1307,8 +1307,8 @@ ecs_run(world, move_sys, delta_time, NULL); // Run system

// The callback code (same for both options)
void Move(ecs_iter_t *it) {
Position *p = ecs_field(it, Position, 1);
Velocity *v = ecs_field(it, Velocity, 2);
Position *p = ecs_field(it, Position, 0);
Velocity *v = ecs_field(it, Velocity, 1);

for (int i = 0; i < it->count; i++) {
p[i].x += v[i].x * it->delta_time;
Expand Down
8 changes: 4 additions & 4 deletions docs/Relationships.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ ecs_query_t *f = ecs_query(world, {

ecs_iter_t it = ecs_query_iter(world, f);
while (ecs_query_next(&it)) {
ecs_id_t pair_id = ecs_field_id(&it, 1);
ecs_id_t pair_id = ecs_field_id(&it, 0);
ecs_entity_t food = ecs_pair_second(world, pair_id); // Apples, ...
for (int i = 0; i < it.count; i ++) {
// Iterate as usual
Expand All @@ -232,7 +232,7 @@ world.query_builder()
.term(Eats, flecs::Wildcard)
.build()
.each([](flecs::iter& it, size_t i) {
flecs::entity food = it.pair(1).second(); // Apples, ...
flecs::entity food = it.pair(0).second(); // Apples, ...
flecs::entity e = it.entity(i);
// Iterate as usual
});
Expand Down Expand Up @@ -386,7 +386,7 @@ ecs_query_t *q = ecs_query(world, {
ecs_iter_t it = ecs_query_iter(world, q);

while (ecs_query_next(&it)) {
ecs_id_t id = ecs_field_id(&it, 1); // Obtain pair id
ecs_id_t id = ecs_field_id(&it, 0); // Obtain pair id

// Get relationship & target
ecs_entity_t rel = ecs_pair_first(world, id);
Expand All @@ -406,7 +406,7 @@ auto q = world.query_builder()
.build();

q.iter([](flecs::iter& it) {
auto id = it.pair(1);
auto id = it.pair(0);

for (auto i : it) {
cout << "entity " << it.entity(i) << " has relationship "
Expand Down
18 changes: 9 additions & 9 deletions docs/Systems.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ An example of a simple system:
// System implementation
void Move(ecs_iter_t *it) {
// Get fields from system query
Position *p = ecs_field(it, Position, 1);
Velocity *v = ecs_field(it, Velocity, 2);
Position *p = ecs_field(it, Position, 0);
Velocity *v = ecs_field(it, Velocity, 1);

// Iterate matched entities
for (int i = 0; i < it->count, i++) {
Expand Down Expand Up @@ -161,8 +161,8 @@ ecs_iter_t it = ecs_query_iter(world, q);
// Iterate tables matched by query
while (ecs_query_next(&it)) {
// Get fields from query
Position *p = ecs_field(it, Position, 1);
Velocity *v = ecs_field(it, Velocity, 2);
Position *p = ecs_field(it, Position, 0);
Velocity *v = ecs_field(it, Velocity, 1);

// Iterate matched entities
for (int i = 0; i < it->count, i++) {
Expand All @@ -174,8 +174,8 @@ while (ecs_query_next(&it)) {
// System code
void Move(ecs_iter_t *it) {
// Get fields from system query
Position *p = ecs_field(it, Position, 1);
Velocity *v = ecs_field(it, Velocity, 2);
Position *p = ecs_field(it, Position, 0);
Velocity *v = ecs_field(it, Velocity, 1);

// Iterate matched entities
for (int i = 0; i < it->count, i++) {
Expand Down Expand Up @@ -265,8 +265,8 @@ A system provides a `delta_time` which contains the time passed since the last f
<li><b class="tab-title">C</b>

```c
Position *p = ecs_field(it, Position, 1);
Velocity *v = ecs_field(it, Velocity, 2);
Position *p = ecs_field(it, Position, 0);
Velocity *v = ecs_field(it, Velocity, 1);

for (int i = 0; i < it->count, i++) {
p[i].x += v[i].x * it->delta_time;
Expand Down Expand Up @@ -433,7 +433,7 @@ Tasks may query for components from a fixed source or singleton:
// System function
void PrintTime(ecs_iter_t *it) {
// Get singleton component
Game *g = ecs_field(it, Game, 1);
Game *g = ecs_field(it, Game, 0);

printf("Time: %f\n", g->time);
}
Expand Down
2 changes: 1 addition & 1 deletion examples/c/entities/basics/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ int main(int argc, char *argv[]) {
// Iterate all entities with Position
ecs_iter_t it = ecs_each(ecs, Position);
while (ecs_each_next(&it)) {
Position *p = ecs_field(&it, Position, 1);
Position *p = ecs_field(&it, Position, 0);
for (int i = 0; i < it.count; i ++) {
printf("%s: {%f, %f}\n", ecs_get_name(ecs, it.entities[i]),
p[i].x, p[i].y);
Expand Down
Loading

0 comments on commit bb8ffca

Please sign in to comment.