Legacy Reference

Creating and Destroying Dynamic Entities with Pools

The processes for creating and destroying dynamic entities are similar to those for static entities, which one key exception: dynamic entities have no entity pool bookmarks (at least initially). Because they are not exported in the level, they have no static instanced data associated with them and so no bookmark is made for them.

Creating a Dynamic Entity

As with static entities, creating a dynamic entity with the pool starts with calling IEntitySystem::SpawnEntity. Construct an SEntitySpawnParams instance to describe its static instanced data. When filling in this struct, set the bCreatedThroughPool property to TRUE if you wish to have the entity be created through the pool. In the following example, a vehicle part from the Vehicle system is being spawned through the pool:

SEntitySpawnParams spawnParams; spawnParams.sName = pPartName spawnParams.pClass = gEnv->pEntitySystem->GetClassRegistry()->FindClass("VehiclePartDetached"); spawnParams.nFlags = ENTITY_FLAG_CLIENT_ONLY; spawnParams.bCreatedThroughPool = true; IEntity* pSpawnedDebris = gEnv->pEntitySystem->SpawnEntity(spawnParams);

Once SpawnEntity, the following execution flow takes place:

  1. CEntitySystem::SpawnEntity will check for an entity pool associated with the provided entity class. If so, it will delegate the workload to the entity pool manager.

  2. From within CEntityPoolManager::PrepareDynamicFromPool, an entity pool bookmark is created for the new entity. This is done primarily for serialization purposes.

  3. The execution flow follows the same sequence as preparing a static entity (see Creating and Destroying Static Entities with Pools.

  4. If the process is successful, the entity container now housing the information is returned. Otherwise, SpawnEntity creates a new CEntity instance to satisfy the request.

At this point, calling CEntity::GetEntity or CEntity::FindEntityByName will return the entity container now housing the dynamic entity and its information.

Destroying a Dynamic Entity with the Pool

As with static entities, use IEntitySystem::RemoveEntity or any other method that can destroy an entity. The entity pool manager will return the entity container to the pool, freeing it for use elsewhere and removing the dynamic entity in the process. The resulting execution flow differ from destroying static entities in two ways:

  • Dynamic entities are not serialized when they are returned.

  • The entity pool bookmark associated with the dynamic entity is removed. It is no longer needed.

At this point, calling CEntity::GetEntity or CEntity::FindEntityByName will return NULL.