Entity

Namespace: Bang.Entities
Assembly: Bang.dll

public class Entity : IDisposable

An entity is a collection of components within the world. This supports hierarchy (parent, children).

Implements: IDisposable

⭐ Properties

Children

public ImmutableArray<T> Children { get; }

Unique id of all the children of the entity.

Returns
ImmutableArray<T>

Components

public ImmutableArray<T> Components { get; }

This is used for editor and serialization. TODO: Optimize this. For now, this is okay since it's only used once the entity is serialized.

Returns
ImmutableArray<T>

EntityId

public int EntityId { get; }

Entity unique identifier.

Returns
int

FetchChildrenWithNames

public ImmutableDictionary<TKey, TValue> FetchChildrenWithNames { get; }

Fetch a list of all the unique identifiers of the children with their respective names.

Returns
ImmutableDictionary<TKey, TValue>

IsActive

public bool IsActive { get; }

Whether this entity is active or not.

Returns
bool

IsDeactivated

public bool IsDeactivated { get; private set; }

Whether this entity has been deactivated or not.

Returns
bool

IsDestroyed

public bool IsDestroyed { get; private set; }

Whether this entity has been destroyed (and probably recycled) or not.

Returns
bool

Parent

public T? Parent { get; }

This is the unique id of the parent of the entity. Null if none (no parent).

Returns
T?

⭐ Events

OnComponentAdded

public event Action<T1, T2> OnComponentAdded;

Fired whenever a new component is added.

Returns
Action<T1, T2>

OnComponentModified

public event Action<T1, T2> OnComponentModified;

Fired whenever any component is replaced.

Returns
Action<T1, T2>

OnComponentRemoved

public event Action<T1, T2, T3> OnComponentRemoved;

Fired whenever a new component is removed. This will send the entity, the component id that was just removed and whether this was caused by a destroy.

Returns
Action<T1, T2, T3>

OnEntityActivated

public event Action<T> OnEntityActivated;

Fired when the entity gets activated, so it gets filtered back in the context listeners.

Returns
Action<T>

OnEntityDeactivated

public event Action<T> OnEntityDeactivated;

Fired when the entity gets deactivated, so it is filtered out from its context listeners.

Returns
Action<T>

OnEntityDestroyed

public event Action<T> OnEntityDestroyed;

Fired when the entity gets destroyed.

Returns
Action<T>

OnMessage

public event Action<T1, T2, T3> OnMessage;

This will be fired when a message gets sent to the entity.

Returns
Action<T1, T2, T3>

⭐ Methods

AddComponent(T, int)

public bool AddComponent(T c, int index)

Parameters
c T
index int

Returns
bool

AddComponentOnce()

public bool AddComponentOnce()

Add an empty component only once to the entity.

Returns
bool

HasChild(int)

public bool HasChild(int entityId)

Try to fetch a child with a entity identifier.

Parameters
entityId int

Returns
bool

HasChild(string)

public bool HasChild(string name)

Try to fetch a child with a identifier

Parameters
name string

Returns
bool

HasComponent()

public bool HasComponent()

Whether this entity has a component of type T.

Returns
bool

HasComponent(int)

public bool HasComponent(int index)

Checks whether an entity has a component.

Parameters
index int

Returns
bool

HasComponent(Type)

public bool HasComponent(Type t)

Whether this entity has a component of type .

Parameters
t Type

Returns
bool

HasMessage()

public bool HasMessage()

Whether entity has a message of type . This should be avoided since it highly depends on the order of the systems being fired and can lead to several bugs. For example, if we check for that on the state machine, it will depend on the order of the entities in the world.

Returns
bool

HasMessage(int)

public bool HasMessage(int index)

Whether entity has a message of index . This should be avoided since it highly depends on the order of the systems being fired and can lead to several bugs. For example, if we check for that on the state machine, it will depend on the order of the entities in the world.

Parameters
index int

Returns
bool

IsActivateWithParent()

public bool IsActivateWithParent()

Whether this entity should be reactivated with the parent. This is used when serializing data and we might need to revisit this soon.

Returns
bool

RemoveChild(string)

public bool RemoveChild(string name)

Remove a child from the entity.

Parameters
name string

Returns
bool

RemoveComponent()

public bool RemoveComponent()

Removes component of type . Do nothing if is not owned by this entity.

Returns
bool

RemoveComponent(int)

public bool RemoveComponent(int index)

Remove a component from the entity. Returns true if the element existed and was removed.

Parameters
index int

Returns
bool

RemoveMessage(int)

public bool RemoveMessage(int index)

This removes a message from the entity. This is used when the message must be removed within this frame.

Parameters
index int

Returns
bool

ReplaceComponent(T, int, bool)

public bool ReplaceComponent(T c, int index, bool forceReplace)

Parameters
c T
index int
forceReplace bool

Returns
bool

TryGetComponent(out T&)

public bool TryGetComponent(T& component)

Parameters
component T&

Returns
bool

AddComponent(T)

public Entity AddComponent(T c)

Parameters
c T

Returns
Entity

TryFetchChild(int)

public Entity TryFetchChild(int id)

Try to fetch a child with a identifier

Parameters
id int

Returns
Entity

TryFetchChild(string)

public Entity TryFetchChild(string name)

Try to fetch a child with a identifier

Parameters
name string

Returns
Entity

TryFetchChildWithComponent()

public Entity TryFetchChildWithComponent()

This fetches a child with a given component. TODO: Optimize, or cache?

Returns
Entity

TryFetchParent()

public Entity TryFetchParent()

Try to fetch the parent entity.

Returns
Entity

GetComponent()

public T GetComponent()

Fetch a component of type T. If the entity does not have that component, this method will assert and fail.

Returns
T

GetComponent(int)

public T GetComponent(int index)

Fetch a component of type T with . If the entity does not have that component, this method will assert and fail.

Parameters
index int

Returns
T

TryGetComponent()

public T? TryGetComponent()

Try to get a component of type T. If none, returns null.

Returns
T?

Dispose()

public virtual void Dispose()

Dispose the entity. This will unparent and remove all components. It also removes subscription from all their contexts or entities.

Activate()

public void Activate()

Marks an entity as active if it isn't already.

AddChild(int, string)

public void AddChild(int id, string name)

Assign an existing entity as a child.

Parameters
id int

name string

AddComponent(IComponent, Type)

public void AddComponent(IComponent c, Type t)

Add a component of type .

Parameters
c IComponent

t Type

AddOrReplaceComponent(IComponent, Type)

public void AddOrReplaceComponent(IComponent c, Type t)

Add or replace component of type with . Do nothing if the entity has been destroyed.

Parameters
c IComponent

t Type

AddOrReplaceComponent(T, int)

public void AddOrReplaceComponent(T c, int index)

Parameters
c T
index int

AddOrReplaceComponent(T)

public void AddOrReplaceComponent(T c)

Parameters
c T

Deactivate()

public void Deactivate()

Marks an entity as deactivated if it isn't already.

Destroy()

public void Destroy()

Destroy the entity from the world. This will notify all components that it will be removed from the entity. At the end of the update of the frame, it will wipe this entity from the world. However, if someone still holds reference to an Entity (they shouldn't), they might see a zombie entity after this.

RemoveChild(int)

public void RemoveChild(int id)

Remove a child from the entity.

Parameters
id int

RemoveComponent(Type)

public void RemoveComponent(Type t)

Removes component of type . Do nothing if is not owned by this entity.

Parameters
t Type

Reparent(Entity)

public void Reparent(Entity parent)

Set the parent of this entity.

Parameters
parent Entity

Replace(IComponent[], List, bool)

public void Replace(IComponent[] components, List<T> children, bool wipe)

Replace all the components of the entity. This is useful when you want to reuse the same entity id with new components.

Parameters
components IComponent[]

children List<T>

wipe bool

ReplaceComponent(IComponent, Type, bool)

public void ReplaceComponent(IComponent c, Type t, bool forceReplace)

Replace componenent of type with . This asserts if the component does not exist or is not assignable from . Do nothing if the entity has been destroyed.

Parameters
c IComponent

t Type

forceReplace bool

ReplaceComponent(T)

public void ReplaceComponent(T c)

Parameters
c T

SendMessage()

public void SendMessage()

Sends a message of type for any system watching it.

SendMessage(int, T)

public void SendMessage(int index, T message)

Parameters
index int
message T

SendMessage(T)

public void SendMessage(T message)

Parameters
message T

SetActivateWithParent()

public void SetActivateWithParent()

Force the entity to be activated and propagated according to the parent. Default is false (they are independent!)

Unparent()

public void Unparent()

This will remove a parent of the entity. It untracks all the tracked components and removes itself from the parent's children.