How to edit "Inheritance" in Object Oriented Programming?

Started by
5 comments, last by supesfan 11 years ago

Hello World,

I have a question about extending child classes from parent classes in Object Oriented Programming. To be specific I am talking about UnrealScript, but I am pretty sure this concept is a kin to all OOP languages.

Lets say I am making a FPS game. I have 4 different weapon types, Pistols, Shotguns, Rifles and Rocket Launchers. Obviously all of these weapons are going to have functionality that is the same like ammo, accuracy, damage and so on. So you would make a parent class with all of the common functionality and extend from that class when programming your weapons so in the child class you didn't have to rewrite all of that code.

However, what if you were writing the code for the rocket launcher for example, and you wanted most of the functionality that the weapons parent class offers, but some of it you didn't want? How would you avoid extending some of the functionality that the parent class has while still keeping most of it?

Here is some example code roughly written:

//Parent Class

class BasicWeaponFunctionality extends UTWeapon;

placeable;

/** Initial ammo count if in weapon locker */
var int LockerAmmoCount;
/** Max ammo count */
var int MaxAmmoCount;
/** Holds the amount of ammo used for a given shot */
var array<int> ShotCost;

//Child Class

class RocketLauncher extends BasicWeaponFunctionality;

placeable;

//How would I avoid extending the variable LockerAmmoCount from BasicWeaponFunctionality Class?

I hope I make sense, let me know if I don't so I can clarify just in case.

Advertisement
One option is to use two base classes with one inheriting from the other. You'd have your rocket launcher class inherit from the first base class that doesn't have your locker information, and have the other three weapons inherit from the second base class that adds the locker information.

Oh ok, well that seems easier than I thought it would be. I can't believe I didn't think of that. Hey thanks a lot that should work just fine!

Lets say I am making a FPS game. I have 4 different weapon types, Pistols, Shotguns, Rifles and Rocket Launchers. Obviously all of these weapons are going to have functionality that is the same like ammo, accuracy, damage and so on. So you would make a parent class with all of the common functionality and extend from that class when programming your weapons so in the child class you didn't have to rewrite all of that code.

This is called 'implementation inheritance', and while it is a common pattern in OOP languages (especially 90's C++ and Java), it wasn't originally a part of Object Oriented Design, so it's not the only option. The OO approach to this situation would be to put all that common code into a class / classes, and then having your new class gain access to / reuse that code by using that class / those classes as member variables (aka 'composition').

You could take a composition approach. Create classes that handle ammo storage, damage/accuracy calculations and whatever else you need for weapons. You then create individual weapon classes that have instances of those basic weapon component classes. This way you can pick and choose which weapon functionality you want for each weapon. It also allows you to easily construct specialisations of any piece of weapon functionality to use in specific weapon classes without having to worry about the effect it has on other weapon classes.

It's not a bug... it's a feature!
I don't know UnrealScript or your design, but here's how it looks to me:

All the variables you mention are not part of the game state, but are fixed properties of the weapon type. It would be better to separate that data and store it once per weapon type.

As it's just data there's no need for inheritance. If a particular weapon doesn't use one of the fields, that's fine.

For the code, the usual way to change the behaviour of a base class is to make the function virtual and override it.

Ok, well the way it is setup is so I can minimize the amount of coding needed to be done. I am actually using Epic Games Unreal Tournament code as a base for my own project. It saves me a lot of work in the long run since all of the default values for weapons are already set, this way the changes I make will be fast and minimal. I don't really want to go through the trouble of rewriting any of the default code. Thanks anyways.

This topic is closed to new replies.

Advertisement