Game Objects

Overview

Game Objects are effectively an extension to the standard IEntity implementation, allowing more complex logic and custom user code contained in modules other than CryEntitySystem and CryAction to be applied to entities.

The primary use cases for game objects are:

  • Binding an entity to the network, done by calling IGameObject::BindToNetwork.
Network bound entities can trigger Remote Method Invocations, or serialize bits of data to a remote version of the same entity.
  • Creating extensions for more complex entities, noteworthy examples being actors (IActor) and the animated character helper (IAnimatedCharacter).

Keep in mind that while every Game Object must have a parent Entity, every entity does not necessarily need to have a Game Object assigned to it.

Implementation Details

The CGameObject (known as IGameObject to projects other than CryAction) class is implemented as an Entity Proxy, residing in the ENTITY_PROXY_USER slot. This means that a game object pointer can be retrieved by calling IEntity:GetProxy(ENTITY_PROXY_USER) and casting to IGameObject. Note that this logic is wrapped in IGameFramework::GetGameObject, allowing easy access to an entity's game object assuming that its entity id is known.

Methods of creating a Game Object

Spawning a New Entity with a Game Object

The game object system supports registering new game object extensions with the intent of automatically creating a game object with a pre-activated instance of the game object extension in question. This is done by calling IGameObjectSystem::RegisterExtension with the pClsDesc parameter set to a description of the entity class you want to create.

The default SDK sample supports this by default in the REGISTER_GAME_OBJECT and REGISTER_GAME_OBJECT_EX preprocessor helpers inside Game/GameDll/GameFactory.h.

Creating a New Game Object Instance for an Already Spawned Entity

In some cases it might not be desired behavior to have every instance of a specific entity class automatically create a game object (for example if spawning an entity with the standard IEntityClass implementation). In this case we can create one per entity:

IEntity *pMyEntity = //..
 
IGameObjectSystem *pGameObjectSystem = gEnv->pGame->GetIGameFramework()->GetIGameObjectSystem();
IGameObject *pGameObject = pGameObjectSystem->CreateGameObjectForEntity(pMyEntity->GetId());

The above code provides a newly created game object that is permanently attached to the entity, and can also now be retrieved via IGameFramework::GetGameObject.

Game Object Extensions

Game Object extensions are defined by inheriting from the IGameObjectExtension interface, and denote the extension of a game object (and indirectly the entity it is linked to) with custom (usually game-specific) code.