Component
How to define and register a component
If you are unfamiliar with Entity Component System (ECS), we recommend reading Introduction to ECS before proceeding.
Components are data attributes attached to an entity. They define the properties of an entity, but it does not implement logic or behavior.
In Cardinal, components are defined as Go structs.
Example:
- A
Position
component may contain thex
,y
, andz
coordinates of an entity. - A
Health
component may contain thecurrent
andmax
health of an entity.
Defining Components
By convention, components are defined in the component
directory in its own separate files.
You can easily create a new component and register it to the world by following these steps:
Define the component struct
A component is defined as a Go struct. It must implement the Name()
method which returns a unique name of the component. This is used to identify the component in the world.
Register the component in main.go
Components must be registered in the world before they can be used. This is done by calling the RegisterComponent
function.