-
-
Notifications
You must be signed in to change notification settings - Fork 98
Utility Features
genar edited this page Mar 5, 2023
·
15 revisions
In some cases, you may want to reference entities among themselves.
Instead of an Entity
you should refer to an EntityReference
instead. This references an entity and also stores a version to check if the original entity still exists.
using var world = World.Create();
var entity = world.Create(_entityGroup);
var reference = world.Reference(entity); // This should be stored in your component ( entity.Reference() is also available. )
// Checks if the entity is the same as before by comparing the version and its id
if(reference.IsAlive()) {
var cmps = reference.Entity.Get(...);
}
Entity creation, deletion, and structural changes can happen during a query or entity iteration.
Sometimes, however, it makes sense to record operations and later transfer them to the world.
For this reason, command buffers exist.
var entity = world.Create<Transform, Rotation, int>(); // Entity in world.
var bufferedEntity = commandBuffer.Create(new ComponentType[] {typeof(Transform), typeof(Rotation), typeof(int) }); // Later also generic overloads to get rid of arrays.
// Records operations on the existing entity.
commandBuffer.Set(in entity, new Transform{ X = 20, Y = 20});
commandBuffer.Add(in entity, new Ai());
// Records operations on the buffered entity.
commandBuffer.Set(in bufferedEntity, new Transform{ X = 20, Y = 20});
commandBuffer.Add(in bufferedEntity, new Ai());
// Transfers them into the world.
commandBuffer.Playback();
Buffers are threadsafe and very fast, perfect for such scenarios. They can and should always be reused! :)