-
Notifications
You must be signed in to change notification settings - Fork 0
Tutorials Component Component
Components can be seen as the smallest part of a game object.
A component adds a specific aspect to a game object, it gives to it a specific behavior. A game object is only an id but when you add components it becomes more than id, it has a specific definition and purpose. A game object without components has no meaning, it's like an empty shell.
For example, we want to create a war game with soldiers. Some soldiers have a shield and others no :
Each component gives a meaning to the game object. When we add a Weapon and Health component to the game object we get a soldier. And when we add a shield component we get an armored soldier.
Components are only made of data, and systems are brains that processes those data.
A component must inherit the abstract class Component.
This class has only one property : Parent. This property hold an instance of the game object that owns the component (if the component has been added to a game object).
Here is an example of a basic implementation for a health component :
public sealed class HealthComponent : Component
{
public int Life { get; set; }
}
Instantiating a component will depends on how you have created it :
// We will use the health component as an example
HealthComponent health = new HealthComponent();
Adding a component to a game object is easy :
// We will use the HealthComponent class from previous example
GameObject soldier = new GameObject() { Id = 1 };
HealthComponent health = new HealthComponent() { Life = 100 };
soldier.Add(health);
There is one restriction when adding a component, a game object can only have one component of the same type at a time.
To remove a component, you have to call the Remove method :
// soldier is a game object
soldier.Remove<HealthComponent>();
// or
soldier.Remove(typeof(HealthComponent));
Each world has a ComponentManager property. This property contains an instance of ComponentManager class. This class is used to store all components of the world.
The ComponentManager regroups each component by type. A group of component is represented by the ComponentGroup class.
// myWorld is a World instance
ComponentGroup healthGroup = myWorld.ComponentManager.GetGroup(typeof(HealthComponent));
// or
ComponentGroup healthGroup = myWorld.ComponentManager.GetGroup<HealthComponent>();
A ComponentGroup instance can be used to get specific information about a type of component.
You can retrieve a component from a game object instance :
// mySoldier is a game object instance
HealthComponent healthComponent = mySoldier.Get<HealthComponent>();
// or
HealthComponent healthComponent = mySoldier.Get(typeof(HealthComponent));
Or you can retrieve a component from a group of component by passing a specific game object instance :
ComponentGroup healthGroup = myWorld.ComponentManager.GetGroup<HealthComponent>();
// Get will return the component associated to the specified game object otherwise null
HealthComponent healthComponent = healthGroup.Get(mySoldier);