Skip to content
Jeff Campbell edited this page Aug 21, 2020 · 10 revisions

Overview

Components are the building blocks of EntitasRedux that comprise the data for an Entity. By itself, an Entity is simple object with a unique identifier; the components are what associates data to that Entity and by extension the systems that associate behaviour with it. They are plain-old CSharp POCO objects whose intent is to define a specific, narrow subset of related data about an Entity.

A class is identified as a component by implementing IComponent. This enables discovery of it when code-generation is run so that it can be associated with an Entity

An example of a component for UnityEngine.Transform.

using JCMG.EntitasRedux;
using UnityEngine;

[Game]
public class TransformComponent : IComponent
{
	public Transform transform;
}

When code-generation is run, any Context's it is associated with and their related entities will have their API extended to include that component. Using the TransformComponent example above, the entity API would appear like so:

// Check to see if it has this component already.
var hasTransformn = entity.HasTransform;

// If a component isn't present, you can add it.
entity.AddTransform(newTransform);

// If a component is present, you can replace its contents and...
entity.ReplaceTransform(newTransform);

// ...you can also remove it, resulting in the component being returned to the pool.
entity.RemoveTransform();

By default, a component will be associated with the primary or first context defined, but can be decorated with a Context's attribute so that it is explicitly associated with only that context. This can be for one or more multiple Contexts. For example, were your first context to be GameContext and the second was UIContext, you could associate a component with one or both like so:

using JCMG.EntitasRedux;
using UnityEngine;

[Game] // This results in this component only being associated with the `GameContext`, regardless of where it was defined.
public class FooComponent : IComponent
{
	public Transform transform;
}

[UI] // This results in this component only being associated with the `UIContext`, regardless of where it was defined.
public class BarComponent : IComponent
{
	public RectTransform rectTransform;
}

[Game, UI] // This results in this component being associated with both the `GameContext` and `UIContext`.
public class FooBarComponent : IComponent
{
	public Transform transform;
}

Flag Component

A flag component that indicates a true or false state about an Entity and does not have any other information associated with it. It is typically implemented as a component with zero members. A [FlagPrefix] attribute can be used to add a custom prefix to the code-generated API rather than using the default prefix Is. It's useful for identifying a quick true/false state about an entity and has a slightly different Entity API than a standard component.

using JCMG.EntitasRedux;
using UnityEngine;

public class EnemyComponent : IComponent { }

public class NPCComponent : IComponent { }

public class PlayerComponent : IComponent { }

[FlagPrefix("Has")]
public class PlagueComponent : IComponent { }

Example API Usage for Flags

entity.IsEnemy = false;
entity.IsNPC = false;
entity.IsPlayer = true;

// This flag component uses a custom prefix
entity.HasPlague = false;

Additional options for Components

There are several different variations of components and additive functionality that can be generated through various attributes. Some alter how a component is associated with an entity or Context while others generate additional helper or search methods. See Attributes for more information.