Classes and inheritance?

Started by
3 comments, last by Spawn_Kcb 13 years, 4 months ago
Two threads in two days. Check me.

Again, I am not looking for "practical" help as such, opinions/guidance.

When implementing a hierarchy, would I be better of creating separate classes (And child classes) for my Hero, Enemies, Treasure etc.

Or would it be more practical to just create one large hierarchy with say "Game objects" as the root node and work my way down?

Either way, I am thankful for the feedback.

Kevin
Advertisement
To me, working your way down, starting with a game object class sounds like the best approach. At least derive the Hero and Enemies from the same class since these should be fairly related.
Inheritance is overused and overrated, particularly by beginners. Composition is generally a superior approach.

Is the Hero really that different from an Enemy? They might have different statistics, be drawn differently and have a different control system (AI as opposed to input devices), but they share a lot in common. Consider data driven solutions for the statistics. A MVC approach can separate the code that renders and drives the model. This way you can have a single Actor class (or whatever you want to call it), and by giving it different statistics, controllers and views customise it, rather than writing lots of derived classes which copies of similar code.

Treasure and other items can benefit from generic, data driven classes too.

For smaller games, keeping everything in one collection of "Game Object" interfaces actually makes writing the game logic harder, because you generally want to recover the type information later. Don't be afraid to have a number of core collection for the various types of interest (Hero, Enemy, Item, ...). You can still reduce code duplication by writing common helper functions that can handle these lists, e.g. using templates in C++ and generics in Java/C#. Other languages generally have similar solutions.

The most important thing is not to waste too much time on this. Good design comes from experience, and experience comes from making mistakes with bad designs. There is no "one true way", every design is a trade off of some kind and different designs suit different programs and programmers.
+1 for composition.
Add suggest to OP that while thinking about inheritance and hierarchy, you may also search and read for the topics on "component and entity", in Google or here.

https://www.kbasm.com -- My personal website

https://github.com/wqking/eventpp  eventpp -- C++ library for event dispatcher and callback list

https://github.com/cpgf/cpgf  cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.

Thanks guys. I am going to go with my gut and use a hierarchy..........

Its working so far but well I think it may grow rather rapidly......

Here's to hoping.....

This topic is closed to new replies.

Advertisement