• Advertisement
Sign in to follow this  

Some game architect problem

This topic is 1785 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

class Humanoid
{
void Walk();
void Jump();
void Talk();
}

class Human : public Humanoid
{
void MuckAround();
}

class Goblin : public Humanoid
{

}

class Main
{
vector <Humanoid*> objs;
}


objs.push_back(new Human());
objs.push_back(new Goblin());
std::vector::iterator it, itEnd = objs.end();
for (it = objs.begin(); it != itEnd; it++)
{
*it->Talk();
*it->MuckAround();
}

 

I think the human object still can't muck around. Is there a major flaw in this design?

Thanks Jack

Edited by lucky6969b

Share this post


Link to post
Share on other sites
Advertisement

I'm not sure I understand your question, but I can see that when the you try to call MuckAround() on a Goblin object it would crash at runtime because the Humanoid class doesn't define that function - is this what you were wondering?

Share this post


Link to post
Share on other sites

I'm not sure I understand your question, but I can see that when the you try to call MuckAround() on a Goblin object it would crash at runtime because the Humanoid class doesn't define that function - is this what you were wondering?

This code will not compile.

Because MuckAround is not defined in Humanoid. So you cant call it on the base class.

Share this post


Link to post
Share on other sites
What you're looking for are virtual members (base class functions). That MuckAround() method in Humanoid should be:

virtual void MuckAround() {}

In your class definition. Sorry if you don't see that in code tags but I'm on my phone atm. If the return type isn't "void", change it to what it should be and return some default value, and it'll compile when called.

EDIT: my bad, I skimmed through it. Looks like you're just trying to call the subclass method, not override. As a rule of thumb, if you declare a method, always define it.

Share this post


Link to post
Share on other sites

Hi lucky,

 

I know what you asked has probably already been answered, but looking at your design based on inheritance, I feel there's a need to at least tell you about architectural decisions in a game engine. As your game grows and you need to keep on adding functions and different classes over the inheritance system, things start to mess up. You need to copy some code from one class to another that does not directly correlate via inheritance.

 

Some bibliography:

http://cmpmedia.vo.llnwd.net/o1/vault/gdccanada09/slides/marcinchadyGDCCanada.ppt

http://www.learn-cocos2d.com/2010/06/prefer-composition-inheritance/

http://gamearchitect.net/2008/06/01/an-anatomy-of-despair-aggregation-over-inheritance/

 

Hope this sheds some light if not for this, for future projects.

Andy

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement