Concord is a feature complete library that allows you to use Entity - Component - System (ECS) model to write and organize your Lua code.
Concord puts a lot of effort on being easy to use and provide a great developer experience, while trying to be competitive in performance with alternative libraries.
You can think of Components as the data and state that make up all of your game.
Components themselves don’t exist as part of our World, they instead are the pieces that build up Entities.
Components have a name, and some arbitrary shape that you can define.
For example if we wanted to store the Position of a given Entity, we would define a component called position with a shape that could be something like {x: number, y: number}
In Concord this would look like this:
Concord.Component("position", function (component, x, y)
component.x= x
component.y= y
end)
The first argument is the name, and the second is what we call the populate function.
Components start as empty tables and this populate function assigns the values for each of the Component properties.
Sometimes you don’t need to store data on the Component since the Component itself will act as a flag, in those cases you don’t need to provide a populate function:
In ECS everything that exists as part of the World is an Entity. An Entity could be a Player, an Enemy, a Crate, a Bullet, etc.
Entities by themselves are empty packages with nothing on them, and they would all behave the same if we left them like that. To tell them appart and give unique data to each of them, we need to give them some Components.
In Concord this is very simple:
local Cat =world:newEntity()
--local Cat = Concord.Entity(world) -- Alternative syntax
Cat:give("position", 50, 50)
Cat:give("terrestrial")
Once you give a Component to an Entity, the Component will become a part of it:
The power of Concord and ECS in general comes from grouping a bunch of Entities based on rules. These rules acts as a Filter where we can ask the World for all Entities that have all the Components we required.
For example we could ask the World for a list of all the Entities that are terrestrial and have a position, and that are not a machine:
local animals =world:query({ "terrestrial", "position", "!machine" }) -- The ! in front of machine indicates negation
for i, entity inipairs(animals) do
print(entity.position.x, entity.position.y)
end
Side note on Filter rules
If we had another Entity like this one:
local Parrot =world:newEntity()
Parrot:give("position", 100, 100)
It would not be part of our list above, since the filter required:
terrestrial and position and not machine
And although Parrot has position and doesn’t have machine, it lacks the terrestrial Component.
This is great but the World:query operation is rather expensive, so Concord instead provides Pools.
A Pool has an associated Filter and will be updated when Entities are added or modified in the World.
In order to use pools however, we need to speak about Systems
In ECS, Components and Entities generally don’t have behavior themselves, and instead rely on Systems that define how a group of Entities should behave.
For example if we want all terrestrial animals to move 5 pixels to the right every time we receive a move event we would first create a System like:
local Movement = Concord.System("Movement", {animals = { "terrestrial", "position" }})
This system is called Movement and has a Pool called animals that contains all the Entities with terrestrial and position Components.
We now want to receive all move events. To do that we define a callback in our System like so:
functionMovement:move ()
for i, entity inipairs(self.animals) do
entity.position.x= entity.position.x+5
end
end
Whenever we receive a move event in our World, this callback will execute, and the Movement System will iterate through the Pool of animals changing their position Components to move them 5 pixels to the right.