Components - ecbambrick/SimpleEntityComponentSystem GitHub Wiki


Components are simple tables of key-pair values. All data within a component should be conceptually related and contain no logic. Each entity is a list of components, whereas each of those components contains the data for that entity.

For example, if you would like to make an entity drawable, you may want to create a "position" component which holds (x,y) coordinates and a "sprite" component which holds an image. Later on, you may want a "collision" component to determine collision behaviour. The position component you created earlier can be re-used with the collision component to determine the position of the collision box.

You can combine various components in an entity to form different behaviours. Behaviours themselves will be determined by systems.

Functions

nil secs.component(componentName, defaultValues)
Create a new component.

nil secs.attach(entity, componentName, values)
Attach a component to an entity along with any custom data for the entity.

componentList secs.detach(entity, componentName...)
Remove each component from an entity and returns a list of references.

Examples

-- create a new component
secs.component("newComponent", { greeting = "hello", size = 10 })

-- attach the component to an entity and override the greeting value
secs.attach(entity, "newComponent", { greeting = "hi" })

-- remove the component from the entity
secs.detach(entity, "newComponent")