Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


How to edit "Inheritance" in Object Oriented Programming?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 supesfan   Members   -  Reputation: 226

Like
0Likes
Like

Posted 19 March 2013 - 04:49 PM

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.



Sponsor:

#2 SiCrane   Moderators   -  Reputation: 9626

Like
2Likes
Like

Posted 19 March 2013 - 05:02 PM

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.

#3 supesfan   Members   -  Reputation: 226

Like
0Likes
Like

Posted 19 March 2013 - 05:53 PM

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!



#4 Hodgman   Moderators   -  Reputation: 30973

Like
3Likes
Like

Posted 19 March 2013 - 06:04 PM

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').

#5 Dom_152   Members   -  Reputation: 453

Like
0Likes
Like

Posted 19 March 2013 - 06:57 PM

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!

#6 EWClay   Members   -  Reputation: 659

Like
0Likes
Like

Posted 20 March 2013 - 04:28 AM

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.

#7 supesfan   Members   -  Reputation: 226

Like
0Likes
Like

Posted 23 March 2013 - 09:58 AM

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.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS