Update example in README

wip/bindgen
copygirl 2 years ago
parent 127e957c97
commit b6af74511f
  1. 24
      README.md

@ -48,8 +48,16 @@ var position = world
// The "Position" struct is defined at the bottom of this example.
.InitComponent<Position>();
world.New("One").Set(new Position( 0, 0)).Build();
world.New("Two").Set(new Position(10, 20)).Build();
// Create an "Entities" parent with two positioned entities inside.
var entities = world.New("Entities").Build();
entities.NewChild("One").Set(new Position(-5, 0)).Build();
entities.NewChild("Two").Set(new Position(10, 20)).Build();
// Changed my mind: Let's multiply each entity's position by 10.
foreach (var child in entities.GetChildren()) {
ref var pos = ref child.GetRefOrThrow<Position>();
pos = new(pos.X * 10, pos.Y * 10);
}
var onUpdate = world.LookupOrThrow("/flecs/pipeline/OnUpdate");
@ -57,21 +65,21 @@ var onUpdate = world.LookupOrThrow("/flecs/pipeline/OnUpdate");
// the "Position" component downwards by 2 every frame.
world.New("FallSystem").Build()
.InitSystem(onUpdate, new("Position"), iter => {
var positionSpan = iter.Field<Position>(1);
var posColumn = iter.Field<Position>(1);
for (var i = 0; i < iter.Count; i++) {
ref var position = ref positionSpan[i];
position = new(position.X, position.Y + 2);
ref var pos = ref posColumn[i];
pos = new(pos.X, pos.Y + 2);
}
});
// Create a system that will print out entities' positions.
world.New("PrintPositionSystem").Build()
.InitSystem(onUpdate, new("[in] Position"), iter => {
var positionSpan = iter.Field<Position>(1);
var posColumn = iter.Field<Position>(1);
for (var i = 0; i < iter.Count; i++) {
var entity = iter.Entity(i);
var position = positionSpan[i];
Console.WriteLine($"{entity.Name} is at {position}");
var pos = posColumn[i];
Console.WriteLine($"{entity.Name} is at {pos}");
}
});

Loading…
Cancel
Save