Click here to Skip to main content
Licence CPOL
First Posted 14 Jul 2008
Views 9,313
Downloads 46
Bookmarked 6 times

Game Class Inheritance

By | 14 Jul 2008 | Article
A way to simplify your game classes and code for maximum efficency.

Introduction

When creating a video game, the amount of coding can become extensive and unorganized. Although games use an abundance (to say the least) of classes, using inheritance, you will find that the process of coding PC video games can be actually quite organized.

Using the code

When a class inherits another class, it basically clones every variable, method, and property, just as if they were all declared in the class (although with limited visibility [private members are only accessible through delegations]). This is a useful avoidance for retyping redundant code. The use is comparable to the blue-print for something... say a house. Instead of creating the same blueprint over and over again, builders are able to copy the plans into use in different jobs.

In this sample, you will see classes for three objects. One is a goblin, and one the main character, and the last, a box. Now, on an intricate level, a goblin, the main character, and a box don't really have a lot in common, but they share some similar properties: they all have dimensions. So using this commonality, you can create an inheritable class that captivates these three objects' basic dimensions.

'(MustInherit Optional)
Public MustInherit Class PhysicalObject()
    Public Area As Rectangle
End Class

Declaring this class saves the trouble of diming an area variable for each class, and ensures consistency in all physical objects in the game. To inherit this into the other classes, you would do:

Public Class Goblin
  Inherits PhysicalObject
End Class
Public Class MainChar
  Inherits PhysicalObject
End Class
Public Class Box
  Inherits PhysicalObject
End Class

Classes may only inherit another class, which can be a bummer, but also forces you to keep your code all consistent and organized. Although only one class may be inherited in each class, inheriting classes that already have inheritance themselves grants access to those inheritances associated with the class. So, if another class were to inherit the "Box" class, they would indirectly also inherit the "PhysicalObject" class.

Using inheritance in your game code is like a flowchart, flowing from vagueness to specification.

In this flowchart, everything is linked with anything it's connected to the left with. Obj is not affiliated with Steel Box, but through the Box class, Steel Box is affiliated with the Obj class, and has all of the same methods, properties, and variables (at least the public ones). Using this method, you can keep all of your code organized and amazingly simple to debug and fix, and this method also speeds up the coding process by saving you time not having to re-declare variables for each individual class, and allows you to hop in at any level and create a new artifact for your game easily. To jump into this flow chart and create for the game a "Golden Box" wouldn't be difficult at all!! Simply inherit the "Box" class and all the box basics are ready to go!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

gerudobombshell



United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 15 Jul 2008
Article Copyright 2008 by gerudobombshell
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid