Skip to content

Entity.AddComponent Method

Definition

Namespace: Brine2D.ECS

Overloads
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.

Entity.AddComponent\<T>() Method

Creates and adds a component of the specified type.

C#
public Brine2D.ECS.Entity AddComponent<T>()
    where T : Brine2D.ECS.Component, new();

Type parameters

T

Returns

Entity

Entity.AddComponent\<T>\(Action\<T\>\) Method

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.

C#
public Brine2D.ECS.Entity AddComponent<T>(System.Action<T>? configure)
    where T : Brine2D.ECS.Component, new();

Type parameters

T

Parameters

configure System.Action<T>

Returns

Entity

Remarks

configure is invoked after Entity has been set to this entity, so the owning entity is accessible inside the callback. Note that the component has not been added to the world's pools yet when configure runs, so Brine2D.ECS.Entity.HasComponent<> will return false for this component type inside the callback.

Entity.AddComponent\<T>\(T\) Method

Adds an existing component instance to this entity.

C#
public Brine2D.ECS.Entity AddComponent<T>(T component)
    where T : Brine2D.ECS.Component;

Type parameters

T

Parameters

component T

Returns

Entity

Exceptions

System.InvalidOperationException
Thrown when the entity has been destroyed or is not attached to a world.

System.ArgumentException
Thrown when the runtime type of component does not match T, which would cause the component to be stored under the wrong pool key. Also thrown when component is already attached to a different entity; a component instance may only belong to one entity at a time.