5.4 C++ Getting Component

#1
Hi. Since Crytek introduced us to component entity system is has been pain in my ass. With the old entity system I was able to create classes i.e Item, then Weapon, then Rifle. All in hierarchy. Rifle inherited from Weapon and Weapon from Item. This way I could always check easily, when I looked at certain entity if it's weapon, and then get it's name and other properties.

To the point.
How do I achieve that in component system? Example
I have CItemComponent class, which is abstract.
I have CRifleComponent class which derives from CItemComponent

Now.. I am trying to get name of this damn Rifle when I look at it.
So when I look at it I try:

Code: Select all

CItemComponent *pItem = pEntity->GetComponent<CItemComponent>();
pItem returns false;
It returns true though when:

Code: Select all

CRifleComponent *pItem = pEntity->GetComponent<CRifleComponent >();
But I don't want to create thousands of "IFs" in my PickUp function, in case of Rifle1, Rifle2, Rifle3 etc.
I want to use the class on the top, because It already provides my OnPickedUp() method for the Rifle.
In previous entity system I did:

Code: Select all

IGameObjectExtension *pObj = gEnv->pGameFramework->QueryGameObjectExtension(pEnt->GetId(), pEnt->GetClass()->GetName()); SItem *pItem = static_cast<SItem*>(pObj); CWeaponClass *pWeapon = static_cast<CWeaponClass*>(pItem);
It's just example but I think you know what I mean. With component system I can't do it.

I use templates, not GameSDK. And I don't care about GameSDK.

Thanks for any help
Cryengine tutorial videos on my Youtube channel! Check it out !
https://www.youtube.com/user/MusicForLifePL20

Re: 5.4 C++ Getting Component

#2
You need to add the base to your components children reflected types by calling AddBase(<type>) on the Schematyc::CTypeDesc given eg;

Code: Select all

// "BulletBase.h" struct IBulletBase : public IEntityComponent { // IEntityComponent static void ReflectType(Schematyc::CTypeDesc<IBulletBase>& desc) { desc.SetGUID("{00000000-0000-0000-0001-000000000000}"_cry_guid); } // ~IEntityComponent }; class CBulletSomething final : public IBulletBase { public: // Reflect type to set a unique identifier for this component static void ReflectType(Schematyc::CTypeDesc<CBulletSomething>& desc) { desc.SetGUID("{00000000-0000-0000-0001-000000000001}"_cry_guid); desc.AddBase<IBulletBase>(); } }; class CBulletDifferent final : public IBulletBase { public: // Reflect type to set a unique identifier for this component static void ReflectType(Schematyc::CTypeDesc<CBulletDifferent>& desc) { desc.SetGUID("{00000000-0000-0000-0001-000000000002}"_cry_guid); desc.AddBase<IBulletBase>(); } };
and you would use simply;

Code: Select all

if (SomeEntity->GetComponent<IBulletBase>(true)) CryLogAlways("Shots Fired!");
hope this helps
Uniflare
CRYENGINE Technical Community Manager
Here to help the community and social channels grow and thrive.

My personal belongings;
Beginner Guides | My GitHub | Splash Plugin

Re: 5.4 C++ Getting Component

#3
I did that, but when I use desc.AddBase<CBaseComponent>(); it doesn't compile. It throws:
CItemComponent is a base component.
AK47 derives from it and is final component.

Code: Select all

1>------ Build started: Project: Game, Configuration: Debug x64 ------ 1>Weapons_uber.cpp 1>D:\Cryengine\CRYENGINE Launcher\Crytek\CRYENGINE_5.4\Code\CryEngine\CryCommon\CrySchematyc/Reflection/TypeDesc.h(555): error C2338: Type must be reflected, see TypeDesc.h for details! 1>D:/Cryengine/CRYENGINE Launcher/Crytek/Projects/WW2_P4/Code/ItemSystem/Weapons/AK47.cpp(20): note: see reference to function template instantiation 'bool Schematyc::CClassDescInterface<TYPE,CEntityComponentClassDesc>::AddBase<CItemComponent>(void)' being compiled 1> with 1> [ 1> TYPE=CAK47 1> ] 1>D:/Cryengine/CRYENGINE Launcher/Crytek/Projects/WW2_P4/Code/ItemSystem/Weapons/AK47.cpp(20): note: see reference to function template instantiation 'bool Schematyc::CClassDescInterface<TYPE,CEntityComponentClassDesc>::AddBase<CItemComponent>(void)' being compiled 1> with 1> [ 1> TYPE=CAK47 1> ] 1>Done building project "Game.vcxproj" -- FAILED. ========== Build: 0 succeeded, 1 failed, 1 up-to-date, 0 skipped ==========
EDIT:

I fixed it! It was simple issue, and easy to miss. I placed ReflectType method inside the private accesibility area :D

Fix:
Remember:
static void ReflectType(Schematyc::CTypeDesc<CYourClass>& desc);
must be
public:

Thank you! It works now :)
Cryengine tutorial videos on my Youtube channel! Check it out !
https://www.youtube.com/user/MusicForLifePL20

Who is online

Users browsing this forum: No registered users and 2 guests

cron