2

After long search i decided to come here, cause it was impossible to find Component Based Entity system written purely in Lua on the internet.

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. My understanding is that components are just regular tables with methods and properties that can be later added to different Entities. Can two components be added togheter or merged for example Health Component and Stamina component into one, if needed? What i really would like is sample code because, i don't understand how this system would completely work, how would the components be created in first place, how could i add them to entity etc... Also how would they be stored in global _G table or as chunks of modules? I completely understand how OOP and inheritance works in lua but component based Entity system just doesn't have any code examples out there only pictures and diagrams.

Thanks:)

1 Answer 1

2

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.

3
  • Thank you very much for this, it's very helpful :)
    – user5130814
    Mar 8, 2017 at 17:29
  • I have one more question. Let's Say i have "Health" Component which just holds current_health, max_health properties and then Component "Health_Bar" which holds a 3D User Interface element now how would the "Health_Bar" component get values from "Health" component and display Health_Bar size according to "Health" component values. I Want them to be seperate components "Health" and "Health_Bar", cause i don't want to display Health_Bar on every Entity.
    – user5130814
    Mar 9, 2017 at 19:43
  • 1
    You should add new "system" which traverse over all the entities having both Health and Health_Bar components simultaneously. I've added the code example for "Health Bar system" to the answer. Mar 9, 2017 at 21:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy