Skip to content

Commit 8e1df17

Browse files
committed
fix iter : clear cache when moving to next table
1 parent cf4c4b9 commit 8e1df17

2 files changed

Lines changed: 17 additions & 17 deletions

File tree

examples/src/main/java/com/github/elebras1/flecs/examples/DirectFieldAccessExample.java

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,19 @@ public static void main(String[] args) {
1212
world.component(Position.class);
1313
world.component(Velocity.class);
1414
world.component(Health.class);
15+
16+
long tagEnemy = world.entity("Enemy");;
1517

1618
// Create entities
1719
for (int i = 0; i < 3; i++) {
20+
long uniqueTag = world.entity("UniqueTag_" + i);
1821
world.obtainEntity(world.entity("Entity_" + i))
1922
.set(new Position(i * 10.0f, i * 5.0f))
2023
.set(new Velocity(1.0f + i, 0.5f + i))
21-
.set(new Health(100 - i * 10));
24+
.set(new Health(100 - i * 10)).add(tagEnemy).add(uniqueTag);
2225
}
2326

24-
try (Query query = world.query()
25-
.with(Position.class)
26-
.with(Velocity.class)
27-
.with(Health.class)
28-
.build()) {
29-
30-
System.out.println("Traditional access (with object allocation):");
31-
query.iter(iter -> {
32-
Field<Position> positions = iter.field(Position.class, 0);
33-
Field<Health> healths = iter.field(Health.class, 2);
34-
for (int i = 0; i < iter.count(); i++) {
35-
Position pos = positions.get(i);
36-
Health health = healths.get(i);
37-
System.out.printf(" pos=(%.1f, %.1f) health=%d%n", pos.x(), pos.y(), health.value());
38-
}
39-
});
27+
try (Query query = world.query().with(Position.class).with(Velocity.class).with(Health.class).build()) {
4028

4129
System.out.println("\nDirect access (no allocation):");
4230
query.iter(iter -> {
@@ -57,6 +45,17 @@ public static void main(String[] args) {
5745
}
5846
});
5947
}
48+
49+
50+
try (Query query = world.query().with(tagEnemy).with(Health.class).build()) {
51+
System.out.println("\nDirect access with tag:");
52+
query.iter(iter -> {
53+
for (int i = 0; i < iter.count(); i++) {
54+
int health = iter.fieldInt(Health.class, 1, "value", i);
55+
System.out.println("health=" + health);
56+
}
57+
});
58+
}
6059
}
6160
}
6261
}

src/main/java/com/github/elebras1/flecs/Query.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public void iter(IterCallback callback) {
6565
Iter it = new Iter(iter, this.world);
6666

6767
while (flecs_h.ecs_iter_next(iter)) {
68+
it.setNativeIter(iter);
6869
callback.accept(it);
6970
}
7071
}

0 commit comments

Comments
 (0)