These two articles: 1, 2 describe ECS (Entity–Component–System) in detail very well.
After reading them it's easy to implement ECS in Lua.
An "entity" is a table: keys are component type names belonged to this entity, values are true.
A "component" is just a table containing some data, the table may be empty for some types of components.
You will need:
- global table "All_Components": keys are component type names, values are subtables, in which keys are entity IDs, values are components,
- global table "All_Entities" (keys are entity IDs, values are entities),
- your
update() function will be consisted of logically ordered chains of "systems" ("system" is a code block to perform some action for all entities having components with some particular combination of component types).
Adding new component to an entity is just creating of component table and modifying some fields inside All_Components and All_Entities.
Lets say i have entity--> "Player" and it contains Health and Walkspeed. Would these two Health and walkspeed considered components themselves that can later be used in other objects like Monster for example?
Components are just raw data private to their entities.
You should create another instances of components for another entities.
All_Entities = {
[100] = { Health = true, Velocity = true, .... }, -- Player
[101] = { Health = true, Velocity = true, .... }, -- Monster
....
}
All_Components = {
Health = {
[100] = { max_hp = 42, current_hp = 31 }, -- Player's health
[101] = { max_hp = 20, current_hp = 20 }, -- Monster's health
....
},
Velocity = {
[100] = { max_speed = 1.0, speed_x = 0, speed_y = 0 }, -- Player
[101] = { max_speed = 2.5, speed_x = -2.5, speed_y = 0 }, -- Monster
....
},
....
}
function update(dt)
....
-- "Health system" will act on all entities having "Health" component
for entity_id, component in pairs(All_Components.Health) do
-- 5 minutes for full regeneration
component.current_hp = math.min(
component.max_hp,
component.current_hp + component.max_hp * dt / (5*60)
)
end
....
-- "Health_Bar system" will traverse over all the entities
-- having both Health and Health_Bar components simultaneously
for entity_id, comp_Health_Bar in pairs(All_Components.Health_Bar) do
local comp_Health = All_Components.Health[entity_id]
if comp_Health then
-- entity "entity_id" has two components:
-- comp_Health and comp_Health_Bar
-- Now we have all the info needed to draw its health bar
....
end
end
....
end
Please note that there is no OOP here at all, ECS has nothing to do with OOP.
Of course you can add OOP here, for example, to treat all components of same type as instances of some class. But do you really need it?
The idea of ECS: "components" are just raw data, all the code is inside "systems", this approach is more flexible than OOP.
Can two components be added together or merged for example Health Component and Stamina component into one, if needed?
Some "systems" will need these components to have different types, so it is better to keep them apart.