world
| Type | Class |
| Namespace | conjure::ecs |
| Include File Path | engine/include/conjure/ecs/world.h |
| Source File Path | engine/src/ecs/world.cpp |
| API ↗ | Archives and serialisation, Command buffers and observers, Entities and components, Entities, components and systems, Queries, views and systems |
Syntax
Constructors
| Access | Signature | Description |
|---|---|---|
| public | explicit world(
const component_registry ®istry,
world_desc desc = {}); |
Important
Copy constructor and assignment has been deleted. Move constructors have been deleted. Move assignment has been deleted.
Destructors
| Access | Signature | Description |
|---|---|---|
| public | ~world(); | Destroys every entity without firing observers and returns every chunk. |
Variables
| Access | Name | Type | Default | Description |
|---|---|---|---|---|
| private | FLUSH_ | static constexpr usize | 4 | Payload records a flush plan entry stores inline: a spawn with up to this many add/set payloads costs the plan no heap allocation. |
| private | mAliveCount | u32 | 0 | |
| private | mAllocator | std:: | {} | |
| private | m | std:: | {} | |
| private | m | std::vector<archetype *> | {} | |
| private | mArchetypes | std:: | {} | |
| private | mChunkPool | std:: | {} | |
| private | mDeferred | std:: | {} | |
| private | m | std:: | {} | |
| private | m | archetype * | nullptr | |
| private | m | std::vector<u64> | {} | |
| private | mFlushPlan | std::vector<flush_entry> | {} | |
| private | mFlushSlots | std::vector<flush_slot> | {} | |
| private | mFlushStamp | u32 | 0 | |
| private | mInObserver | bool | false | |
| private | mLockDepth | std::atomic<i32> | 0 | |
| private | mName | std::string | {} | |
| private | mObservers | std:: | {} | |
| private | mOwnerThread | std::thread::id | {} | |
| private | m | std::vector<entity> | {} | |
| private | mRecords | std:: | {} | |
| private | mRegistry | const component_ | nullptr | |
| private | m | u32 | 0 | |
| private | mVersion | std::atomic<u64> | 1 |
Functions
Public
add
addtemplate <typename ComponentType> [[nodiscard]] core::result<ComponentType *> add(
entity target,
ComponentType value = {});Adds a component with an initial value and fires ADDED observers once the value is in place. Returns the stored component: nullptr for a tag, and also when an ADDED observer's deferred work (drained before add returns) removed the component again or destroyed the entity; check the pointer when observers may do that. ENTITY_NOT_ALIVE, COMPONENT_ALREADY_PRESENT, UNKNOWN_COMPONENT, structural errors.
add_raw
add_raw[[nodiscard]] core::result<void *> add_raw(
entity target,
component_id id,
std::span<const std::byte> initial = {});Adds a component from bytes (ops.copy or memcpy; empty = default value) and returns the stored data: nullptr for a tag, and also when an ADDED observer's deferred work removed the component again or destroyed the entity, as with add(). Same errors as add().
archetypes
archetypes[[nodiscard]] std::span<archetype *const> archetypes() const noexcept;Every archetype in creation order. Pointers stay valid for the world's lifetime.
bump_version
bump_versionu64 bump_version() noexcept;Increments the version and returns the new value. Any thread.
clear
clearvoid clear();Destroys every entity (firing observers) and returns every chunk, the parked chunk metadata and the structural scratch (flush plan, placement undo list, the deferred buffers' room) to the heap. Archetypes, observers and the id space are kept: destroyed ids come back with a bumped generation, so handles into the cleared world stay stale and ids reserved by command buffers stay valid. Owner thread, unlocked.
create_entities
create_entities[[nodiscard]] core::result<void> create_entities(
const component_mask &mask,
std::span<entity> out);Creates out.size() entities of one archetype chunk by chunk, writes their ids and fires ADDED observers for each, which see the default values. create_entities(mask, out, {}).
[[nodiscard]] core::result<void> create_entities(
const component_mask &mask,
std::span<entity> out,
std::span<const component_init> inits);Like create_entities(mask, out), but copies each init into the new rows (the registered copy, a memcpy for a trivial type) after placement and before the ADDED observers fire, so the observers see the values, as with add(). Inits apply in order, so a later init of the same id wins. Every new column is stamped as written by this call, the copied ones included. The caller's values must cover out.size() entities at each stride; a non-zero stride below the component size overlaps the values, which is the caller's choice. UNKNOWN_COMPONENT, before anything is placed, when an init id is unregistered or not in mask (also for an empty out); otherwise the errors of create_entity, and a failed call creates no entity.
create_entity
create_entity[[nodiscard]] core::result<entity> create_entity(
const component_mask &mask = {});Creates one entity in the archetype of mask (default-constructed components) and fires ADDED observers. ARCHETYPE_TOO_LARGE, OUT_OF_MEMORY, ENTITY_LIMIT_REACHED, STRUCTURE_LOCKED, WRONG_THREAD, UNKNOWN_COMPONENT.
destroy_entity
destroy_entity[[nodiscard]] core::result<void> destroy_entity(
entity target);Fires REMOVED and destroyed observers, destroys the components and frees the id. The deferred buffer observers filled is drained before returning. ENTITY_NOT_ALIVE for a dead or stale entity. Never cascades.
flush
flush[[nodiscard]] flush_report flush(
command_buffer &commands);Applies a command buffer: one archetype move per touched entity, destroys last, observers for the mask delta only. Commands on dead or stale entities are skipped and counted. A destroy of the root an instantiation of the same buffer returned cancels that copy: the archive is not placed for it, its ids are handed back with a bumped generation and the root never becomes alive, as a reserved id the buffer destroys. The buffer is empty afterwards. A structural violation leaves the buffer untouched and reports nothing applied.
get
gettemplate <typename ComponentType> [[nodiscard]] ComponentType *get(
entity target) noexcept;The component; a non-const T is for writing and stamps its chunk column, a const T never stamps. nullptr when dead or absent.
template <typename ComponentType> [[nodiscard]] const ComponentType *get(
entity target) const noexcept;The component for reading. nullptr when dead or absent. Never stamps.
get_raw
get_raw[[nodiscard]] void *get_raw(
entity target,
component_id id,
bool forWrite) noexcept;Component data by id; forWrite stamps the column. nullptr when dead, absent or a tag.
[[nodiscard]] const void *get_raw(
entity target,
component_id id) const noexcept;Read-only component data by id. nullptr when dead, absent or a tag. Safe from grain jobs while the world is locked (an iteration_scope, a system stage, parallel_for_chunks): no structural change can move a row meanwhile, so a job may read another entity's component this way (the scene's transform pass reads parent columns through it) as long as no grain is writing that column at the same time.
handle
handle[[nodiscard]] entity_handle handle(
entity target) noexcept;An OOP handle to an entity (entity_handle.h). Defined in entity_handle.h.
has
hastemplate <typename ComponentType> [[nodiscard]] bool has(
entity target) const noexcept;True when the entity is alive and holds the component.
has_raw
has_raw[[nodiscard]] bool has_raw(
entity target,
component_id id) const noexcept;True when the entity is alive and its archetype holds id.
instance_count
instance_count[[nodiscard]] u32 instance_count(
component_id id) const noexcept;Number of alive entities holding id.
instantiate
instantiate[[nodiscard]] core::result<entity> instantiate(
const std::shared_ptr<const entity_archive> &archive,
command_buffer *commands = nullptr);Instantiates one copy of an archive and returns its root (entry 0). With a command buffer the copy is recorded and applied at flush; the root id is still returned now. The buffer must target this world (INVALID_ARGUMENT otherwise, as instantiate_batch).
instantiate_batch
instantiate_batch[[nodiscard]] core::result<void> instantiate_batch(
const std::shared_ptr<const entity_archive> &archive,
std::span<entity> outRoots,
command_buffer *commands = nullptr,
std::vector<entity> *outEntities = nullptr);Instantiates outRoots.size() copies of an archive, placing entities directly in their final archetypes and rewiring internal entity references. ARCHIVE_CORRUPT before anything is created when the archive does not validate against this world's registry. When given, outEntities receives every created id, copy-major in entry order (copies x entity_count() entries, entries without components included); with a command buffer those are the ids reserved for the flush. After a failed call outEntities is empty. INVALID_ARGUMENT (asserted in Debug and Development) when the command buffer targets another world; nothing is reserved then.
is_alive
is_alive[[nodiscard]] bool is_alive(
entity target) const noexcept;True when the entity is alive with this generation. Any thread while locked.
is_locked
is_locked[[nodiscard]] bool is_locked() const noexcept;True while at least one iteration_scope is alive.
lock_iteration
lock_iteration[[nodiscard]] iteration_scope lock_iteration() noexcept;Locks the world for iteration until the returned scope dies. Any thread.
mask_of
mask_of[[nodiscard]] component_mask mask_of(
entity target) const noexcept;The component set of an entity; empty for a dead one.
observe
observe[[nodiscard]] observer_id observe(
component_id id,
component_event event,
component_observer_fn fn);Registers a component observer. Observers run on the owner thread inside the structural operation and may only enqueue into the deferred buffer they receive. Off the owner thread, an unregistered id, an event past REMOVED or an empty callable asserts and returns an invalid observer_id.
observe_destroyed
observe_destroyed[[nodiscard]] observer_id observe_destroyed(
entity_observer_fn fn);Registers an observer called before an entity is destroyed.
owner_thread
owner_thread[[nodiscard]] std::thread::id owner_thread() const noexcept;The thread structural operations must run on.
registry
registry[[nodiscard]] const component_registry ®istry() const noexcept;The registry this world was created on.
remove
removetemplate <typename ComponentType> [[nodiscard]] core::result<void> remove(
entity target);Removes a component after firing REMOVED observers. COMPONENT_NOT_PRESENT when absent.
remove_from_all
remove_from_all[[nodiscard]] core::result<void> remove_from_all(
component_id id);Removes id from every entity holding it (module unload, dynamic type redefinition).
remove_raw
remove_raw[[nodiscard]] core::result<void> remove_raw(
entity target,
component_id id);Removes a component by id.
set
settemplate <typename ComponentType> [[nodiscard]] core::result<void> set(
entity target,
ComponentType value);Replaces the value of an existing component and stamps its column. Not structural. ENTITY_NOT_ALIVE, COMPONENT_NOT_PRESENT.
set_owner_thread
set_owner_thread[[nodiscard]] core::result<void> set_owner_thread(
std::thread::id thread);Hands ownership to another thread. Only while unlocked (STRUCTURE_LOCKED otherwise).
validate
validate[[nodiscard]] core::result<void> validate() const;Full invariant check: records and rows agree, chunk counts sum to archetype counts, only the last chunk of an archetype is partial, free-list indices are FREE, every archetype mask maps back to itself, column offsets are aligned and inside the block. The error message names the first violated invariant.
version
version[[nodiscard]] u64 version() const noexcept;The current change-detection version (starts at 1, never decreases).
view
viewtemplate <typename... ComponentTypes> [[nodiscard]] ecs::view<ComponentTypes...> view();A typed view over this world (view.h).
Private
add_storage
add_storage[[nodiscard]] core::result<void *> add_storage(
entity target,
component_id id);archetype_for
archetype_for[[nodiscard]] core::result<archetype *> archetype_for(
const component_mask &mask);archetype_with
archetype_with[[nodiscard]] core::result<archetype *> archetype_with(
archetype &source,
component_id id,
bool added);begin_structural
begin_structuralvoid begin_structural() noexcept;cancel_destroyed_copies
cancel_destroyed_copiesvoid cancel_destroyed_copies(
command_buffer::instantiate_entry &entry,
std::span<const u64> destroyed);check_structural
check_structural[[nodiscard]] core::result<void> check_structural() const;construct_column_default
construct_column_defaultvoid construct_column_default(
const component_info &info,
void *destination,
u32 count) noexcept;destroy_alive
destroy_alivevoid destroy_alive(
entity target,
entity_record &record,
bool fireObservers);destruct_column
destruct_columnvoid destruct_column(
const component_info &info,
void *object,
u32 count) noexcept;discard_placed
discard_placedvoid discard_placed(
entity target);drain_deferred
drain_deferredvoid drain_deferred();end_structural
end_structuralvoid end_structural();ensure_records
ensure_records[[nodiscard]] core::result<void> ensure_records(
u32 highestIndex);find_alive
find_alive[[nodiscard]] entity_record *find_alive(
entity target) noexcept;[[nodiscard]] const entity_record *find_alive(
entity target) const noexcept;finish_add
finish_addvoid finish_add(
entity target,
component_id id);fire_added
fire_addedvoid fire_added(
entity target,
const component_mask &added);fire_destroyed
fire_destroyedvoid fire_destroyed(
entity target);fire_removed
fire_removedvoid fire_removed(
entity target,
const component_mask &removed);flush_internal
flush_internal[[nodiscard]] flush_report flush_internal(
command_buffer &commands);instantiate_now
instantiate_now[[nodiscard]] core::result<void> instantiate_now(
const entity_archive &archive,
std::span<const entity> ids,
u32 copies);is_reserved
is_reserved[[nodiscard]] bool is_reserved(
entity target) const noexcept;move_entity
move_entity[[nodiscard]] core::result<void> move_entity(
entity target,
entity_record &record,
archetype &destination);place_rows
place_rows[[nodiscard]] core::result<void> place_rows(
archetype &target,
std::span<const entity> ids);relayout_archetypes
relayout_archetypesvoid relayout_archetypes(
component_id id);release_reserved
release_reservedvoid release_reserved(
entity target);write_inits
write_initsvoid write_inits(
archetype &target,
std::span<const entity> ids,
std::span<const component_init> inits) noexcept;