IEntityComponent

Represents a component that can be attached to an individual entity instance Allows for populating entities with custom game logic. More...

#include

Inherits ICryUnknown, and ISimpleEntityEventListener.

Inherited by IClipVolumeComponent, IEntityAreaComponent, IEntityAttributesComponent, IEntityAudioComponent, IEntityBehaviorTreeComponent, IEntityCameraComponent, IEntityCoverUserComponent, IEntityDynamicResponseComponent, IEntityFactionComponent, IEntityFlowGraphComponent, IEntityListenerComponent, IEntityNavigationComponent, IEntityObservableComponent, IEntityObserverComponent, IEntityRopeComponent, IEntityScriptComponent, IEntitySubstitutionComponent, IEntityTriggerComponent, IGeometryEntityComponent, and IParticleEntityComponent.

Classes

struct SInitParams
SInitParams is only used from Schematyc to call PreInit to initialize Schematyc Entity Component. More...

Public Types

typedef int ComponentEventPriority
typedef EEntityComponentFlags EFlags
typedef EntityComponentFlags ComponentFlags

Public Member Functions

virtual ICryFactory * GetFactory () const
ILINE IEntity * GetEntity () const
ILINE EntityId GetEntityId () const
IEntityComponent (IEntityComponent &&other)
IEntityComponent (const IEntityComponent &)=default
IEntityComponent & operator= (const IEntityComponent &)=default
IEntityComponent & operator= (IEntityComponent &&)=default
const CEntityComponentClassDesc & GetClassDesc () const
virtual uint64 GetEventMask () const
virtual ComponentEventPriority GetEventPriority () const
Determines the order in which this component will receive entity events (including update). Lower number indicates a higher priority.
virtual NetworkAspectType GetNetSerializeAspectMask () const
Network serialization. Override to provide a mask of active network aspects used by this component. Called once during binding to network. More...
virtual bool NetSerialize (TSerialize ser, EEntityAspects aspect, uint8 profile, int flags)
Network serialization. Will be called for each active aspect for both reading and writing. More...
virtual void NetReplicateSerialize (TSerialize ser)
virtual void NetMarkAspectsDirty (const NetworkAspectType aspects)
Call this to trigger aspect synchronization over the network. A shortcut. More...
virtual IEntityComponentPreviewer * GetPreviewer ()
Override this to return preview render interface for the component. Multiple component instances can usually share the same previewer class instance. More...
void SetComponentFlags (ComponentFlags flags)
END IEntityComponent virtual interface. More...
const ComponentFlags & GetComponentFlags () const
Return flags for this component.
ComponentFlags & GetComponentFlags ()
const CryGUID & GetGUID () const
IEntityComponent * GetParent () const
const CryTransform::CTransformPtr & GetTransform () const
Return Transformation of the entity component relative to the owning entity or parent component.
void SetTransformMatrix (const Matrix34 &transform)
Sets the transformation form a matrix. If the component doesn't have a transformation yet the function will add one.
void SetTransformMatrix (const CryTransform::CTransformPtr &transform)
Sets the transformation from another transformation. If the component doesn't have a transformation yet the function will add one.
Matrix34 GetWorldTransformMatrix () const
Return Transformation of the entity component relative to the world.
Matrix34 GetTransformMatrix () const
Return Calculated Transformation Matrix for current component transform.
const char * GetName () const
Get name of this individual component, usually only Schematyc components will have names.
void SetName (const char *szName)
int GetEntitySlotId () const
Return optional EntitySlot id used by this Component.
int GetOrMakeEntitySlotId ()
void SetEntitySlotId (int slotId)
Stores Entity slot id used by this component.
void FreeEntitySlot ()
Frees entity slot used by this component.
EEntitySimulationMode GetEntitySimulationMode () const
Return Current simulation mode of the host Entity.
void SendEvent (const SEntityEvent &event)
virtual void GetMemoryUsage (ICrySizer *pSizer) const
virtual void GameSerialize (TSerialize ser)
virtual bool NeedGameSerialize ()
virtual void LegacySerializeXML (XmlNodeRef &entityNode, XmlNodeRef &componentNode, bool bLoading)
virtual struct IEntityPropertyGroup * GetPropertyGroup ()
Only for backward compatibility to Release 5.3.0 for loading.
virtual EEntityProxy GetProxyType () const
Legacy, used for old entity proxies.

Static Public Attributes

static constexpr int EmptySlotId = -1

Protected Member Functions

virtual void * QueryInterface (const CryInterfaceID &iid) const
virtual void * QueryComposite (const char *name) const
virtual void PreInit (const SInitParams &params)
virtual void Initialize ()
Called at the very first initialization of the component, at component creation time.
virtual void OnShutDown ()
Called on all Entity components right before all of the Entity Components are destructed.
virtual void OnTransformChanged ()
Called when the transformation of the component is changed.
virtual void ProcessEvent (const SEntityEvent &event)

Protected Attributes

friend IEntity
IEntity * m_pEntity = nullptr
ComponentFlags m_componentFlags
CryGUID m_guid
Unique GUID of the instance of this component.
string m_name
name of this component
CryTransform::CTransformPtr m_pTransform
Optional transformation setting for the component within the Entity object.
IEntityComponent * m_pParent = nullptr
Optional pointer to our parent component.
const CEntityComponentClassDesc * m_pClassDesc = nullptr
int m_entitySlotId = EmptySlotId
Optional Entity SlotId for storing component data like geometry of character.

Friends

class CEntity
class CEntitySystem
class CGameObject

Detailed Description

Represents a component that can be attached to an individual entity instance Allows for populating entities with custom game logic.

#include 

class CMinimalComponent final : public IEntityComponent
{
public:
    // Provide a virtual destructor, ensuring correct destruction of IEntityComponent members
    virtual ~CMinimalComponent() = default;
    
    // Reflect type to set a unique identifier for this component
    // and provide additional information to expose it in the sandbox
    static void ReflectType(Schematyc::CTypeDesc& desc)
    {
        // Provide a globally unique identifier for the component, can be generated in Visual Studio via Tools -> Create GUID (in registry format).
        desc.SetGUID("{35EC6526-3D24-4376-9509-806908FF4698}"_cry_guid);
        desc.SetEditorCategory("MyComponents");
        desc.SetLabel("MyComponent");
    }
};

Member Function Documentation

◆ GameSerialize()

virtual void IEntityComponent::GameSerialize ( TSerialize ser)
inline
virtual

SaveGame serialization. Override to specify what to serialize in a saved game.

Parameters
serSerializing stream. Use IsReading() to decide read/write phase. Use Value() to read/write a property.

◆ GetClassDesc()

const CEntityComponentClassDesc& IEntityComponent::GetClassDesc ( ) const
inline

Return ClassDesc for this component Class description is storing runtime C++ class reflection information It contain information about member variables of the component and how to serialize them, information how to create an instance of class and all relevant additional information to handle this class.

◆ GetEventMask()

virtual uint64 IEntityComponent::GetEventMask ( ) const
inline
virtual

Return bit mask of the EEntityEvent flags that we want to receive in ProcessEvent (ex: BIT64(ENTITY_EVENT_HIDE) | BIT64(ENTITY_EVENT_UNHIDE)) Only events matching the returned bit mask will be sent to the ProcessEvent method

#include 

class CMyEventComponent final : public IEntityComponent
{
public:
    // Provide a virtual destructor, ensuring correct destruction of IEntityComponent members
    virtual ~CMyEventComponent() = default;

    static void ReflectType(Schematyc::CTypeDesc& desc) { /* Reflect the component GUID in here. */}

    // Override the ProcessEvent function to receive the callback whenever an event specified in GetEventMask is triggered
    virtual void ProcessEvent(const SEntityEvent& event) override
    {
        // Check if this is the update event
        if (event.event == ENTITY_EVENT_UPDATE)
        {
            // The Update event provides delta time as the first floating-point parameter
            const float frameTime = event.fParam[0];

            /* Handle update logic here */
        }
    }

    // As an optimization, components have to specify a bitmask of the events that they want to handle
    // This is called once at component creation, and then only if IEntity::UpdateComponentEventMask is called, to support dynamic change of desired events (useful for disabling update when you don't need it)
    virtual uint64 GetEventMask() const override { return BIT64(ENTITY_EVENT_UPDATE); }
};

◆ GetGUID()

const CryGUID& IEntityComponent::GetGUID ( ) const
inline

Return GUID of this component. This GUID is only guaranteed to be unique within the host entity, different entities can have components with equal GUIDs. Each component in the entity have type guid (GetClassDesc().GetGUID()) (ex: identify EntityLightComponent class) and a unique instance guid IEntityComponent::GetGUID() (ex: identify Light01,Light02,etc.. component)

◆ GetNetSerializeAspectMask()

virtual NetworkAspectType IEntityComponent::GetNetSerializeAspectMask ( ) const
inline
virtual

Network serialization. Override to provide a mask of active network aspects used by this component. Called once during binding to network.

#include 

class CMyNetSerializedComponent final : public IEntityComponent
{
public:
    // Provide a virtual destructor, ensuring correct destruction of IEntityComponent members
    virtual ~CMyNetSerializedComponent() = default;

    static void ReflectType(Schematyc::CTypeDesc& desc) { /* Reflect the component GUID in here. */}

    // Define the aspect / stream to use for this aspect, in our case we want a server stream since only the server can change health
    static constexpr EEntityAspects HealthAspect = eEA_GameServerA;
    // Override GetNetSerializeAspectMask to specify that we will handle HealthAspect
    virtual NetworkAspectType GetNetSerializeAspectMask() const override { return HealthAspect; }

    // Override NetSerialize, called on the server when the aspect has been marked dirty, and then on the client to read the data
    virtual bool NetSerialize(TSerialize ser, EEntityAspects aspect, uint8 profile, int flags) override
    {
        // Check that we are processing the desired aspect
        if (aspect == HealthAspect)
        {
            // Serialize health, either to network (on the server) or from network (on a client)
            ser.Value("health", m_health);
        }
    }

    // Helper utility that game code should call to set the health of this component
    void SetHealth(float newHealth)
    {
        CRY_ASSERT_MESSAGE(gEnv->bServer, "Health can only be modified on the server!");
        // Update the local state
        m_health = newHealth;
        // Notify the network that the health aspect has changed
        // This will result in NetSerialize being called to serialize data over the network
        NetMarkAspectsDirty(HealthAspect);
    }

protected:
    float m_health = 100.f;
};

◆ GetOrMakeEntitySlotId()

ILINE int IEntityComponent::GetOrMakeEntitySlotId ( )

Return optional EntitySlot id used by this Component If slot id is not allocated, new slotid will be allocated and returned

◆ GetParent()

IEntityComponent* IEntityComponent::GetParent ( ) const
inline

Return Parent component, only used by Schematyc components Initialized by the PreInit call

◆ GetPreviewer()

virtual IEntityComponentPreviewer* IEntityComponent::GetPreviewer ( )
inline
virtual

Override this to return preview render interface for the component. Multiple component instances can usually share the same previewer class instance.

See also
IEntityComponentPreviewer

◆ LegacySerializeXML()

virtual void IEntityComponent::LegacySerializeXML ( XmlNodeRef & entityNode,
XmlNodeRef & componentNode,
bool bLoading
)
inline
virtual

Optionally serialize component to/from XML. For user-facing properties, see GetProperties.

◆ NeedGameSerialize()

virtual bool IEntityComponent::NeedGameSerialize ( )
inline
virtual

SaveGame serialization. Override to enable serialization for the component.

Returns
true If component needs to be serialized to/from a saved game.

◆ NetMarkAspectsDirty()

ILINE void IEntityComponent::NetMarkAspectsDirty ( const NetworkAspectType aspects)
virtual

Call this to trigger aspect synchronization over the network. A shortcut.

See also
INetEntity::MarkAspectsDirty()
#include 

class CMyNetSerializedComponent final : public IEntityComponent
{
public:
    // Provide a virtual destructor, ensuring correct destruction of IEntityComponent members
    virtual ~CMyNetSerializedComponent() = default;

    static void ReflectType(Schematyc::CTypeDesc& desc) { /* Reflect the component GUID in here. */}

    // Define the aspect / stream to use for this aspect, in our case we want a server stream since only the server can change health
    static constexpr EEntityAspects HealthAspect = eEA_GameServerA;
    // Override GetNetSerializeAspectMask to specify that we will handle HealthAspect
    virtual NetworkAspectType GetNetSerializeAspectMask() const override { return HealthAspect; }

    // Override NetSerialize, called on the server when the aspect has been marked dirty, and then on the client to read the data
    virtual bool NetSerialize(TSerialize ser, EEntityAspects aspect, uint8 profile, int flags) override
    {
        // Check that we are processing the desired aspect
        if (aspect == HealthAspect)
        {
            // Serialize health, either to network (on the server) or from network (on a client)
            ser.Value("health", m_health);
        }
    }

    // Helper utility that game code should call to set the health of this component
    void SetHealth(float newHealth)
    {
        CRY_ASSERT_MESSAGE(gEnv->bServer, "Health can only be modified on the server!");
        // Update the local state
        m_health = newHealth;
        // Notify the network that the health aspect has changed
        // This will result in NetSerialize being called to serialize data over the network
        NetMarkAspectsDirty(HealthAspect);
    }

protected:
    float m_health = 100.f;
};

◆ NetReplicateSerialize()

virtual void IEntityComponent::NetReplicateSerialize ( TSerialize ser)
inline
virtual

Used to match an entity's state when it is replicated onto a remote machine. This is called once when spawning an entity, in order to serialize its data - and once again on the remote client to deserialize the state. Deserialization will always occur before IEntityComponent::Initialize is called.

Parameters
[in,out]serSerializer for reading / writing values.
See also
ISerialize::Value()
#include 

class CMyReplicatedComponent final : public IEntityComponent
{
public:
    static void ReflectType(Schematyc::CTypeDesc& desc) { /* Reflect the component GUID in here. */}

    // Implement NetReplicateSerialize, called on the server when an entity with the component is spawned
    virtual void NetReplicateSerialize(TSerialize ser) override 
    {
        ser.Value("health", m_health);
    }

    void SetHealth(float health) { m_health = health; }

protected:
    float m_health = 100.f;
};

void SpawnEntityOnAllClients()
{
    CRY_ASSERT(gEnv->bServer);

    // Spawn a new entity, but do *not* automatically initialize it
    // This is delayed as want to add our component before network replication occurs
    if (IEntity* pEntity = gEnv->pEntitySystem->SpawnEntity(SEntitySpawnParams(), false))
    {
        // Create an instance of our component and attach it to the entity
        CMyReplicatedComponent* pComponent = pEntity->CreateComponentClass();
        // Change the initial health, as an example of replication a new state on the remote clients
        pComponent->SetHealth(0.f);

        // Now initialize the entity, resulting in CMyComponent::NetReplicateSerialize first being called on the server (this instance) to write the data to network, and then on all clients to deserialize.
        gEnv->pEntitySystem->InitEntity(pEntity, SEntitySpawnParams());
    }
}

◆ NetSerialize()

virtual bool IEntityComponent::NetSerialize ( TSerialize ser,
EEntityAspects aspect,
uint8 profile,
int flags
)
inline
virtual

Network serialization. Will be called for each active aspect for both reading and writing.

Parameters
[in,out]serSerializer for reading/writing values.
[in]aspectThe number of the aspect being serialized.
[in]profileCan be ignored, used by CryPhysics only.
[in]flagsCan be ignored, used by CryPhysics only.
See also
ISerialize::Value()
#include 

class CMyNetSerializedComponent final : public IEntityComponent
{
public:
    // Provide a virtual destructor, ensuring correct destruction of IEntityComponent members
    virtual ~CMyNetSerializedComponent() = default;

    static void ReflectType(Schematyc::CTypeDesc& desc) { /* Reflect the component GUID in here. */}

    // Define the aspect / stream to use for this aspect, in our case we want a server stream since only the server can change health
    static constexpr EEntityAspects HealthAspect = eEA_GameServerA;
    // Override GetNetSerializeAspectMask to specify that we will handle HealthAspect
    virtual NetworkAspectType GetNetSerializeAspectMask() const override { return HealthAspect; }

    // Override NetSerialize, called on the server when the aspect has been marked dirty, and then on the client to read the data
    virtual bool NetSerialize(TSerialize ser, EEntityAspects aspect, uint8 profile, int flags) override
    {
        // Check that we are processing the desired aspect
        if (aspect == HealthAspect)
        {
            // Serialize health, either to network (on the server) or from network (on a client)
            ser.Value("health", m_health);
        }
    }

    // Helper utility that game code should call to set the health of this component
    void SetHealth(float newHealth)
    {
        CRY_ASSERT_MESSAGE(gEnv->bServer, "Health can only be modified on the server!");
        // Update the local state
        m_health = newHealth;
        // Notify the network that the health aspect has changed
        // This will result in NetSerialize being called to serialize data over the network
        NetMarkAspectsDirty(HealthAspect);
    }

protected:
    float m_health = 100.f;
};

◆ PreInit()

virtual void IEntityComponent::PreInit ( const SInitParams & params)
inline
protected
virtual

Only called by system classes to initalize component. Users must not call this method directly

◆ ProcessEvent()

virtual void IEntityComponent::ProcessEvent ( const SEntityEvent & event)
inline
protected
virtual

By overriding this function component will be able to handle events sent from the host Entity. Requires returning the desired event flag in GetEventMask.

Parameters
eventEvent structure, contains event id and parameters.
#include 

class CMyEventComponent final : public IEntityComponent
{
public:
    // Provide a virtual destructor, ensuring correct destruction of IEntityComponent members
    virtual ~CMyEventComponent() = default;

    static void ReflectType(Schematyc::CTypeDesc& desc) { /* Reflect the component GUID in here. */}

    // Override the ProcessEvent function to receive the callback whenever an event specified in GetEventMask is triggered
    virtual void ProcessEvent(const SEntityEvent& event) override
    {
        // Check if this is the update event
        if (event.event == ENTITY_EVENT_UPDATE)
        {
            // The Update event provides delta time as the first floating-point parameter
            const float frameTime = event.fParam[0];

            /* Handle update logic here */
        }
    }

    // As an optimization, components have to specify a bitmask of the events that they want to handle
    // This is called once at component creation, and then only if IEntity::UpdateComponentEventMask is called, to support dynamic change of desired events (useful for disabling update when you don't need it)
    virtual uint64 GetEventMask() const override { return BIT64(ENTITY_EVENT_UPDATE); }
};

◆ SendEvent()

void IEntityComponent::SendEvent ( const SEntityEvent & event)
inline

Send event to this specific component, first checking if the component is interested in the event

Parameters
eventdescription
receivingcomponent

◆ SetComponentFlags()

void IEntityComponent::SetComponentFlags ( ComponentFlags flags)
inline

END IEntityComponent virtual interface.

Set flags for this component

◆ SetName()

void IEntityComponent::SetName ( const char * szName)
inline

Set a new name for this component Names of the components must not be unique

Member Data Documentation

◆ m_pClassDesc

const CEntityComponentClassDesc* IEntityComponent::m_pClassDesc = nullptr
protected

Reflected type description for this component Contain description of the reflected member variables