11package com .github .elebras1 .flecs .examples ;
22
33import com .github .elebras1 .flecs .Entity ;
4+ import com .github .elebras1 .flecs .EntityView ;
45import com .github .elebras1 .flecs .World ;
56import com .github .elebras1 .flecs .examples .components .*;
67import com .github .elebras1 .flecs .util .FlecsConstants ;
@@ -13,24 +14,27 @@ public static void main(String[] args) {
1314 world .component (Velocity .class );
1415 world .component (Inventory .class );
1516
16- for (int i = 0 ; i < 10 ; i ++) {
17- long entityId = world .entity ("Entity_" + i );
18- Entity entity = world .obtainEntity (entityId );
19- entity .set (new Position (i * 10.0f , i * 5.0f ));
20- entity .set (new Velocity (1.0f , 0.5f ));
21- int [] elements = new int [10 ];
22- for (int j = 0 ; j < elements .length ; j ++) {
23- elements [j ] = j ;
17+ // obtainEntityView() requires an active scope to function properly.
18+ world .scope (() -> {
19+ for (int i = 0 ; i < 10 ; i ++) {
20+ long entityId = world .entity ("Entity_" + i );
21+ EntityView entityView = world .obtainEntityView (entityId );
22+ entityView .set (new Position (i * 10.0f , i * 5.0f ));
23+ entityView .set (new Velocity (1.0f , 0.5f ));
24+ int [] elements = new int [10 ];
25+ for (int j = 0 ; j < elements .length ; j ++) {
26+ elements [j ] = j ;
27+ }
28+ entityView .set (new Inventory (elements ));
2429 }
25- entity .set (new Inventory (elements ));
26- }
30+ });
2731
2832 world .system ("MovementSystem" ).kind (FlecsConstants .EcsOnUpdate ).with (Position .class ).with (Velocity .class ).multiThreaded ().each (entityId -> {
29- Entity entity = world .obtainEntity (entityId );
33+ EntityView entityView = world .obtainEntityView (entityId );
3034
31- PositionView positionView = entity .getMutView (Position .class );
32- VelocityView velocityView = entity .getMutView (Velocity .class );
33- InventoryView inventoryView = entity .getMutView (Inventory .class );
35+ PositionView positionView = entityView .getMutView (Position .class );
36+ VelocityView velocityView = entityView .getMutView (Velocity .class );
37+ InventoryView inventoryView = entityView .getMutView (Inventory .class );
3438
3539 float currentX = positionView .x ();
3640 float currentY = positionView .y ();
0 commit comments