|
Nebula
|
#include <world.h>
A container of entities, their components, and processors.
Worlds can be identified by their hash, or their id.
A game world keeps track of:
Components are stored as columns in a database, while an entity maps to a row of multiple component columns, in a table. The database splits entities based on their components, so all entities with the same components are stored in the same table. "Adding or removing" a component from an entity means moving it from one table to another.
Processors are functions that process entity components data. They loop over all entities that fulfill some condition of having certain components or not, and runs a user defined processing function over this data.
The game frame pipeline stores all the processor in order of execution.
Classes | |
| struct | AllocateInstanceCommand |
| struct | DeallocInstanceCommand |
| struct | AddStagedComponentCommand |
| struct | RemoveComponentCommand |
Public Member Functions | |
| World (WorldHash hash, WorldId id) | |
| ~World () | |
| WorldHash | GetWorldHash () const |
| Returns the world hash for this world. | |
| WorldId | GetWorldId () const |
| Returns the world ID for this world. This corresponds to Game::Entity::world. | |
| Ptr< TransformHierarchy > | GetHierarchy () const |
| The transform hierarchy of this world. | |
| Entity | CreateEntity (bool immediate=true) |
| Create a new empty entity. | |
| void | DeleteEntity (Entity entity) |
| Delete entity. | |
| bool | IsValid (Entity entity) const |
| Check if an entity ID is still valid. | |
| bool | HasInstance (Entity entity) const |
| Check if an entity has an instance. It might be valid, but not have received an instance just after it has been created. | |
| EntityMapping | GetEntityMapping (Entity entity) const |
| Returns the entity mapping of an entity. | |
| SizeT | GetNumEntities () const |
| Number of entity ids currently allocated in this world. | |
| void | SetEntityCreatedCallback (EntityCreatedCallback const &callback) |
| Set the callback invoked for every entity instance allocated in this world. | |
| void | EnumerateEntities (EntityCreatedCallback const &callback) |
| Call callback for every entity in the world that has an instance in the database. | |
| template<typename TYPE> | |
| TYPE * | AddComponent (Entity entity) |
| Create a component. This queues the component in a command buffer to be added later. | |
| template<typename TYPE> | |
| void | AddComponent (Entity entity, TYPE const &component) |
| Add a component to an entity. | |
| void * | AddComponent (Entity entity, ComponentId component) |
| Queues a component to be added to the entity in a command buffer. | |
| void * | AddComponent (Entity entity, ComponentId component, const void *value) |
| Queues a component with initial data, copied before component initialization. | |
| template<typename TYPE> | |
| bool | HasComponent (Entity entity) const |
| Check if entity has a specific component. | |
| bool | HasComponent (Entity entity, ComponentId component) const |
| Check if entity has a specific component. (SLOW!). | |
| byte * | FindComponentData (Entity entity, ComponentId component) const |
| The data of a component, read from the entity's table when it is there, otherwise from its chain of staged commands. | |
| template<typename TYPE> | |
| void | RemoveComponent (Entity entity) |
| Remove a component from an entity. | |
| void | RemoveComponent (Entity entity, ComponentId component) |
| Remove a component from an entity. Only a component which is in the entity's table. | |
| template<typename TYPE> | |
| void | SetComponent (Entity entity, TYPE const &value) |
| Set the value of an entitys component. | |
| template<typename TYPE> | |
| TYPE | GetComponent (Entity entity) |
| Get an entitys component. | |
| void | MarkAsModified (Game::Entity entity) |
| Mark an entity as modified in its table. | |
| Dataset | Query (Filter filter) |
| Query the entity database using specified filter set. This does NOT wait for resources to be available. | |
| Dataset | Query (Filter filter, Util::Array< MemDb::TableId > &tids) |
| Query a subset of tables using a specified filter set. | |
| ComponentDecayBuffer const | GetDecayBuffer (ComponentId component) |
| Get a decay buffer for the given component. | |
| void | DecayComponentValue (Entity entity, ComponentId component) |
| Put the value an entity's component holds into the component's decay buffer, the way removing it would, and leave the component on the entity. | |
| PackedLevel * | PreloadLevel (Util::String const &path) |
| preload a level that can be instantiated | |
| void | UnloadLevel (PackedLevel *level) |
| unload a preloaded level | |
| bool | ExportLevel (Util::String const &path) |
| Export the world as a level. | |
| FramePipeline & | GetFramePipeline () |
| Get the frame pipeline. | |
| Ptr< MemDb::Database > | GetDatabase () |
| Get the entity database. Be careful when directly modifying the database, as some information is only kept track of via the World. | |
| MemDb::RowId | GetInstance (Entity entity) const |
| Get instance of entity. | |
| void | SetComponentValue (Entity entity, ComponentId component, void *value, uint64_t size) |
| Set the value of a component by providing a pointer and type size. | |
| void | ReinitializeComponent (Entity entity, ComponentId component, void *value, uint64_t size) |
| Set the value of a component by providing a pointer and type size, then reinitialize the component. | |
| MemDb::TableId | CreateEntityTable (EntityTableCreateInfo const &info) |
| Create a table in the entity database that has a specific set of components. | |
| Entity | AllocateEntityId () |
| Allocate an entity id. Use this with caution! | |
| void | DeallocateEntityId (Entity entity) |
| Deallocate an entity id. Use this with caution! | |
| MemDb::RowId | AllocateInstance (Entity entity) |
| Allocate an entity instance in default (empty) table without initializing components. Use this with caution! | |
| MemDb::RowId | AllocateInstance (Entity entity, MemDb::TableId table, Util::Blob const *const data=nullptr) |
| Allocate an entity instance in a table without initializing components. Use this with caution! | |
| void | InitializeInstance (Entity entity) |
| Initialize all components on an allocated entity instance. | |
| void | InitializeInstance (Entity entity, MemDb::TableId tableId, MemDb::RowId row) |
| Initialize all components using an already fetched table and row. | |
| void | FinalizeAllocate (Entity entity) |
| void | DeallocateInstance (MemDb::TableId table, MemDb::RowId instance, Entity entity) |
| Deallocate an entity instance. Use this with caution! | |
| void | DeallocateInstance (Entity entity) |
| Deallocate an entity instance. Use this with caution! | |
| void | Defragment (MemDb::TableId tableId) |
| Defragment an entity table. | |
| void * | GetInstanceBuffer (MemDb::TableId const tableId, uint16_t partitionId, ComponentId const component) |
| Get a pointer to the first instance of a component in a partition of an entity table. Use with caution! | |
| void * | GetColumnData (MemDb::TableId const tableId, uint16_t partitionId, MemDb::ColumnIndex const column) |
| Get a pointer to the first instance of a column in a partition of an entity table. Use with caution! | |
| void | ExecuteAddComponentCommands () |
| dispatches all staged components to be added to entities | |
| void | ExecuteRemoveComponentCommands () |
| dispatches all staged components to be removed from entities | |
| void | RenderDebug () |
| template<> | |
| Game::Position | GetComponent (Entity entity) |
| template<> | |
| Game::Orientation | GetComponent (Entity entity) |
| template<> | |
| Game::Scale | GetComponent (Entity entity) |
| template<> | |
| void | SetComponent (Entity entity, Game::Position const &value) |
| template<> | |
| void | SetComponent (Entity entity, Game::Orientation const &value) |
| template<> | |
| void | SetComponent (Entity entity, Game::Scale const &value) |
| template<> | |
| void | SetComponent (Entity entity, Game::HTransform const &value) |
| template<> | |
| Game::Position | GetComponent (Entity entity) |
| template<> | |
| Game::Orientation | GetComponent (Entity entity) |
| template<> | |
| Game::Scale | GetComponent (Entity entity) |
| template<> | |
| void | SetComponent (Entity entity, Game::Position const &) |
| template<> | |
| void | SetComponent (Entity entity, Game::Orientation const &) |
| template<> | |
| void | SetComponent (Entity entity, Game::Scale const &) |
| template<> | |
| void | SetComponent (Entity entity, Game::HTransform const &) |
| The local transform is the component which puts an entity in the transform hierarchy, which is why a write of it has to reach the instances below it. | |
Static Public Member Functions | |
| static void | Override (World *src, World *dst) |
| Copy src over dst without initializing components. This is extremely destructive. | |
Public Attributes | |
| bool | componentInitializationEnabled = true |
| Disable if initialization of components is not required (ex. when running as editor db). | |
| WorldMode | mode = PlayMode |
| the mode this world is currently running in, which decides which processors execute | |
Private Member Functions | |
| void | Start () |
| void | BeginFrame () |
| void | SimFrame () |
| void | EndFrame () |
| void | OnLoad () |
| void | OnSave () |
| void | ManageEntities () |
| void | Reset () |
| void | PrefilterProcessors () |
| void | ClearDecayBuffers () |
| Clears all decay buffers. This is called by the game server automatically. | |
| SizeT | GetNumInstances (MemDb::TableId tid) |
| Get total number of instances in an entity table. | |
| MemDb::RowId | Migrate (Entity entity, MemDb::TableId newTable) |
| Migrate an entity from it's current table to a different table. | |
| void | Migrate (Util::Array< Entity > const &entities, MemDb::TableId fromTable, MemDb::TableId newTable, Util::FixedArray< MemDb::RowId > &newInstances) |
| Migrate an array of entities within the same table to a different table. | |
| void | DecayComponent (ComponentId component, MemDb::TableId tableId, MemDb::ColumnIndex column, MemDb::RowId instance, Entity entity) |
| Copies the component to the decay table and runs its decay callback. | |
| void | AppendToDecayBuffer (ComponentId component, const void *value) |
| Copies a component value to the component's decay buffer. | |
| void | MoveInstance (MemDb::Table::Partition *partition, MemDb::RowId from, MemDb::RowId to) |
| Move a instance/row within a partition. | |
| void | AddStagedComponentsToEntity (Entity entity, AddStagedComponentCommand *cmds, SizeT numCmds) |
| Adds all components in cmds to entity. | |
| void | RemoveComponentsFromEntity (Entity entity, RemoveComponentCommand *cmds, SizeT numCmds) |
| Removes all components in cmds from entity. | |
| void * | StageComponent (Entity entity, ComponentId component, const void *value, SizeT typeSize) |
| Stage a component for an entity and put it on the entity's chain of staged commands. | |
| IndexT | StagedHead (Entity entity) const |
| The first command on the entity's chain of staged commands, or InvalidIndex. | |
| void * | FindStagedComponent (Entity entity, ComponentId component) const |
| A component on the entity's chain of staged commands, or nullptr. | |
Private Attributes | |
| EntityPool | pool |
| used to allocate entity ids for this world | |
| SizeT | numEntities |
| Number of entities alive. | |
| EntityCreatedCallback | entityCreatedCallback |
| Callback invoked for every entity instance allocated in this world. | |
| Util::Array< EntityMapping > | entityMap |
| maps entity index to table+row pair | |
| Util::Array< IndexT > | stagedHeads |
| the first staged command per entity index. | |
| Ptr< MemDb::Database > | db |
| contains all entity instances | |
| WorldHash | hash |
| world hash | |
| WorldId | worldId |
| world id | |
| Util::Queue< AllocateInstanceCommand > | allocQueue |
| Stores all deferred allocation commands. | |
| Util::Queue< DeallocInstanceCommand > | deallocQueue |
| Stores all deferred deallocation commands. | |
| Util::Array< AddStagedComponentCommand > | addStagedQueue |
| Stores all deferred add staged component commands. | |
| Util::Array< RemoveComponentCommand > | removeComponentQueue |
| Stores all deferred remove component commands. | |
| Memory::ArenaAllocator< 4096_KB > | componentStageAllocator |
| Allocator for staged components. | |
| Ptr< TransformHierarchy > | hierarchy |
| The transform hierarchy of the entities in this world. | |
| bool | cacheValid = false |
| Set to true if the caches for the frame pipeline are valid. | |
| FramePipeline | pipeline |
| The frame pipeline for this world. | |
| MemDb::TableId | defaultTableId |
| The default table that empty entities are instantiated into. | |
| Util::FixedArray< ComponentDecayBuffer > | componentDecayTable |
| Contains all the component decay buffers. Lookup directly via ComponentId. | |
Friends | |
| class | GameServer |
| class | PackedLevel |
| Game::World::~World | ( | ) |
|
inline |
Create a component. This queues the component in a command buffer to be added later.
| void * Game::World::AddComponent | ( | Entity | entity, |
| Game::ComponentId | id ) |
Queues a component to be added to the entity in a command buffer.
| void * Game::World::AddComponent | ( | Entity | entity, |
| Game::ComponentId | id, | ||
| const void * | value ) |
Queues a component with initial data, copied before component initialization.
|
inline |
Add a component to an entity.
This queues the component in a command buffer to be added later This version of AddComponent allows you to set the values of the component before the component is initialized.
|
private |
Adds all components in cmds to entity.
| Entity Game::World::AllocateEntityId | ( | ) |
Allocate an entity id. Use this with caution!
| MemDb::RowId Game::World::AllocateInstance | ( | Entity | entity | ) |
Allocate an entity instance in default (empty) table without initializing components. Use this with caution!
| MemDb::RowId Game::World::AllocateInstance | ( | Entity | entity, |
| MemDb::TableId | table, | ||
| Util::Blob const *const | data = nullptr ) |
Allocate an entity instance in a table without initializing components. Use this with caution!
|
private |
Copies a component value to the component's decay buffer.
|
private |
|
private |
Clears all decay buffers. This is called by the game server automatically.
| Game::Entity Game::World::CreateEntity | ( | bool | immediate = true | ) |
Create a new empty entity.
| MemDb::TableId Game::World::CreateEntityTable | ( | EntityTableCreateInfo const & | info | ) |
Create a table in the entity database that has a specific set of components.
| void Game::World::DeallocateEntityId | ( | Entity | entity | ) |
Deallocate an entity id. Use this with caution!
| void Game::World::DeallocateInstance | ( | Entity | entity | ) |
Deallocate an entity instance. Use this with caution!
| void Game::World::DeallocateInstance | ( | MemDb::TableId | table, |
| MemDb::RowId | instance, | ||
| Entity | entity ) |
Deallocate an entity instance. Use this with caution!
|
private |
Copies the component to the decay table and runs its decay callback.
The entity is the owner of the component, which is what allows the callback to touch anything related to it
| void Game::World::DecayComponentValue | ( | Entity | entity, |
| ComponentId | component ) |
Put the value an entity's component holds into the component's decay buffer, the way removing it would, and leave the component on the entity.
Replacing the value of a live component and initializing it again leaves what the old value's init made, a graphics entity, with nothing pointing at it, and this is what lets its manager clean it up. Only components flagged to decay have a buffer, the decay callbacks are not run.
| void Game::World::Defragment | ( | MemDb::TableId | tableId | ) |
Defragment an entity table.
| void Game::World::DeleteEntity | ( | Game::Entity | entity | ) |
Delete entity.
|
private |
| void Game::World::EnumerateEntities | ( | EntityCreatedCallback const & | callback | ) |
Call callback for every entity in the world that has an instance in the database.
Freed indices and indices without an instance (deferred allocation) are skipped.
| void Game::World::ExecuteAddComponentCommands | ( | ) |
dispatches all staged components to be added to entities
| void Game::World::ExecuteRemoveComponentCommands | ( | ) |
dispatches all staged components to be removed from entities
| bool Game::World::ExportLevel | ( | Util::String const & | path | ) |
Export the world as a level.
| void Game::World::FinalizeAllocate | ( | Entity | entity | ) |
| byte * Game::World::FindComponentData | ( | Game::Entity | entity, |
| Game::ComponentId | component ) const |
The data of a component, read from the entity's table when it is there, otherwise from its chain of staged commands.
Returns nullptr when the entity has neither. Returned pointer is not safe between frames!
Returns nullptr when the entity has neither
Assumes the entity is valid.
|
private |
A component on the entity's chain of staged commands, or nullptr.
The component on the entity's chain of staged commands, or nullptr when the entity has no staged component with that id.
| void * Game::World::GetColumnData | ( | MemDb::TableId const | tableId, |
| uint16_t | partitionId, | ||
| MemDb::ColumnIndex const | column ) |
Get a pointer to the first instance of a column in a partition of an entity table. Use with caution!
| Game::Scale Game::World::GetComponent | ( | Entity | entity | ) |
| Game::Orientation Game::World::GetComponent | ( | Entity | entity | ) |
| Game::Position Game::World::GetComponent | ( | Entity | entity | ) |
| Game::Scale Game::World::GetComponent | ( | Entity | entity | ) |
| Game::Orientation Game::World::GetComponent | ( | Entity | entity | ) |
| Game::Position Game::World::GetComponent | ( | Entity | entity | ) |
|
inline |
Get an entitys component.
| Ptr< MemDb::Database > Game::World::GetDatabase | ( | ) |
Get the entity database. Be careful when directly modifying the database, as some information is only kept track of via the World.
| ComponentDecayBuffer const Game::World::GetDecayBuffer | ( | Game::ComponentId | component | ) |
Get a decay buffer for the given component.
| EntityMapping Game::World::GetEntityMapping | ( | Game::Entity | entity | ) | const |
Returns the entity mapping of an entity.
| FramePipeline & Game::World::GetFramePipeline | ( | ) |
Get the frame pipeline.
| Ptr< TransformHierarchy > Game::World::GetHierarchy | ( | ) | const |
The transform hierarchy of this world.
The world owns it, which is what lets the hierarchy read the component tables through it without knowing which world it was called from.
| MemDb::RowId Game::World::GetInstance | ( | Entity | entity | ) | const |
Get instance of entity.
| void * Game::World::GetInstanceBuffer | ( | MemDb::TableId const | tableId, |
| uint16_t | partitionId, | ||
| ComponentId const | component ) |
Get a pointer to the first instance of a component in a partition of an entity table. Use with caution!
| SizeT Game::World::GetNumEntities | ( | ) | const |
Number of entity ids currently allocated in this world.
|
private |
Get total number of instances in an entity table.
|
inline |
Returns the world hash for this world.
|
inline |
Returns the world ID for this world. This corresponds to Game::Entity::world.
|
inline |
Check if entity has a specific component.
| bool Game::World::HasComponent | ( | Entity | entity, |
| ComponentId | component ) const |
Check if entity has a specific component. (SLOW!).
TODO: This is not thread safe!
| bool Game::World::HasInstance | ( | Entity | entity | ) | const |
Check if an entity has an instance. It might be valid, but not have received an instance just after it has been created.
| void Game::World::InitializeInstance | ( | Entity | entity | ) |
Initialize all components on an allocated entity instance.
| void Game::World::InitializeInstance | ( | Entity | entity, |
| MemDb::TableId | tableId, | ||
| MemDb::RowId | row ) |
Initialize all components using an already fetched table and row.
| bool Game::World::IsValid | ( | Entity | entity | ) | const |
Check if an entity ID is still valid.
|
private |
| void Game::World::MarkAsModified | ( | Game::Entity | entity | ) |
Mark an entity as modified in its table.
This needs a row, so a change made to a component which is only staged cannot be recorded, and a processor built with OnlyModified() does not see it
|
private |
Migrate an entity from it's current table to a different table.
|
private |
Migrate an array of entities within the same table to a different table.
| newInstances | Will be filled with the new instance ids in the destination table. |
|
private |
Move a instance/row within a partition.
|
private |
|
private |
Copy src over dst without initializing components. This is extremely destructive.
|
private |
| PackedLevel * Game::World::PreloadLevel | ( | Util::String const & | path | ) |
preload a level that can be instantiated
Query the entity database using specified filter set. This does NOT wait for resources to be available.
| Dataset Game::World::Query | ( | Filter | filter, |
| Util::Array< MemDb::TableId > & | tids ) |
Query a subset of tables using a specified filter set.
Modifies the tables array so that it only contains valid tables. This does NOT wait for resources to be available.
| void Game::World::ReinitializeComponent | ( | Game::Entity | entity, |
| Game::ComponentId | component, | ||
| void * | value, | ||
| uint64_t | size ) |
Set the value of a component by providing a pointer and type size, then reinitialize the component.
|
inline |
Remove a component from an entity.
Only a component which is in the entity's table can be removed, because decay runs through the decay record of the table column, and a staged component has none
| void Game::World::RemoveComponent | ( | Entity | entity, |
| ComponentId | component ) |
Remove a component from an entity. Only a component which is in the entity's table.
|
private |
Removes all components in cmds from entity.
| void Game::World::RenderDebug | ( | ) |
|
private |
| void Game::World::SetComponent | ( | Entity | entity, |
| Game::HTransform const & | ) |
The local transform is the component which puts an entity in the transform hierarchy, which is why a write of it has to reach the instances below it.
| void Game::World::SetComponent | ( | Entity | entity, |
| Game::HTransform const & | value ) |
| void Game::World::SetComponent | ( | Entity | entity, |
| Game::Orientation const & | ) |
| void Game::World::SetComponent | ( | Entity | entity, |
| Game::Orientation const & | value ) |
| void Game::World::SetComponent | ( | Entity | entity, |
| Game::Position const & | ) |
| void Game::World::SetComponent | ( | Entity | entity, |
| Game::Position const & | value ) |
| void Game::World::SetComponent | ( | Entity | entity, |
| Game::Scale const & | ) |
| void Game::World::SetComponent | ( | Entity | entity, |
| Game::Scale const & | value ) |
|
inline |
Set the value of an entitys component.
| void Game::World::SetComponentValue | ( | Game::Entity | entity, |
| Game::ComponentId | component, | ||
| void * | value, | ||
| uint64_t | size ) |
Set the value of a component by providing a pointer and type size.
| void Game::World::SetEntityCreatedCallback | ( | EntityCreatedCallback const & | callback | ) |
Set the callback invoked for every entity instance allocated in this world.
Pass an invalid delegate to clear it.
|
private |
|
private |
Stage a component for an entity and put it on the entity's chain of staged commands.
A staged component is not in the entity's table, so the chain is what answers for it until the queue runs. It is what lets an Init read a component that was staged before it
|
private |
The first command on the entity's chain of staged commands, or InvalidIndex.
|
private |
| void Game::World::UnloadLevel | ( | PackedLevel * | level | ) |
unload a preloaded level
|
friend |
|
friend |
|
private |
Stores all deferred add staged component commands.
|
private |
Stores all deferred allocation commands.
|
private |
Set to true if the caches for the frame pipeline are valid.
|
private |
Contains all the component decay buffers. Lookup directly via ComponentId.
| bool Game::World::componentInitializationEnabled = true |
Disable if initialization of components is not required (ex. when running as editor db).
|
private |
Allocator for staged components.
|
private |
contains all entity instances
|
private |
Stores all deferred deallocation commands.
|
private |
The default table that empty entities are instantiated into.
|
private |
Callback invoked for every entity instance allocated in this world.
|
private |
maps entity index to table+row pair
|
private |
world hash
|
private |
The transform hierarchy of the entities in this world.
the mode this world is currently running in, which decides which processors execute
|
private |
Number of entities alive.
|
private |
The frame pipeline for this world.
|
private |
used to allocate entity ids for this world
|
private |
Stores all deferred remove component commands.
|
private |
the first staged command per entity index.
Read by HasComponent and GetComponent for a component that is staged and therefore not in the entity's table yet
|
private |
world id