Sign in to follow this  
Spawn_Kcb

Classes and inheritance?

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
+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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this