Skip to content
Magicolo edited this page Feb 21, 2019 · 23 revisions

Content


Description

An entity is an identifier used to group components together and to refer to other groups of components. In Entia, an entity is implemented using generational indices. This means that there is an index based register that holds every live entity. The indices of that register are reused whenever an entity is destroyed and to ensure that a destroyed entity does not collide with a live one that reused its index, a generation counter is incremented each time an index is reused. So, the identifier inside the entity struct is composed of 32 bits for the index and 32 bits for the generation.

Having an index associated with the entity allows for nearly instant component lookups while still using the available identifiers efficiently.


Usage

using Entia;
using Entia.Injectables;
using Entia.Queryables;
using Entia.Systems;

namespace Components
{
    public struct Target : IComponent
    {
        // An Entity field can be used to link data together.
        public Entity Entity;
    }
}

namespace Systems
{
    public struct CreatorDestructor : IRun
    {
        // Entity can be included in a query but does not add a filter.
        public readonly struct Query : IQueryable
        {
            public readonly Entity Entity;
        }

        // An injectable that gives access to all entity operations.
        public readonly AllEntities Entities;

        // These groups will contain all live entities.
        // Entity can be included directly in a group.
        public readonly Group<Entity> Group1;
        public readonly Group<Query> Group2;

        public void Run()
        {
            Entity someEntity = Entities.Create();
            Entities.Destroy(someEntity);

            // Iterates over all live entities.
            foreach (var entity in Entities) { /*...*/ }
            foreach (ref readonly var item in Group1) { /*...*/ }
            foreach (ref readonly var item in Group2) { /*...*/ }
        }
    }
}

Related