Attach component to entity[SOLVED]

Hi, I'm trying to spawn an entity and attach a component to it. I've successfully created a function for spawning the entity and such as shown here:

Code: Select all

SEntitySpawnParams spawnParams; spawnParams.pClass = gEnv->pEntitySystem->GetClassRegistry()->FindClass(""); spawnParams.sName = ""; spawnParams.vPosition = Vec3(500, 580, 30); spawnParams.vScale = Vec3(1, 1, 1); //Spawn the entity if (IEntity* pEntity = gEnv->pEntitySystem->SpawnEntity(spawnParams)) { pEntity->CreateComponentClass<class test>(); }
What I want to do is that I want the entity to choose a random component class to attach to it. I've successfully created a function that does this for the "spawnParams.pClass", but the problem is that I can't use the same method for this code as it wants a class as input.

Code: Select all

pEntity->CreateComponentClass<class test>();
I tried creating a function that returns a pointer to a class as shown here:

Code: Select all

IEntityClass* GetClass(const string* sClass) { IEntityClass* pEntityClass(NULL); pEntityClass = gEnv->pEntitySystem->GetClassRegistry()->FindClass(sClass); return pEntityClass; }
Then I tried to call that function like this:

Code: Select all

But that gave me an error saying: ""This" can't be used with a constexpr" or something like that. (BTW I know that there is some code missing like the declaration for sClass in the first code).

Anybody who can help me figure out how to this?

Last edited by tinkertrain on Wed Mar 28, 2018 2:53 pm, edited 1 time in total.

Re: Attach component to entity

Wow, it's completely wrong. IEntityClass is not IEntityComponent. Not mentioning, that you try to get pointer to the class. While spawning entity, you have to use default class so in spawnParams you go...
spawnParams.pClass = gEnv->pEntitySystem->GetClassRegistry()->GetDefaultClass ();

This way you spawn empty entity without components. Now.. to create component for entity you go

So this is first thing. Secondly you cant return pointer to class like this. C++ does not have reflection originally. To do this, you would need a lot more work than this. However it can be solved by using polymorphism or maybe if you already have some machine that randomly chooses component by name, pass entity pointer to this function along with class name and then depending on string you do simple checks if (componentClass == "SomeClass")
Cryengine tutorial videos on my Youtube channel! Check it out !

Re: Attach component to entity

Yeah, there are many ways to approach your issue, but getting class pointer is not one of them. You're right, that solution I proposed is rather bad and is more like a workaround. You can also do something different. Instead of attaching component. Create components that you want, and expose them for editor and schematyc. Then go to schematyc editor, create entities for each component and save them in some folder. Then in c++ you can actually spawn entity with specified component from start. So in c++ you go spawnParams.pClass = gEnv->pEntitySystem->GetClassRegistry()->FindClass (schematyc::yourfolder::entityname);

Tell me what do you want to achieve, I might be able to have better solution
Cryengine tutorial videos on my Youtube channel! Check it out !

Who is online

Users browsing this forum: No registered users and 1 guest