Technical manual
...
Engine module
Entities, components and systems
8 min
what is an ecs? ecs refers to the " entity component system " architecture paradigm, that is characterized by these three key elements entities are simply collections of components identified by a number components are abstract objects that contain data they allow data to be structured with composition instead of inheritance systems are functions that operate on these entities and components component data types components support two types of data structure of arrays array of structures structure of arrays component data structure of arrays is a data layout that stores data in a way that is more cache friendly it is a good choice for data that is accessed often and in a predictable way, such as transform data const transformcomponent = definecomponent({ name 'transformcomponent', schema { position types f64, rotation types f64, scale types f64 } }) array of structures reactive component data reactive component data is an implementation unique to ir engine, using react and hookstate under the hood its key benefits are it allows for reactive data binding when a property is changed, all effects depending on that data will be triggered it is a good choice for data that is accessed infrequently and in an unpredictable way (especially when react style logic is associated with it) const debugarrowcomponent = definecomponent({ name 'debugarrowcomponent', oninit (entity) => { return { color 0xffffff, direction new vector3(), position new vector3() } }, onset (entity, component, json) => { if (!json) return if (json color) component color set(json color) if (json direction) component direction set(json direction) if (json position) component position set(json position) } }) examples timer in the following code snippets we will define a component and a system the timercomponent will hold a property to store the current elapsed time rounded down in the initializer of the system, we will create a new entity and adds the time component to it in the execute function of the system, we will set the property time on the component of the entity this example uses the structure of arrays (soa) component data syntax, from bitecs const timercomponent = definecomponent({ name 'timercomponent', schema { time types f32 } }) const timerquery = definequery(\[timercomponent]) const execute = () => { const { deltaseconds } = getstate(enginestate) for (const entity of timerquery()) { timercomponent time\[entity] += delta } } export const timersystem = definesystem({ uuid 'timersystem', execute }) this example uses the array of structures reactive component data syntax, from ir engine, which allows for reactive data binding const timercomponent = definecomponent({ name 'timercomponent', oninit (entity) => { return { time 0 } }, onset (entity, component, json) => { if (typeof json? time === 'number') component time set(json time) }, tojson (entity, component) => { return { time component time value } } }) const timerquery = definequery(\[timercomponent]) const execute = () => { const { elapsedseconds } = getstate(enginestate) for (const entity of timerquery()) { const timercomponent = getmutablecomponent(entity, timercomponent) timercomponent time set(math floor(elapsedseconds)) } } export const timersystem = definesystem({ uuid 'timersystem', execute }) references entity component system overview in 7 minutes https //www youtube com/watch?v=2rw7alyhaasentity component system in typescript with phaser 3 and bitecs) https //www youtube com/watch?v=bviiao5 2 yoverwatch gdc ecs & netcode https //www youtube com/watch?v=w3aiehjynvw (note, ir engine does not use this style of networking)