Skip to content

Entity Class

Definition

Namespace: Brine2D.ECS

Base class for entities in the ECS. Supports hybrid approach: can be used as a component container \(data\-oriented\) or as a behavior host \(object\-oriented\) by overriding lifecycle methods.

C#
public class Entity

Inheritance System.Object → Entity

Properties
Children Gets the read-only collection of child entities. The wrapper is created once and cached; it reflects live changes to the child list.
Id Unique ID for this entity. Assigned atomically at creation. IDs start at 1; 0 is reserved as an invalid/null sentinel.
IsActive Indicates whether this entity is active in the world. Inactive entities are skipped by the ECS processing and excluded from queries.
IsRoot Gets whether this entity is a root entity \(has no parent\).
Parent Gets the parent entity, or null if this is a root entity.
PoolsCleared True after OnDestroy() has removed this entity's components from all pools. Used by EntityWorld to skip the redundant safety-net cleanup pass.
Tags Gets the tags for this entity as a read-only set. Use Brine2D.ECS.Entity.AddTag\(System\.String\), Brine2D.ECS.Entity.RemoveTag\(System\.String\), and Brine2D.ECS.Entity.ClearTags to modify tags so that validation and logging are applied consistently.
World The entity world this entity belongs to, or null if not yet added to a world.
Methods
AddBehavior<T>() Adds a behavior to this entity with automatic dependency injection.
AddBehavior<T>\(Action<T>\) Creates, configures, and adds a behavior of the specified type.
AddChild\(Entity\) Adds a child entity to this entity.
AddComponent<T>() Creates and adds a component of the specified type.
AddComponent<T>\(Action<T>\) Creates and adds a component with inline configuration. If the entity already has this component type, the call is silently ignored, consistent with the other AddComponent overloads.
AddComponent<T>\(T\) Adds an existing component instance to this entity.
CollectDescendants\(List<Entity>\) Appends all descendant entities into results via iterative depth-first traversal. Avoids allocating the result collection; use a pre-allocated or reused System.Collections.Generic.List<> to reduce per-call heap pressure.
Destroy() Destroys this entity, removing it from the world.
DetachFromParent() Detaches this entity from its parent, making it a root entity.
FindDescendant\(string\) Finds the first descendant entity with the specified name.
GetAllBehaviors() Gets all behaviors attached to this entity.
GetBehavior<T>() Gets a behavior of the specified type attached to this entity.
GetBehaviorInChildren<T>() Gets a behavior of the specified type on this entity or any of its children \(depth\-first recursive search\).
GetBehaviorInParent<T>() Gets a behavior of the specified type on this entity or any of its ancestors.
GetBehaviors<T>() Gets all behaviors of the specified type \(or a derived type\) attached to this entity.
GetComponent\(Type\) Gets a component of the specified type attached to this entity \(non\-generic\). Used internally by the serialization layer when the concrete type is only known at runtime. Prefer the generic overload for all other uses.
GetComponentInChildren<T>() Gets a component on this entity or any of its children \(depth\-first recursive search\).
GetComponentInParent<T>() Gets a component on this entity or any of its ancestors.
GetDepth() Gets the depth of this entity in the hierarchy \(0 = root\).
GetDescendants() Gets all descendant entities \(children, grandchildren, etc\.\) via iterative depth-first traversal.
GetDescendantsWithTag\(string\) Gets all descendants with the specified tag.
GetRequiredBehavior<T>() Gets a behavior of the specified type, throwing if it is not found.
GetRoot() Gets the root entity of this hierarchy.
HasBehavior\(Type\) Checks if this entity has a behavior of the specified type \(non\-generic version\). Used internally by query infrastructure for dynamic type checking.
HasBehavior<T>() Checks if this entity has a behavior of the specified type.
HasComponent\(Type\) Checks if this entity has a component of the specified type \(non\-generic version\). Used internally by EntityQuery for dynamic type checking.
NotifyEnabledChanged() Forwards IsEnabled and IsEnabled change notifications to the world so cached queries built with OnlyEnabled() are invalidated.
OnActivated() Called when the entity transitions from inactive to active \(i\.e\., [IsActive](IsActive.md 'Brine2D\.ECS\.Entity\.IsActive') changes to [true](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/bool 'https://docs\.microsoft\.com/en\-us/dotnet/csharp/language\-reference/builtin\-types/bool')\). Override to resume state or restart effects.
OnDeactivated() Called when the entity transitions from active to inactive \(i\.e\., [IsActive](IsActive.md 'Brine2D\.ECS\.Entity\.IsActive') changes to [false](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/bool 'https://docs\.microsoft\.com/en\-us/dotnet/csharp/language\-reference/builtin\-types/bool')\). Override to pause state, stop effects, or release transient resources.
OnDestroy() Called once when the entity is destroyed and removed from the world. Override to implement custom cleanup logic.
OnInitialize() Called once when the entity is created and added to the world. Override to implement initialization logic.
RemoveBehavior<T>() Removes a behavior from this entity.
RemoveChild\(Entity\) Removes a child entity from this entity \(makes it a root entity\). Delegates to SetParent\(Entity\) so any future side effects added there \(events, transform propagation, etc\.\) apply through this path too.
SetActiveHierarchically\(bool\) Sets IsActive on this entity and all of its descendants. Use this to toggle an entire hierarchy in one call instead of walking children manually.
SetActiveWithoutNotification\(bool\) Sets IsActive without notifying the world. Used during entity destruction to avoid O\(n × q\) redundant query invalidations that will be superseded by pool removal.
SetLogger\(ILogger<Entity>\) Sets the logger for this entity. Internal — called by CreateEntity<T>\(string\) after construction when the new() constraint prevents constructor injection.
SetParent\(Entity\) Sets the parent of this entity.
SetWorld\(EntityWorld\) Sets the world this entity belongs to. Internal - only called by EntityWorld and SceneManager.
TryGetBehavior<T>\(T\) Gets a behavior of the specified type and returns whether it was found. Performs a single linear scan, avoiding the double-scan of HasBehavior<T>() followed by GetBehavior<T>().
TryGetComponent<T>\(T\) Gets a component of the specified type and returns whether it was found. Performs a single pool lookup, avoiding the double-lookup of Brine2D.ECS.Entity.HasComponent<> followed by Brine2D.ECS.Entity.GetComponent<>.