Update documentation

This commit is contained in:
Ukendio 2025-01-18 00:13:34 +01:00
parent f66961fc9b
commit 61a7acae55
4 changed files with 343 additions and 282 deletions

View file

@ -1,16 +1,15 @@
# Assets # Addons
A collection of third-party jecs assets made by the community. If you would like to share what you're working on, [submit a pull request](https://github.com/Ukendio/jecs)! A collection of third-party jecs assets made by the community. If you would like to share what you're working on, [submit a pull request](https://github.com/Ukendio/jecs)!
# Debuggers ## [jabby](https://github.com/alicesaidhi/jabby)
## [jabby](https://github.com/alicesaidhi/jabby) A jecs debugger with a string-based query language and entity editing capabilities.
A jecs debugger with a string-based query language and entity editing capabilities.
# Schedulers
# Schedulers ## [lockstep scheduler](https://gist.github.com/1Axen/6d4f78b3454cf455e93794505588354b)
## [lockstep scheduler](https://gist.github.com/1Axen/6d4f78b3454cf455e93794505588354b) A simple fixed step system scheduler.
A simple fixed step system scheduler.
## [sapphire-jecs](https://github.com/Mark-Marks/sapphire/tree/main/crates/sapphire-jecs)
## [sapphire-jecs](https://github.com/Mark-Marks/sapphire/tree/main/crates/sapphire-jecs) A batteries-included [sapphire](https://github.com/mark-marks/sapphire) scheduler for jecs
A batteries-included [sapphire](https://github.com/mark-marks/sapphire) scheduler for jecs
## [jam](https://github.com/revvy02/Jam)
## [jam](https://github.com/revvy02/Jam) Provides hooks and a scheduler that implements jabby and a topographical runtime
Provides hooks and a scheduler that implements jabby and a topographical runtime

View file

@ -1,150 +1,185 @@
# Component Traits # Component Traits
Component traits are IDs and pairs that can be added to components to modify their behavior. Although it is possible to create custom traits, this manual only contains an overview of all builtin component traits supported by Jecs.
Component traits are IDs and pairs that can be added to components to modify their behavior. Although it is possible to create custom traits, this manual only contains an overview of all builtin component traits supported by Jecs.
# Component
Every (component) ID comes with a `Component` which helps with the distinction between normal entities and component IDs. # Component
# Tag Every (component) ID comes with a `Component` which helps with the distinction between normal entities and component IDs.
A (component) ID can be marked with `Tag´ in which the component will never contain any data. This allows for zero-cost components which improves performance for structural changes.
# Tag
# Hooks
Hooks are part of the "interface" of a component. You could consider hooks as the counterpart to OOP methods in ECS. They define the behavior of a component, but can only be invoked through mutations on the component data. You can only configure a single `OnAdd`, `OnRemove` and `OnSet` hook per component, just like you can only have a single constructor and destructor. A (component) ID can be marked with `Tag´ in which the component will never contain any data. This allows for zero-cost components which improves performance for structural changes.
## Examples # Hooks
::: code-group
Hooks are part of the "interface" of a component. You could consider hooks as the counterpart to OOP methods in ECS. They define the behavior of a component, but can only be invoked through mutations on the component data. You can only configure a single `OnAdd`, `OnRemove` and `OnSet` hook per component, just like you can only have a single constructor and destructor.
```luau [luau]
local Transform= world:component() ## Examples
world:set(Transform, OnAdd, function(entity)
-- A transform component has been added to an entity ::: code-group
end)
world:set(Transform, OnRemove, function(entity) ```luau [luau]
-- A transform component has been removed from the entity local Transform= world:component()
end) world:set(Transform, OnAdd, function(entity)
world:set(Transform, OnSet, function(entity, value) -- A transform component has been added to an entity
-- A transform component has been assigned/changed to value on the entity end)
end) world:set(Transform, OnRemove, function(entity)
``` -- A transform component has been removed from the entity
end)
```typescript [typescript] world:set(Transform, OnSet, function(entity, value)
-- A transform component has been assigned/changed to value on the entity
const Transform = world.component() end)
world.set(Transform, OnAdd, (entity) => { ```
// A transform component has been added to an entity
}) ```typescript [typescript]
world.set(Transform, OnRemove, (entity) => { const Transform = world.component();
// A transform component has been removed from the entity world.set(Transform, OnAdd, (entity) => {
}) // A transform component has been added to an entity
world.set(Transform, OnSet, (entity, value) => { });
// A transform component has been assigned/changed to value on the entity world.set(Transform, OnRemove, (entity) => {
}) // A transform component has been removed from the entity
});
``` world.set(Transform, OnSet, (entity, value) => {
// A transform component has been assigned/changed to value on the entity
::: });
```
# Cleanup Traits
When entities that are used as tags, components, relationships or relationship targets are deleted, cleanup traits ensure that the store does not contain any dangling references. Any cleanup policy provides this guarantee, so while they are configurable, games cannot configure traits that allows for dangling references. :::
We also want to specify this per relationship. If an entity has `(Likes, parent)` we may not want to delete that entity, meaning the cleanup we want to perform for `Likes` and `ChildOf` may not be the same. # Cleanup Traits
This is what cleanup traits are for: to specify which action needs to be executed under which condition. They are applied to entities that have a reference to the entity being deleted: if I delete the `Archer` tag I remove the tag from all entities that have it. When entities that are used as tags, components, relationships or relationship targets are deleted, cleanup traits ensure that the store does not contain any dangling references. Any cleanup policy provides this guarantee, so while they are configurable, games cannot configure traits that allows for dangling references.
To configure a cleanup policy for an entity, a `(Condition, Action)` pair can be added to it. If no policy is specified, the default cleanup action (`Remove`) is performed. We also want to specify this per relationship. If an entity has `(Likes, parent)` we may not want to delete that entity, meaning the cleanup we want to perform for `Likes` and `ChildOf` may not be the same.
There are two cleanup actions: This is what cleanup traits are for: to specify which action needs to be executed under which condition. They are applied to entities that have a reference to the entity being deleted: if I delete the `Archer` tag I remove the tag from all entities that have it.
- `Remove`: removes instances of the specified (component) id from all entities (default) To configure a cleanup policy for an entity, a `(Condition, Action)` pair can be added to it. If no policy is specified, the default cleanup action (`Remove`) is performed.
- `Delete`: deletes all entities with specified id
There are two cleanup actions:
There are two cleanup conditions:
- `Remove`: removes instances of the specified (component) id from all entities (default)
- `OnDelete`: the component, tag or relationship is deleted - `Delete`: deletes all entities with specified id
- `OnDeleteTarget`: a target used with the relationship is deleted
There are two cleanup conditions:
## Examples
The following examples show how to use cleanup traits - `OnDelete`: the component, tag or relationship is deleted
- `OnDeleteTarget`: a target used with the relationship is deleted
### (OnDelete, Remove)
::: code-group ## Examples
```luau [luau] The following examples show how to use cleanup traits
local Archer = world:component()
world:add(Archer, pair(jecs.OnDelete, jecs.Remove)) ### (OnDelete, Remove)
local e = world:entity() ::: code-group
world:add(e, Archer)
```luau [luau]
-- This will remove Archer from e local Archer = world:component()
world:delete(Archer) world:add(Archer, pair(jecs.OnDelete, jecs.Remove))
```
local e = world:entity()
```typescript [typescript] world:add(e, Archer)
const Archer = world.component()
world.add(Archer, pair(jecs.OnDelete, jecs.Remove)) -- This will remove Archer from e
world:delete(Archer)
const e = world:entity() ```
world.add(e, Archer)
```typescript [typescript]
// This will remove Archer from e const Archer = world.component();
world.delete(Archer) world.add(Archer, pair(jecs.OnDelete, jecs.Remove));
```
const e = world.entity();
::: world.add(e, Archer);
### (OnDelete, Delete) // This will remove Archer from e
::: code-group world.delete(Archer);
```
```luau [luau]
local Archer = world:component() :::
world:add(Archer, pair(jecs.OnDelete, jecs.Delete))
### (OnDelete, Delete)
local e = world:entity()
world:add(e, Archer) ::: code-group
-- This will delete entity e because the Archer component has a (OnDelete, Delete) cleanup trait ```luau [luau]
world:delete(Archer) local Archer = world:component()
``` world:add(Archer, pair(jecs.OnDelete, jecs.Delete))
```typescript [typescript] local e = world:entity()
const Archer = world.component() world:add(e, Archer)
world.add(Archer, pair(jecs.OnDelete, jecs.Delete))
-- This will delete entity e because the Archer component has a (OnDelete, Delete) cleanup trait
const e = world:entity() world:delete(Archer)
world.add(e, Archer) ```
// This will delete entity e because the Archer component has a (OnDelete, Delete) cleanup trait ```typescript [typescript]
world.delete(Archer) const Archer = world.component();
``` world.add(Archer, pair(jecs.OnDelete, jecs.Delete));
::: const e = world.entity();
world.add(e, Archer);
### (OnDeleteTarget, Delete)
::: code-group // This will delete entity e because the Archer component has a (OnDelete, Delete) cleanup trait
world.delete(Archer);
```luau [luau] ```
local ChildOf = world:component()
world:add(ChildOf, pair(jecs.OnDeleteTarget, jecs.Delete)) :::
local parent = world:entity() ### (OnDeleteTarget, Remove)
local child = world:entity()
world:add(child, pair(ChildOf, parent)) ::: code-group
-- This will delete both parent and child ```luau [luau]
world:delete(parent) local OwnedBy = world:component()
``` world:add(OwnedBy, pair(jecs.OnDeleteTarget, jecs.Remove))
local loot = world:entity()
```typescript [typescript] local player = world:entity()
const Archer = world.component() world:add(loot, pair(OwnedBy, player))
world.add(Archer, pair(jecs.OnDelete, jecs.Remove))
-- This will remove (OwnedBy, player) from loot
const e = world:entity() world:delete(player)
world.add(e, Archer) ```
// This will delete e ```typescript [typescript]
world.delete(Archer) const OwnedBy = world.component();
``` world.add(OwnedBy, pair(jecs.OnDeleteTarget, jecs.Remove));
const loot = world.entity();
::: const player = world.entity();
world.add(loot, pair(OwnedBy, player));
This page takes wording and terminology directly from Flecs [documentation](https://www.flecs.dev/flecs/md_docs_2ComponentTraits.html)
// This will remove (OwnedBy, player) from loot
world.delete(player);
```
### (OnDeleteTarget, Delete)
::: code-group
```luau [luau]
local ChildOf = world:component()
world:add(ChildOf, pair(jecs.OnDeleteTarget, jecs.Delete))
local parent = world:entity()
local child = world:entity()
world:add(child, pair(ChildOf, parent))
-- This will delete both parent and child
world:delete(parent)
```
```typescript [typescript]
const ChildOf = world.component();
world.add(ChildOf, pair(jecs.OnDeleteTarget, jecs.Delete));
const parent = world.entity();
const child = world.entity();
world.add(child, pair(ChildOf, parent));
// This will delete both parent and child
world.delete(parent);
```
:::
This page takes wording and terminology directly from Flecs [documentation](https://www.flecs.dev/flecs/md_docs_2ComponentTraits.html)

View file

@ -1,113 +1,140 @@
# Entities and Components # Entities and Components
## Entities ## Entities
Entities represent things in a game. In a game there may be entities of characters, buildings, projectiles, particle effects etc.
Entities represent things in a game. In a game there may be entities of characters, buildings, projectiles, particle effects etc.
By itself, an entity is just an unique identifier without any data
By itself, an entity is just an unique identifier without any data
## Components
A component is something that is added to an entity. Components can simply tag an entity ("this entity is an `Npc`"), attach data to an entity ("this entity is at `Position` `Vector3.new(10, 20, 30)`") and create relationships between entities ("bob `Likes` alice") that may also contain data ("bob `Eats` `10` apples"). ## Components
### Operations A component is something that is added to an entity. Components can simply tag an entity ("this entity is an `Npc`"), attach data to an entity ("this entity is at `Position` `Vector3.new(10, 20, 30)`") and create relationships between entities ("bob `Likes` alice") that may also contain data ("bob `Eats` `10` apples").
Operation | Description
----------|------------ ## Operations
`get` | Get a specific component or set of components from an entity.
`add` | Adds component to an entity. If entity already has the component, `add` does nothing. | Operation | Description |
`set` | Sets the value of a component for an entity. `set` behaves as a combination of `add` and `get` | --------- | ---------------------------------------------------------------------------------------------- |
`remove` | Removes component from entity. If entity doesn't have the component, `remove` does nothing. | `get` | Get a specific component or set of components from an entity. |
`clear` | Remove all components from an entity. Clearing is more efficient than removing one by one. | `add` | Adds component to an entity. If entity already has the component, `add` does nothing. |
| `set` | Sets the value of a component for an entity. `set` behaves as a combination of `add` and `get` |
### Components are entities | `remove` | Removes component from entity. If entity doesn't have the component, `remove` does nothing. |
| `clear` | Remove all components from an entity. Clearing is more efficient than removing one by one. |
In an ECS, components need to be uniquely identified. In Jecs this is done by making each component its own unique entity. If a game has a component Position and Velocity, there will be two entities, one for each component. Component entities can be distinguished from "regular" entities as they have a `Component` component. An example:
## Components are entities
::: code-group
In an ECS, components need to be uniquely identified. In Jecs this is done by making each component its own unique entity. If a game has a component Position and Velocity, there will be two entities, one for each component. Component entities can be distinguished from "regular" entities as they have a `Component` component. An example:
```luau [luau]
local Position = world:component() :: jecs.Entity<Vector3> ::: code-group
print(world:has(Position, Jecs.Component))
``` ```luau [luau]
local Position = world:component() :: jecs.Entity<Vector3>
```typescript [typescript] print(world:has(Position, jecs.Component))
const Position = world.component<Vector3>(); ```
print(world.has(Position, Jecs.Component))
``` ```typescript [typescript]
const Position = world.component<Vector3>();
::: print(world.has(Position, jecs.Component));
```
All of the APIs that apply to regular entities also apply to component entities. This means it is possible to contexualize components with logic by adding traits to components
:::
::: code-group
All of the APIs that apply to regular entities also apply to component entities. This means it is possible to contexualize components with logic by adding traits to components
```luau [luau]
local Networked = world:component() ::: code-group
local Type = world:component()
local Name = world:component() ```luau [luau]
local Position = world:component() :: jecs.Entity<Vector3> local Networked = world:component()
world:add(Position, Networked) local Type = world:component()
world:set(Position, Name, "Position") local Name = world:component()
world:set(Position, Type, { size = 12, type = "Vector3" } ) -- 12 bytes to represent a Vector3 local Position = world:component() :: jecs.Entity<Vector3>
world:add(Position, Networked)
for id, ty, name in world:query(Type, Name):with(Networked) do world:set(Position, Name, "Position")
local batch = {} world:set(Position, Type, { size = 12, type = "Vector3" } ) -- 12 bytes to represent a Vector3
for entity, data in world:query(id) do
table.insert(batch, { entity = entity, data = data }) for id, ty, name in world:query(Type, Name, Networked) do
end local batch = {}
-- entities are sized f64 for entity, data in world:query(id) do
local packet = buffer.create(#batch * (8 + ty.size)) table.insert(batch, { entity = entity, data = data })
local offset = 0 end
for _, entityData in batch do -- entities are sized f64
offset+=8 local packet = buffer.create(#batch * (8 + ty.size))
buffer.writef64(packet, offset, entityData.entity) local offset = 0
if ty.type == "Vector3" then for _, entityData in batch do
local vec3 = entity.data :: Vector3 offset+=8
offset += 4 buffer.writef64(packet, offset, entityData.entity)
buffer.writei32(packet, offset, vec3.X) if ty.type == "Vector3" then
offset += 4 local vec3 = entity.data :: Vector3
buffer.writei32(packet, offset, vec3.Y) offset += 4
offset += 4 buffer.writei32(packet, offset, vec3.X)
buffer.writei32(packet, offset, vec3.Z) offset += 4
end buffer.writei32(packet, offset, vec3.Y)
end offset += 4
buffer.writei32(packet, offset, vec3.Z)
updatePositions:FireServer(packet) end
end end
```
updatePositions:FireServer(packet)
```typescript [typescript] end
const Networked = world.component() ```
const Type = world.component()
const Name = world.component() ```typescript [typescript]
const Position = world.component<Vector3>(); const Networked = world.component();
world.add(Position, Networked) const Type = world.component();
world.set(Position, Name, "Position") const Name = world.component();
world.set(Position, Type, { size: 12, type: "Vector3" } ) // 12 bytes to represent a Vector3 const Position = world.component<Vector3>();
world.add(Position, Networked);
for (const [id, ty, name] of world.query(Type, Name).with(Networked)) { world.set(Position, Name, "Position");
const batch = new Array<{ entity: Entity, data: unknown}>() world.set(Position, Type, { size: 12, type: "Vector3" }); // 12 bytes to represent a Vector3
for (const [entity, data] of world.query(id)) { for (const [id, ty, name] of world.query(Type, Name, Networked)) {
batch.push({ entity, data }) const batch = new Array<{ entity: Entity; data: unknown }>();
}
// entities are sized f64 for (const [entity, data] of world.query(id)) {
const packet = buffer.create(batch.size() * (8 + ty.size)) batch.push({ entity, data });
const offset = 0 }
for (const [_, entityData] of batch) { // entities are sized f64
offset+=8 const packet = buffer.create(batch.size() * (8 + ty.size));
buffer.writef64(packet, offset, entityData.entity) const offset = 0;
if (ty.type == "Vector3") { for (const [_, entityData] of batch) {
const vec3 = entity.data as Vector3 offset += 8;
offset += 4 buffer.writef64(packet, offset, entityData.entity);
buffer.writei32(packet, offsetm, vec3.X) if (ty.type == "Vector3") {
offset += 4 const vec3 = entity.data as Vector3;
buffer.writei32(packet, offset, vec3.Y) offset += 4;
offset += 4 buffer.writei32(packet, offsetm, vec3.X);
buffer.writei32(packet, offset, vec3.Z) offset += 4;
} buffer.writei32(packet, offset, vec3.Y);
} offset += 4;
buffer.writei32(packet, offset, vec3.Z);
updatePositions.FireServer(packet) }
} }
```
updatePositions.FireServer(packet);
::: }
```
:::
## Singletons
Singletons are components for which only a single instance
exists on the world. They can be accessed on the
world directly and do not require providing an entity.
Singletons are useful for global game resources, such as
game state, a handle to a physics engine or a network socket. An example:
::: code-group
```luau [luau]
local TimeOfDay = world:component() :: jecs.Entity<number>
world:set(TimeOfDay, TimeOfDay, 0.5)
local t = world:get(TimeOfDay, TimeOfDay)
```
```typescript [typescript]
const TimeOfDay = world.component<number>();
world.set(TimeOfDay, TimeOfDay, 0.5);
const t = world.get(TimeOfDay, TimeOfDay);
```
:::

View file

@ -17,14 +17,14 @@ This manual contains a full overview of the query features available in Jecs. So
## Performance and Caching ## Performance and Caching
Understanding the basic architecture of queries helps to make the right tradeoffs when using queries in games. Understanding the basic architecture of queries helps to make the right tradeoffs when using queries in games.
The biggest impact on query performance is whether a query is cached or not. The biggest impact on query performance is whether a query is cached or not.
This section goes over what caching is, how it can be used and when it makes sense to use it. This section goes over what caching is, how it can be used and when it makes sense to use it.
### Caching: what is it? ### Caching: what is it?
Jecs is an archetype ECS, which means that entities with exactly the same components are grouped together in an "archetype". Archetypes are created on the fly whenever a new component combination is created in the ECS. For example: Jecs is an archetype ECS, which means that entities with exactly the same components are
grouped together in an "archetype". Archetypes are created on the fly
whenever a new component combination is created in the ECS. For example:
:::code-group :::code-group