Relations
Relation
entity.AddRelation(new InventoryItem { type = ItemType.Coin });
entity.AddRelation(new InventoryItem { type = ItemType.Axe });
entity.RemoveRelation <InventoryItem,ItemType>(ItemType.Coin);
entity.GetRelations <InventoryItem>(); // O(1)
entity.GetRelation <InventoryItem,ItemType>(ItemType.Axe); // O(N)
// N: number of relations on a single entityenum ItemType {
Coin = 1,
Axe = 2,
}
struct InventoryItem : IRelation<ItemType> { // relation key type: ItemType
public ItemType type;
public int count;
public ItemType GetRelationKey() => type; // unique relation key
}
public static void Relations()
{
var store = new EntityStore();
var entity = store.CreateEntity();
// add multiple relations of the same component type
entity.AddRelation(new InventoryItem { type = ItemType.Coin, count = 42 });
entity.AddRelation(new InventoryItem { type = ItemType.Axe, count = 3 });
// Get all relations added to an entity. O(1)
entity.GetRelations <InventoryItem>(); // { Coin, Axe }
// Get a specific relation from an entity. O(N)
entity.GetRelation <InventoryItem,ItemType>(ItemType.Coin); // {type=Coin, count=42}
// Remove a specific relation from an entity
entity.RemoveRelation<InventoryItem,ItemType>(ItemType.Axe);
entity.GetRelations <InventoryItem>(); // { Coin }
}Serialization
Benchmark - Relations
relationsPerEntity
duration ms
entities
Last updated