Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

no one

entity factory....

This topic is 5223 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hiya I had written an entity system a few weeks ago (just now got time to work on it). I had made a scene graph class and a factory class. The system works fine except for one thing... heres how I may create the scene... CEntity *root = g_EntityFactory->CreateEntity("terrain"); CEntity *ent1 = g_EntityFactory->CreateEntity("player"); CEntity *ent2 = g_EntityFactory->CreateEntity("pistol"); ent1->AddChild(ent2); root->AddChild(ent1); etc.. g_scene->SetRoot(root); g_scene->InitScene(); *The entity factory... CEntity *CreateEntity(string ent)* Now you would have a scene consisting of a terrain, a user interactive player entity with a gun. The only problem with this is: CEntity *ent1 = g_EntityFactory->CreateEntity("player"); ent1->mpSkin = "models/player/skin2.tga"; //illegual since mpSkin is not a memebr of CEntity! Heres what I need... CPlayer *player = new CPlayer; player->mpSkin = "..."; root->AddChild(player); ... However, I need make a entity factory that can ecapsulate "player = new CPlayer" or "class_x = new class_x()"; Any ideas how I can do this? Thanks a ton in advance!!! ~Jason

Share this post


Link to post
Share on other sites
Advertisement
This may sound stupid question, or perhaps i didn''t undestood the question, by why dont you make CPlayer a derivated class of CEntity?

Share this post


Link to post
Share on other sites
CEntity* pEnt = g_EntityFactory->CreateEntity("player");
CPlayer* pPlayer = dynamic_cast<CPlayer*>(pEnt);
pPlayer->mpSkin = "the\skin\path.tga";

Share this post


Link to post
Share on other sites
(CPlayer : public CEntity )

Dynamic cast? Cool!.

Hmm...
CEntity* pEnt = g_EntityFactory->CreateEntity("player");
CPlayer* pPlayer = dynamic_cast(pEnt);
pPlayer->mpSkin = "the\skin\path.tga";
...

now Im wondering...
pEnt = pPlayer; ?
or maybe
pEnt = dynamic_cast(pPlayer); ??

Thanks a ton!

edit: not sure how to inlcude angle brackets...

[edited by - no one on June 3, 2004 3:29:03 AM]

Share this post


Link to post
Share on other sites
The warning:

C:\Documents and Settings\twiggy\Desktop\gamedev\My Projects\test\main.cpp(23) : warning C4541: 'dynamic_cast' used on polymorphic type 'class CEntity' with /GR-; unpredictable behavior may result

the error:
crash!

I think it is because I am using pure virtual functions in the base class..?


void InitEntities()
{
g_EntityFactory = new CEntityFactory;
g_scene = new CSceneGraph;

CEntity *root = g_EntityFactory->CreateEntity("dragon");

CEntity *ent1 = g_EntityFactory->CreateEntity("dragon");

CDragon *pDragon = dynamic_cast<CDragon*>(ent1);
pDragon->mpStr = "dragon: test";

CEntity *ent2 = g_EntityFactory->CreateEntity("knight");

root->AttachChild(ent1);
root->AttachChild(ent2);

g_scene->SetRoot(root);
g_scene->InitRoot();

}


edit: this is actually from a simple "test" program, by no means is the engine code like this

[edited by - no one on June 3, 2004 4:12:17 AM]

Share this post


Link to post
Share on other sites
go to your project properties and mark config->c++->enable rtti. Or add the /GR flag. dynamic_cast work like this, if the cast was valid, the result will be a pointer to the object, if it was not, the result will be 0. For instance:


Entitiy* ent = new Player;
Player* obj = dynamic_cast<Player>(ent);
if(obj)
obj->gun = "gun.tga";



Share this post


Link to post
Share on other sites
There is no real need to use a dynamic_cast here.


CEntity *root = g_EntityFactory->CreateEntity("terrain");
CPlayer *ent1 = g_EntityFactory->CreateEntity("player");
CPistol *ent2 = g_EntityFactory->CreateEntity("pistol");
etc.

ent1->mpSkin = "models/player/skin2.tga";


This is off course assuming that CPlayer and CPistol are derived from CEntity.

Share this post


Link to post
Share on other sites
quote:
Original post by rick_appleton
There is no real need to use a dynamic_cast here.


CEntity *root = g_EntityFactory->CreateEntity("terrain");
CPlayer *ent1 = g_EntityFactory->CreateEntity("player");
CPistol *ent2 = g_EntityFactory->CreateEntity("pistol");


This is off course assuming that CPlayer and CPistol are derived from CEntity.


Nonsense. CEntity* will not automatically convert to a CPlayer* even if it is pointing to a CPlayer object.

OP: There are a couple of things going on which are a problem:

Why do you want to only have CEntity pointers? I know you are being abstract but actually they are next to useless.

You need to abstract the initialisation of entities too. Maybe they can take a string or something else. Maybe you could pass them the environment so that they can get what they need to initialise themselves.

Entity::Initialise(Environment& env, int ID);

CPlayer::Initialise(Environment& env, int ID) {
CPlayerData data = env.getDataForPlayers(ID);
InitialiseSelfWithData(data);
}

CTerrain::Initialise(Environment& env, int ID);
CTerrainData data = env.getDataForTerrain(ID);
InitialiseSelfWithData(data);
}

Just an idea. Similar to the Visitor Pattern. Double Dispatch resolves the problem of not knowing what you are initialising.

edit: using dynamic_cast usually indicates there is a problem with your design. It won't scale well and is a maintenance headache with much room for bugs. Better to explicitly put the design into the code as I've demonstrated. It may take more thought and work initially but dynamic_cast is the easy path which is tempting to go down and bypasses any need for design.

[edited by - quorn on June 3, 2004 9:01:20 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by rick_appleton
oops, you're right. You'll need a static cast there.

CPlayer *ent1 = (CPlayer*)g_EntityFactory->CreateEntitity("player");


If you're going to static cast you should spell it out

static_cast<CPlayer*>

Read up on casts at gotw.ca

I still say the design is better-off as in my previous post rather than resorting to casts as a first repsonse to a design problem.

[edited by - quorn on June 3, 2004 10:14:15 AM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!