Nebula
Loading...
Searching...
No Matches
Game::World Class Reference

#include <world.h>

Detailed Description

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  AddStagedComponentCommand
 
struct  AllocateInstanceCommand
 
struct  DeallocInstanceCommand
 
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.
 
Entity CreateEntity (bool immediate=true)
 Create a new empty entity.
 
Entity CreateEntity (EntityCreateInfo const &info)
 Create a new entity from create info.
 
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.
 
template<typename TYPE >
TYPE * AddComponent (Entity entity)
 Create a component. This queues the component in a command buffer to be added later.
 
void * AddComponent (Entity entity, ComponentId component)
 Queues a component to be added to the entity in a command buffer.
 
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!)
 
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.
 
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.
 
PackedLevelPreloadLevel (Util::String const &path)
 preload a level that can be instantiated
 
void UnloadLevel (PackedLevel *level)
 unload a preloaded level
 
void ExportLevel (Util::String const &path)
 Export the world as a level.
 
FramePipelineGetFramePipeline ()
 Get the frame pipeline.
 
Ptr< MemDb::DatabaseGetDatabase ()
 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, MemDb::TableId table, Util::Blob const *const data=nullptr)
 Allocate an entity instance in a table. Use this with caution!
 
MemDb::RowId AllocateInstance (Entity entity, BlueprintId blueprint)
 Allocate an entity instance from a blueprint. Use this with caution!
 
MemDb::RowId AllocateInstance (Entity entity, TemplateId templateId, bool performInitialize)
 Allocate an entity instance from a template. Use this with caution!
 
void FinalizeAllocate (Entity entity)
 
void DeallocateInstance (MemDb::TableId table, MemDb::RowId instance)
 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 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<>
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 &)
 

Static Public Member Functions

static void Override (World *src, World *dst)
 copies and overrides dst with src. This is extremely destructive - make sure you understand the implications!
 

Public Attributes

bool componentInitializationEnabled = true
 Disable if initialization of components is not required (ex. when running as editor db)
 

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.
 
void ExecuteRemoveComponentCommands ()
 
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)
 Copies the component to the decay table.
 
void MoveInstance (MemDb::Table::Partition *partition, MemDb::RowId from, MemDb::RowId to)
 Move a instance/row within a partition.
 
void InitializeAllComponents (Entity entity, MemDb::TableId tableId, MemDb::RowId row)
 Run OnInit on all components. Use with caution, since they can only be initialized once and the function doesn't check for this.
 
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.
 

Private Attributes

EntityPool pool
 used to allocate entity ids for this world
 
SizeT numEntities
 Number of entities alive.
 
Util::Array< EntityMappingentityMap
 maps entity index to table+row pair
 
Ptr< MemDb::Databasedb
 contains all entity instances
 
WorldHash hash
 world hash
 
WorldId worldId
 world id
 
Util::HashTable< BlueprintId, MemDb::TableIdblueprintToTableMap
 maps from blueprint to a table that has the same signature
 
Util::Queue< AllocateInstanceCommandallocQueue
 Stores all deferred allocation commands.
 
Util::Queue< DeallocInstanceCommanddeallocQueue
 Stores all deferred deallocation commands.
 
Util::Array< AddStagedComponentCommandaddStagedQueue
 Stores all deferred add staged component commands.
 
Util::Array< RemoveComponentCommandremoveComponentQueue
 Stores all deferred remove component commands.
 
Memory::ArenaAllocator< 4096_KB > componentStageAllocator
 Allocator for staged components.
 
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< ComponentDecayBuffercomponentDecayTable
 Contains all the component decay buffers. Lookup directly via ComponentId.
 

Friends

class GameServer
 
class BlueprintManager
 
class PackedLevel
 

Constructor & Destructor Documentation

◆ World()

Game::World::World ( WorldHash hash,
WorldId id )

◆ ~World()

Game::World::~World ( )

Member Function Documentation

◆ AddComponent() [1/2]

template<typename TYPE >
TYPE * Game::World::AddComponent ( Entity entity)
inline

Create a component. This queues the component in a command buffer to be added later.

◆ AddComponent() [2/2]

void * Game::World::AddComponent ( Entity entity,
Game::ComponentId id )

Queues a component to be added to the entity in a command buffer.

◆ AddStagedComponentsToEntity()

void Game::World::AddStagedComponentsToEntity ( Entity entity,
AddStagedComponentCommand * cmds,
SizeT numCmds )
private

Adds all components in cmds to entity.

◆ AllocateEntityId()

Entity Game::World::AllocateEntityId ( )

Allocate an entity id. Use this with caution!

◆ AllocateInstance() [1/3]

MemDb::RowId Game::World::AllocateInstance ( Entity entity,
BlueprintId blueprint )

Allocate an entity instance from a blueprint. Use this with caution!

◆ AllocateInstance() [2/3]

MemDb::RowId Game::World::AllocateInstance ( Entity entity,
MemDb::TableId table,
Util::Blob const *const data = nullptr )

Allocate an entity instance in a table. Use this with caution!

◆ AllocateInstance() [3/3]

MemDb::RowId Game::World::AllocateInstance ( Entity entity,
TemplateId templateId,
bool performInitialize )

Allocate an entity instance from a template. Use this with caution!

◆ BeginFrame()

void Game::World::BeginFrame ( )
private

◆ ClearDecayBuffers()

void Game::World::ClearDecayBuffers ( )
private

Clears all decay buffers. This is called by the game server automatically.

◆ CreateEntity() [1/2]

Game::Entity Game::World::CreateEntity ( bool immediate = true)

Create a new empty entity.

◆ CreateEntity() [2/2]

Game::Entity Game::World::CreateEntity ( EntityCreateInfo const & info)

Create a new entity from create info.

◆ CreateEntityTable()

MemDb::TableId Game::World::CreateEntityTable ( EntityTableCreateInfo const & info)

Create a table in the entity database that has a specific set of components.

◆ DeallocateEntityId()

void Game::World::DeallocateEntityId ( Entity entity)

Deallocate an entity id. Use this with caution!

◆ DeallocateInstance() [1/2]

void Game::World::DeallocateInstance ( Entity entity)

Deallocate an entity instance. Use this with caution!

◆ DeallocateInstance() [2/2]

void Game::World::DeallocateInstance ( MemDb::TableId table,
MemDb::RowId instance )

Deallocate an entity instance. Use this with caution!

◆ DecayComponent()

void Game::World::DecayComponent ( Game::ComponentId component,
MemDb::TableId tableId,
MemDb::ColumnIndex column,
MemDb::RowId instance )
private

Copies the component to the decay table.

◆ Defragment()

void Game::World::Defragment ( MemDb::TableId tableId)

Defragment an entity table.

◆ DeleteEntity()

void Game::World::DeleteEntity ( Game::Entity entity)

Delete entity.

◆ EndFrame()

void Game::World::EndFrame ( )
private

◆ ExecuteAddComponentCommands()

void Game::World::ExecuteAddComponentCommands ( )

dispatches all staged components to be added to entities

◆ ExecuteRemoveComponentCommands()

void Game::World::ExecuteRemoveComponentCommands ( )
private

◆ ExportLevel()

void Game::World::ExportLevel ( Util::String const & path)

Export the world as a level.

◆ FinalizeAllocate()

void Game::World::FinalizeAllocate ( Entity entity)

◆ GetColumnData()

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!

◆ GetComponent() [1/7]

template<>
Game::Position Game::World::GetComponent ( Entity entity)

◆ GetComponent() [2/7]

template<>
Game::Orientation Game::World::GetComponent ( Entity entity)

◆ GetComponent() [3/7]

template<>
Game::Scale Game::World::GetComponent ( Entity entity)

◆ GetComponent() [4/7]

template<typename TYPE >
TYPE Game::World::GetComponent ( Entity entity)
inline

Get an entitys component.

◆ GetComponent() [5/7]

template<>
Game::Position Game::World::GetComponent ( Entity entity)

◆ GetComponent() [6/7]

template<>
Game::Orientation Game::World::GetComponent ( Entity entity)

◆ GetComponent() [7/7]

template<>
Game::Scale Game::World::GetComponent ( Entity entity)

◆ GetDatabase()

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.

◆ GetDecayBuffer()

ComponentDecayBuffer const Game::World::GetDecayBuffer ( Game::ComponentId component)

Get a decay buffer for the given component.

◆ GetEntityMapping()

EntityMapping Game::World::GetEntityMapping ( Game::Entity entity) const

Returns the entity mapping of an entity.

◆ GetFramePipeline()

FramePipeline & Game::World::GetFramePipeline ( )

Get the frame pipeline.

◆ GetInstance()

MemDb::RowId Game::World::GetInstance ( Entity entity) const

Get instance of entity.

◆ GetInstanceBuffer()

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!

◆ GetNumInstances()

SizeT Game::World::GetNumInstances ( MemDb::TableId tid)
private

Get total number of instances in an entity table.

◆ GetWorldHash()

WorldHash Game::World::GetWorldHash ( ) const
inline

Returns the world hash for this world.

◆ GetWorldId()

WorldId Game::World::GetWorldId ( ) const
inline

Returns the world ID for this world. This corresponds to Game::Entity::world.

◆ HasComponent() [1/2]

template<typename TYPE >
bool Game::World::HasComponent ( Game::Entity entity) const
inline

Check if entity has a specific component.

◆ HasComponent() [2/2]

bool Game::World::HasComponent ( Entity entity,
ComponentId component ) const

Check if entity has a specific component. (SLOW!)

TODO: This is not thread safe!

◆ HasInstance()

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.

◆ InitializeAllComponents()

void Game::World::InitializeAllComponents ( Entity entity,
MemDb::TableId tableId,
MemDb::RowId row )
private

Run OnInit on all components. Use with caution, since they can only be initialized once and the function doesn't check for this.

◆ IsValid()

bool Game::World::IsValid ( Entity entity) const

Check if an entity ID is still valid.

◆ ManageEntities()

void Game::World::ManageEntities ( )
private

◆ MarkAsModified()

void Game::World::MarkAsModified ( Game::Entity entity)

Mark an entity as modified in its table.

◆ Migrate() [1/2]

MemDb::RowId Game::World::Migrate ( Entity entity,
MemDb::TableId newTable )
private

Migrate an entity from it's current table to a different table.

◆ Migrate() [2/2]

void Game::World::Migrate ( Util::Array< Entity > const & entities,
MemDb::TableId fromTableId,
MemDb::TableId newTableId,
Util::FixedArray< MemDb::RowId > & newInstances )
private

Migrate an array of entities within the same table to a different table.

Parameters
newInstancesWill be filled with the new instance ids in the destination table.
Note
This assumes ALL entities in the entity array is of same table!

◆ MoveInstance()

void Game::World::MoveInstance ( MemDb::Table::Partition * partition,
MemDb::RowId from,
MemDb::RowId to )
private

Move a instance/row within a partition.

◆ OnLoad()

void Game::World::OnLoad ( )
private

◆ OnSave()

void Game::World::OnSave ( )
private

◆ Override()

void Game::World::Override ( World * src,
World * dst )
static

copies and overrides dst with src. This is extremely destructive - make sure you understand the implications!

◆ PrefilterProcessors()

void Game::World::PrefilterProcessors ( )
private

◆ PreloadLevel()

PackedLevel * Game::World::PreloadLevel ( Util::String const & path)

preload a level that can be instantiated

◆ Query() [1/2]

Dataset Game::World::Query ( Filter filter)

Query the entity database using specified filter set. This does NOT wait for resources to be available.

Returns
Dataset with category table views.
Note
The category table view buffer can be NULL if the filter contains a non-typed/flag component.

◆ Query() [2/2]

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.

◆ ReinitializeComponent()

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.

◆ RemoveComponent() [1/2]

template<typename TYPE >
void Game::World::RemoveComponent ( Entity entity)
inline

Remove a component from an entity.

◆ RemoveComponent() [2/2]

void Game::World::RemoveComponent ( Entity entity,
ComponentId component )

Remove a component from an entity.

◆ RemoveComponentsFromEntity()

void Game::World::RemoveComponentsFromEntity ( Entity entity,
RemoveComponentCommand * cmds,
SizeT numCmds )
private

Removes all components in cmds from entity.

◆ RenderDebug()

void Game::World::RenderDebug ( )

◆ Reset()

void Game::World::Reset ( )
private

◆ SetComponent() [1/7]

template<>
void Game::World::SetComponent ( Entity entity,
Game::Orientation const &  )

◆ SetComponent() [2/7]

template<>
void Game::World::SetComponent ( Entity entity,
Game::Orientation const & value )

◆ SetComponent() [3/7]

template<>
void Game::World::SetComponent ( Entity entity,
Game::Position const &  )

◆ SetComponent() [4/7]

template<>
void Game::World::SetComponent ( Entity entity,
Game::Position const & value )

◆ SetComponent() [5/7]

template<>
void Game::World::SetComponent ( Entity entity,
Game::Scale const &  )

◆ SetComponent() [6/7]

template<>
void Game::World::SetComponent ( Entity entity,
Game::Scale const & value )

◆ SetComponent() [7/7]

template<typename TYPE >
void Game::World::SetComponent ( Entity entity,
TYPE const & value )
inline

Set the value of an entitys component.

◆ SetComponentValue()

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.

◆ SimFrame()

void Game::World::SimFrame ( )
private

◆ Start()

void Game::World::Start ( )
private

◆ UnloadLevel()

void Game::World::UnloadLevel ( PackedLevel * level)

unload a preloaded level

Friends And Related Symbol Documentation

◆ BlueprintManager

friend class BlueprintManager
friend

◆ GameServer

friend class GameServer
friend

◆ PackedLevel

friend class PackedLevel
friend

Member Data Documentation

◆ addStagedQueue

Util::Array<AddStagedComponentCommand> Game::World::addStagedQueue
private

Stores all deferred add staged component commands.

◆ allocQueue

Util::Queue<AllocateInstanceCommand> Game::World::allocQueue
private

Stores all deferred allocation commands.

◆ blueprintToTableMap

Util::HashTable<BlueprintId, MemDb::TableId> Game::World::blueprintToTableMap
private

maps from blueprint to a table that has the same signature

◆ cacheValid

bool Game::World::cacheValid = false
private

Set to true if the caches for the frame pipeline are valid.

◆ componentDecayTable

Util::FixedArray<ComponentDecayBuffer> Game::World::componentDecayTable
private

Contains all the component decay buffers. Lookup directly via ComponentId.

◆ componentInitializationEnabled

bool Game::World::componentInitializationEnabled = true

Disable if initialization of components is not required (ex. when running as editor db)

◆ componentStageAllocator

Memory::ArenaAllocator<4096_KB> Game::World::componentStageAllocator
private

Allocator for staged components.

◆ db

Ptr<MemDb::Database> Game::World::db
private

contains all entity instances

◆ deallocQueue

Util::Queue<DeallocInstanceCommand> Game::World::deallocQueue
private

Stores all deferred deallocation commands.

◆ defaultTableId

MemDb::TableId Game::World::defaultTableId
private

The default table that empty entities are instantiated into.

◆ entityMap

Util::Array<EntityMapping> Game::World::entityMap
private

maps entity index to table+row pair

◆ hash

WorldHash Game::World::hash
private

world hash

◆ numEntities

SizeT Game::World::numEntities
private

Number of entities alive.

◆ pipeline

FramePipeline Game::World::pipeline
private

The frame pipeline for this world.

◆ pool

EntityPool Game::World::pool
private

used to allocate entity ids for this world

◆ removeComponentQueue

Util::Array<RemoveComponentCommand> Game::World::removeComponentQueue
private

Stores all deferred remove component commands.

◆ worldId

WorldId Game::World::worldId
private

world id


The documentation for this class was generated from the following files: