Skip to content

Commit

Permalink
!B (CryEntitySystem) Fix inability to reliably query other Schematyc …
Browse files Browse the repository at this point in the history
…components inside the Initialize call, we now add a Schematyc object's components without initializing them, and then go throguh all components again to initialize them in the added order.

Review: ID 67357 by Author: Filip Lundgren, Reviewer: Alexander Klinger, Observer: Thomas Wollenzin, Observer: Sebastien Laurent, Observer: Nico Moss, Observer: Dominique Mader
 (Approved by filipl)

Copied from Perforce
 Change: 1535890
  • Loading branch information
Cry-59 committed Jul 18, 2017
1 parent 9ec7ca6 commit 7b42549
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
3 changes: 3 additions & 0 deletions Code/CryEngine/CryCommon/CryEntitySystem/IEntity.h
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,9 @@ struct IEntity
std::shared_ptr<ComponentType> pComponent = std::make_shared<ComponentType>();
if (AddComponent(pComponent))
{
// Initialize the now added component
static_cast<IEntityComponent*>(pComponent.get())->Initialize();

return pComponent.get();
}

Expand Down
5 changes: 3 additions & 2 deletions Code/CryEngine/CryCommon/CryEntitySystem/IEntityComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -503,11 +503,12 @@ struct IEntityComponent : public ICryUnknown
//////////////////////////////////////////////////////////////////////////

protected:
friend IEntity;
friend class CEntity;
// Needs access to OnShutDown to maintain legacy game object extension shutdown behavior
friend class CGameObject;
// Needs access to Run, remove when the function is gone
friend class CEntityObject;
// Needs access to Initialize
friend class Schematyc::CObject;

// Host Entity pointer
IEntity* m_pEntity = nullptr;
Expand Down
13 changes: 8 additions & 5 deletions Code/CryEngine/CryEntitySystem/Entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1436,8 +1436,11 @@ void CEntity::LoadComponent(Serialization::IArchive& archive)
//classProperties.Apply(componentClassDesc, pComponent.get());
Schematyc::Utils::SerializeClass(archive, componentClassDesc, pComponent.get(), "properties", "properties");

// Finally Create and Initialize the component
// Add the component to the entity
AddComponentInternal(pComponent, typeGUID, &initParams, &componentClassDesc);

// Initialize the component
pComponent->Initialize();
}
else
{
Expand Down Expand Up @@ -1807,8 +1810,12 @@ IEntityComponent* CEntity::CreateComponentByInterfaceID(const CryInterfaceID& in
std::shared_ptr<IEntityComponent> pComponent = pEnvComponent != nullptr ? pEnvComponent->CreateFromPool() : cryinterface_cast<IEntityComponent>(pLegacyComponentFactory->CreateClassInstance());
CRY_ASSERT(pComponent != nullptr);

// Add the component to the entity
AddComponentInternal(pComponent, componentTypeID, pInitParams, pClassDescription);

// Initialize the component
pComponent->Initialize();

return pComponent.get();
}

Expand Down Expand Up @@ -1865,12 +1872,8 @@ void CEntity::AddComponentInternal(std::shared_ptr<IEntityComponent> pComponent,
// Sorted insertion, all elements of the m_components are sorted by the proxyType
m_components.Add(componentRecord);

// Call initialization of the component
pComponent->Initialize();

// Entity has changed so make the state dirty
m_componentChangeState++;

}

//////////////////////////////////////////////////////////////////////////
Expand Down
7 changes: 7 additions & 0 deletions Code/CryEngine/CrySchematyc/Core/Impl/Object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,13 @@ bool CObject::CreateComponents()

pEntity->AddComponent(component.pComponent, &initParams);
}

// Now initialize all the components
// This is done in a separate iteration step from adding in order to allow components to query each other in the Initialize call.
for(const SComponent& component : m_components)
{
component.pComponent->Initialize();
}
}

return true;
Expand Down

0 comments on commit 7b42549

Please sign in to comment.