-
Notifications
You must be signed in to change notification settings - Fork 2
IEntityRelationComponent
Martin Evans edited this page Aug 1, 2024
·
3 revisions
A entity relation component is a component defined with IEntityRelationComponent
instead of IComponent
.
public interface IEntityRelationComponent : IComponent
{
public Entity Target { get; set; }
}
An entity relation component solves a specific problem with referring to entities in components. When an entity is created in a CommandBuffer
you don't know the entity ID until after the buffer is executed. This means that if you create two entities at once and want to store a link from one to the other in a component it's a two-stage process:
var buffer = new CommandBuffer(world);
// Create an entity
var a = buffer.Create();
// Create another entity, linked to an invalid ID
var b = buffer.Create().Set(new LinkComponent(default(Entity)));
// Execute the buffer
using var resolver = buffer.Playback();
// Now you can finally fill in the link
b.GetComponentRef<LinkComponent>().Entity = resolver[a];
An IEntityRelationComponent
solves this problem, the CommandBuffer
will automatically resolve the entity ID for you when it is executed:
var buffer = new CommandBuffer(world);
// Create an entity
var a = buffer.Create();
// Create another entity, linked to the new entity
var b = buffer.Create().Set(default(LinkComponent), a);
// No need to resolve anything, the `LinkComponent` has been initialised automatically
buffer.Playback.Dispose();