Click here to Skip to main content
15,881,172 members
Articles / Programming Languages / Visual Basic

Game Class Inheritance

Rate me:
Please Sign up or sign in to vote.
1.78/5 (9 votes)
14 Jul 2008CPOL2 min read 24.3K   144   6  
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.

VB
'(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:

VB
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.

Image 1

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)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --