class GameObjectCollection

Inherits: std::enable_shared_from_this<GameObjectCollection>

Collection of all game objects associated with a particular scene or prefab.

Provides functionality to patch game object handles as may be needed when deserializing, updating from prefab, applying deltas, and similar.

Also maintains an ECS registry that holds all the entities and components created by its game objects.

Public

Constructors

GameObjectCollection

GameObjectCollection(PrivatelyConstruct)

Methods

~GameObjectCollection

~GameObjectCollection()

GetId

UUID GetId() const

Unique ID for the collection.

RegisterNewObject

GameObjectHandle RegisterNewObject(const SPtr<GameObject> &object)

Registers a newly created game object and returns the handle to the object.

object
Constructed GameObject to wrap in the handle and initialize.

Returns: Handle to the GameObject.

RegisterExistingObject

void RegisterExistingObject(const GameObjectHandle &object)

Registers a game object that already has a handle.

UnregisterObject

void UnregisterObject(GameObjectHandle &object, bool triggerDestroyEvent)

Unregisters the game object.

Handles to this object will no longer be valid after this call.

object
Object handle to unregister.
triggerDestroyEvent
If true, will trigger OnDestroyed event. Should be true if the object was instantiated and is being destroyed.

GetObject

GameObjectHandle GetObject(const UUID &id) const

Attempts to find a game object based on the provided ID.

Returns empty handle if ID cannot be found.

TryGetObject

bool TryGetObject(const UUID &id, GameObjectHandle &outObject) const

Attempts to find a game object handle based on the provided ID.

Returns true if object with the specified ID is found, false otherwise.

ObjectExists

bool ObjectExists(const UUID &id) const

Checks if the GameObject with the specified ID exists.

GetObjectCount

u32 GetObjectCount() const

Returns the number of game objects registered with this collection.

ReplaceGameObjectInstance

void ReplaceGameObjectInstance(GameObjectHandle &newObjectHandle, const SPtr<GameObjectInstanceData> &originalObjectInstanceData)

Call this method after you clone/copy/deserialize a new instance of an existing game object.

It will ensure that any existing handles are updated so they point to the newly created object. The old object is expected to be discarded, as they cannot both exist at once.

This works because any old handles will point to the original GameObjectInstanceData, which we just patched to point to the new object.

And any new handles will be patched when we update itself, as our system guarantees that all handles created during a single clone/deserialization operation share the same handle data, so it's enough to just patch this handle.

newObjectHandle
Newly created handle pointing to the newly created object.
originalObjectInstanceData
Game object instance data of the game object we're replacing.

ChangeGameObjectId

void ChangeGameObjectId(GameObjectHandle &gameObject, const UUID &newId)

Changes the instance ID of the provided game object.

BeginHandleResolve

void BeginHandleResolve()

Notifies the collection that we are about to register game objects whose handle might not be immediately valid.

This is primarily used during deserialization, as game objects and handles will be registered with the collection during deserialization as they are deserialized, but may be resolved only after deserialization for the entire collection is complete.

Must be followed by EndHandleResolve(), which will resolve any unresolved handles. When handle resolve is active you are allowed to call the following methods:

EndHandleResolve

void EndHandleResolve()

Ends the handle resolve process started in BeginHandleResolve().

Resolves any unresolved handles by mapping them to the correct game object instance data and correct id.

RegisterUnresolvedHandle

void RegisterUnresolvedHandle(GameObjectHandle &handle)

Registers a handle to be resolved when EndHandleResolve() is called.

Unresolved handles are generated during deserialization, as the objects they are pointing to might not be deserialized yet.

RegisterUnresolvedHandleIdRemapping

void RegisterUnresolvedHandleIdRemapping(const UUID &originalId, const UUID &newId)

Notifies the system that id of the object an resolved handle is pointing to has changed.

Useful if you are generating new IDs during deserialization.

originalId
Current ID, as registered by RegisterUnresolvedHandle.
newId
New wanted ID of the object.

QueueForDestroy

void QueueForDestroy(const GameObjectHandle &object)

Queues the object to be destroyed at the end of a GameObject update cycle.

DestroyQueuedObjects

void DestroyQueuedObjects()

Destroys any GameObjects that were queued for destruction.

GetECSRegistry

ecs::Registry &GetECSRegistry()

Returns the ECS registry associated with this collection.

GetECSRegistry

const ecs::Registry &GetECSRegistry() const

staticCreate

static SPtr<GameObjectCollection> Create()

Creates a new empty game object collection.

Fields

OnDestroyed

Event<void (const HGameObject &)> OnDestroyed

Triggered when a game object is being destroyed.

Private

Fields

mId

UUID mId

mHandleResolveActive

bool mHandleResolveActive

mUUIDRemapping

UnorderedMap<UUID, UUID> mUUIDRemapping

mObjects

UnorderedMap<UUID, GameObjectHandle> mObjects

mQueuedForDestroy

UnorderedMap<UUID, GameObjectHandle> mQueuedForDestroy

mOrderedQueuedForDestroy

Vector<UUID> mOrderedQueuedForDestroy

mUnresolvedHandleSharedHandleData

UnorderedMap<UUID, SPtr<GameObjectHandleData>> mUnresolvedHandleSharedHandleData

mUnresolvedHandles

Vector<GameObjectHandle> mUnresolvedHandles

mECSRegistry

ecs::Registry mECSRegistry