World
A collection of Systems and Entities.
Require
Section titled “Require”If the concord folder is in the root directory of your game, you can import this module like so:
concord.system = require('concord.world')A World emits events so Systems can iterate their Pools. A World contains any amount of Systems and Entities, plus optional named resources.
Fields
Section titled “Fields”| Name | Type | Description |
|---|---|---|
addEntity |
fun(self: World, e: Entity): World |
Creates a new World.
world = World.new( ... )Arguments
Section titled “Arguments”| Name | Type | Description |
|---|---|---|
... |
SystemClass | SystemClasses of Systems to add |
Returns
Section titled “Returns”| Name | Type | Description |
|---|---|---|
world |
World | The new World |
See also
Section titled “See also”addEntity
Section titled “addEntity”Queues an Entity to be added to the World.
The Entity’s world is set immediately. The Entity is not in getEntities() and is not evaluated by Systems until the next flush.
world = world:addEntity( e )Arguments
Section titled “Arguments”| Name | Type | Description |
|---|---|---|
e |
Entity | Entity to add |
Returns
Section titled “Returns”| Name | Type | Description |
|---|---|---|
world |
World |
newEntity
Section titled “newEntity”Creates a new Entity and queues it to be added to the World.
world:newEntity()Returns
Section titled “Returns”| Name | Type | Description |
|---|---|---|
e |
Entity | the new Entity |
Queries flushed Entities against a Filter definition.
Pending (not yet flushed) additions are not included. Component changes on already flushed Entities are visible immediately.
world:query( def, onMatch )Pass a function as onMatch. It is called once per matching Entity. Nothing is returned.
Arguments
Section titled “Arguments”| Name | Type | Description |
|---|---|---|
def |
FilterDefinition | Filter definition (component names, with ! prefix to reject) |
onMatch |
fun(e: Entity) | Called for each match. |
matches = world:query( def, onMatch )Collect matches into a table and return it. onMatch is optional; when omitted, a new empty table is used.
Arguments
Section titled “Arguments”| Name | Type | Description |
|---|---|---|
def |
FilterDefinition | Filter definition (component names, with ! prefix to reject) |
onMatch? |
table? | Optional. Table to fill with matches; when omitted, a new empty table is allocated and returned. |
Returns
Section titled “Returns”| Name | Type | Description |
|---|---|---|
matches |
table | The list of matches, or nil if onMatch was a callback |
removeEntity
Section titled “removeEntity”Queues an Entity to be removed from the World.
If the Entity has a key Component, it is removed first. The Entity remains in the World until the next flush.
world = world:removeEntity( e )Arguments
Section titled “Arguments”| Name | Type | Description |
|---|---|---|
e |
Entity | Entity to remove |
Returns
Section titled “Returns”| Name | Type | Description |
|---|---|---|
world |
World |
canFlush
Section titled “canFlush”Returns whether a Flush can currently be performed.
Flushing is forbidden while a Flush is already in progress (for example from World:onEntityAdded / Pool:onAdded).
boolean = world:canFlush()Returns
Section titled “Returns”| Name | Type | Description |
|---|---|---|
boolean |
boolean |
Applies queued Entity additions, removals, and dirty reevaluations.
Only the queued buffers are processed, not every Entity in the World.
world = world:flush()Returns
Section titled “Returns”| Name | Type | Description |
|---|---|---|
world |
World |
addSystem
Section titled “addSystem”Adds a System to the World.
Functions on the SystemClass are registered as event listeners. Entities already in the World are evaluated against the System’s Filters.
world = world:addSystem( systemClass )Arguments
Section titled “Arguments”| Name | Type | Description |
|---|---|---|
systemClass |
SystemClass | SystemClass of System to add |
Returns
Section titled “Returns”| Name | Type | Description |
|---|---|---|
world |
World |
See also
Section titled “See also”addSystems
Section titled “addSystems”Adds multiple Systems to the World.
Functions on each SystemClass are registered as event listeners.
world = world:addSystems( ... )Arguments
Section titled “Arguments”| Name | Type | Description |
|---|---|---|
... |
SystemClass | SystemClasses of Systems to add |
Returns
Section titled “Returns”| Name | Type | Description |
|---|---|---|
world |
World |
See also
Section titled “See also”hasSystem
Section titled “hasSystem”Returns whether the World has a System.
boolean = world:hasSystem( systemClass )Arguments
Section titled “Arguments”| Name | Type | Description |
|---|---|---|
systemClass |
SystemClass | SystemClass of System to check for |
Returns
Section titled “Returns”| Name | Type | Description |
|---|---|---|
boolean |
boolean |
getSystem
Section titled “getSystem”Gets a System from the World.
system = world:getSystem( systemClass )Arguments
Section titled “Arguments”| Name | Type | Description |
|---|---|---|
systemClass |
SystemClass | SystemClass of System to get |
Returns
Section titled “Returns”| Name | Type | Description |
|---|---|---|
system |
System | System to get |
Emits a callback in the World.
Calls all functions with the functionName of added Systems.
Prefix the name with a System’s name to target one System: "SystemName.event".
Automatically flushes before forwarding the event, unless this emit is nested
or a flush is already in progress.
If beforeEmit exists and returns a truthy value, listeners are skipped.
afterEmit is called after listeners when present.
world = world:emit( functionName, ... )Arguments
Section titled “Arguments”| Name | Type | Description |
|---|---|---|
functionName |
string | Event name, or "SystemName.event" to call only that System |
... |
any | Parameters passed to System’s functions |
Returns
Section titled “Returns”| Name | Type | Description |
|---|---|---|
world |
World |
emitNoFlush
Section titled “emitNoFlush”Emits a callback in the World without flushing.
world = world:emitNoFlush( functionName, ... )Arguments
Section titled “Arguments”| Name | Type | Description |
|---|---|---|
functionName |
string | Name of functions to call. |
... |
any | Parameters passed to System’s functions |
Returns
Section titled “Returns”| Name | Type | Description |
|---|---|---|
world |
World |
See also
Section titled “See also”Removes all Entities from the World and flushes.
Entities that were queued to be added are detached immediately without onEntityRemoved. Already flushed Entities are removed through the normal flush.
world = world:clear()Returns
Section titled “Returns”| Name | Type | Description |
|---|---|---|
world |
World |
getEntities
Section titled “getEntities”Returns the List of flushed Entities in the World.
list = world:getEntities()Returns
Section titled “Returns”| Name | Type | Description |
|---|---|---|
list |
List |
getSystems
Section titled “getSystems”Returns the List of Systems in the World.
list = world:getSystems()Returns
Section titled “Returns”| Name | Type | Description |
|---|---|---|
list |
List |
serialize
Section titled “serialize”Serializes serializable Entities in the World.
Flushes first. Only Entities with a serializable Component are included.
serializedWorld = world:serialize( ignoreKeys )Arguments
Section titled “Arguments”| Name | Type | Description |
|---|---|---|
ignoreKeys |
boolean | If true, Entity keys are omitted |
Returns
Section titled “Returns”| Name | Type | Description |
|---|---|---|
serializedWorld |
SerializedWorld | Serialized world data |
deserialize
Section titled “deserialize”Deserializes Entities into the World.
world = world:deserialize( data, startClean, ignoreGenerator )Arguments
Section titled “Arguments”| Name | Type | Description |
|---|---|---|
data |
SerializedWorld | Serialized world data |
startClean |
boolean | If true, existing Entities are cleared first |
ignoreGenerator |
boolean | If true, the World’s key generator state is left unchanged |
Returns
Section titled “Returns”| Name | Type | Description |
|---|---|---|
world |
World |
setKeyGenerator
Section titled “setKeyGenerator”Sets the function used to generate Entity keys.
world = world:setKeyGenerator( generator, initialState )Arguments
Section titled “Arguments”| Name | Type | Description |
|---|---|---|
generator |
fun(state: any): string|number, | any Callable that receives the current state and returns key, newState |
initialState |
any | Initial state passed to the generator |
Returns
Section titled “Returns”| Name | Type | Description |
|---|---|---|
world |
World |
getEntityByKey
Section titled “getEntityByKey”Gets an Entity by its key.
world:getEntityByKey( key )Arguments
Section titled “Arguments”| Name | Type | Description |
|---|---|---|
key |
string|number | Key to look up |
Returns
Section titled “Returns”| Name | Type | Description |
|---|---|---|
| Entity|nil |
setResource
Section titled “setResource”Sets a named resource in the World.
world = world:setResource( name, resource )Arguments
Section titled “Arguments”| Name | Type | Description |
|---|---|---|
name |
string | Name of the resource |
resource |
any | Resource to set |
Returns
Section titled “Returns”| Name | Type | Description |
|---|---|---|
world |
World |
getResource
Section titled “getResource”Gets a named resource from the World.
any = world:getResource( name )Arguments
Section titled “Arguments”| Name | Type | Description |
|---|---|---|
name |
string | Name of the resource |
onEntityAdded
Section titled “onEntityAdded”Callback for when an Entity is added to the World.
world:onEntityAdded( e )Arguments
Section titled “Arguments”| Name | Type | Description |
|---|---|---|
e |
Entity | The Entity that was added |
onEntityRemoved
Section titled “onEntityRemoved”Callback for when an Entity is removed from the World.
world:onEntityRemoved( e )Arguments
Section titled “Arguments”| Name | Type | Description |
|---|---|---|
e |
Entity | The Entity that was removed |
beforeEmit
Section titled “beforeEmit”Callback before World:emit forwards an event to Systems.
Returning a truthy value skips System listeners and afterEmit for this emit. Nested emits are forbidden inside this callback
boolean = world:beforeEmit( systemName, functionName, listeners, ... )Arguments
Section titled “Arguments”| Name | Type | Description |
|---|---|---|
systemName |
string | System name when emit used "SystemName.event", otherwise nil |
functionName |
string | Name of the event being emitted |
listeners |
table | Listeners registered for this event, or nil |
... |
any | Parameters passed to World:emit |
Returns
Section titled “Returns”| Name | Type | Description |
|---|---|---|
boolean |
boolean? | Truthy to skip listeners |
afterEmit
Section titled “afterEmit”Callback after World:emit has forwarded an event to Systems.
Not called when beforeEmit skips the emit Nested emits are forbidden inside this callback
world:afterEmit( systemName, functionName, listeners, ... )Arguments
Section titled “Arguments”| Name | Type | Description |
|---|---|---|
systemName |
string | System name when emit used "SystemName.event", otherwise nil |
functionName |
string | Name of the event being emitted |
listeners |
table | Listeners registered for this event, or nil |
... |
any | Parameters passed to World:emit |
beforeSystemCallback
Section titled “beforeSystemCallback”Callback before World:emit forwards an event to a System.
Returning a truthy value skips the System listener and afterSystemCallback for this event. Nested emits are forbidden inside this callback
boolean = world:beforeSystemCallback( system, eventName, ... )Arguments
Section titled “Arguments”| Name | Type | Description |
|---|---|---|
system |
System | System that is being called |
eventName |
string | Name of the event being emitted |
... |
any | Parameters passed to World:emit |
Returns
Section titled “Returns”| Name | Type | Description |
|---|---|---|
boolean |
boolean? | Truthy to skip the System listener and afterSystemCallback |
afterSystemCallback
Section titled “afterSystemCallback”Callback after World:emit has forwarded an event to a System.
Not called when beforeSystemCallback skips the emit Nested emits are forbidden inside this callback
world:afterSystemCallback( system, eventName, ... )Arguments
Section titled “Arguments”| Name | Type | Description |
|---|---|---|
system |
System | System that is being called |
eventName |
string | Name of the event being emitted |
... |
any | Parameters passed to World:emit |
SerializedWorld
Section titled “SerializedWorld”Payload from World:serialize, passed to World:deserialize.
Fields
Section titled “Fields”| Name | Type | Description |
|---|---|---|
generator |
any | Snapshot of the World’s key-generator state |
[integer] |
table | Serialized Entity data from Entity:serialize |