Returns an iterator over all the entities in the world.
Removes every entity, and destroys all components.
Creates an entity, and optionally assigns all components
to it.
Destroys an entity and all its components
Calls .free()
(if available) on each destroyed component
Example:
class A { free() { console.log("A freed"); } }
const world = new World();
const entity = world.create(new A);
world.destroy(entity); // logs "A freed"
Sets entity
's instance of component type
to component
.
Returns true if entity
exists in this World
Retrieves component
belonging to entity
. Returns undefined
if it the entity doesn't have component
, or the entity
doesn't exist.
Example:
class A { value = 50 }
class B {}
const world = new World();
const entity = world.create();
world.emplace(entity, new A);
world.get(entity, A).value; // 50
world.get(entity, A).value = 10;
world.get(entity, A).value; // 10
world.get(entity, B); // undefined
world.get(100, A); // undefined
Returns true
if entity
exists AND has component
, false otherwise.
Example:
class A {}
const world = new World();
const entity = world.create();
world.has(entity, A); // false
world.emplace(entity, new A);
world.has(entity, A); // true
world.has(100, A); // false
Inserts the entity
, and optionally assigns all components
to it.
If the entity already exists, all components
will be assigned to it.
If it already has some other components, they won't be destroyed:
class A { constructor(value = 0) { this.value = value } }
class B { constructor(value = 0) { this.value = value } }
const world = new World;
const entity = world.create(new A, new B);
world.get(entity, A); // A { value: 0 }
world.insert(entity, new A(5));
world.get(entity, A); // A { value: 5 }
world.get(entity, B); // B { value: 0 }
You can first check if the entity exists, destroy it if so, and then insert it.
if (world.exists(entity)) {
world.destroy(entity);
}
world.insert(entity, new A, new B, ...);
Removes instance of component
from entity
, and returns the removed component.
Returns undefined
if nothing was removed, or if entity
does not exist.
Example:
class A { value = 10 }
const world = new World();
const entity = world.create();
world.emplace(entity, new A);
world.get(entity, A).value = 50
world.remove(entity, A); // A { value: 50 }
world.remove(entity, A); // undefined
This does not call .free()
on the component. The reason for this is that
you don't always want to free the removed component. Don't fret, you can still
free component, because the World.remove
call returns it! Example:
class F { free() { console.log("freed") } }
const world = new World;
const entity = world.create(new F);
world.remove(entity, F).free();
// you can use optional chaining to easily guard against the 'undefined' case:
world.remove(entity, F)?.free();
Returns the size of the world (how many entities are stored)
Used to query for entities with specific component combinations and efficiently iterate over the result.
Example:
class Fizz { }
class Buzz { }
const world = new World();
for (let i = 0; i < 100; ++i) {
const entity = world.create();
if (i % 3 === 0) world.emplace(entity, new Fizz);
if (i % 5 === 0) world.emplace(entity, new Buzz);
}
world.view(Fizz, Buzz).each((n) => {
console.log(`FizzBuzz! (${n})`);
});
Generated using TypeDoc
World is the core of the ECS. It stores all entities and their components, and enables efficiently querying them.
Visit https://jprochazk.github.io/uecs/ for a comprehensive tutorial.