Most recently in GAM111, we have been implementing weapon systems into our SHMUP. Our teacher recommended the use of inheritance to group our weapon systems, a move that I appreciated. When I learned inheritance in my previous trimester in Scripting 1, it was a brainwave. Inheritance allows a parent class to have many classes which derive from it, something which will save you repeating yourself into the wee hours of the morning. Inheritance and Polymorphism make programmers’ lives easier. Particularly in the Games’ Industry, Inheritance and Polymorphism allow simple and logical implementation of weapon systems, enemies and other objects which can be grouped.

Inheritance and Polymorphism are the second and third pillars of Object-oriented programming (OOP). The two principles allow for classes which can derive and override each other to streamline development. Inheritance – as I described above – allows a class to inherit the properties of its parent. In the context of the SHMUP we are developing, we use Inheritance to create a “weapon” parent class for which the actual class can derive. For  example, we want to have three weapon types, listed below:

 

Untitled drawing-2
We can imagine these as Machine Gun, Rocket Launcher, Laser.

Each of the weapon classes must have its independent properties, but some of their functions are similar to each other, and can be grouped using Inheritance.

1
All three classes have the same properties and functions, but interact with them all differently

Inheritance and polymorphism require a parent class for which the actual weapon classes can derive. This class will be an empty class that has the basic skeletal properties and associated variables.

1-2
Child Classes can take their base properties from the parent class

Polymorphism – the morphing of inherited classes – enables child classes to change properties that it derives from its parent. As such, Polymorphism and Inheritance to go hand-in-hand; Inheritance lets classes copy properties and Polymorphism lets them be changed. For each of the three weapon types to be independent, they can’t be the same as each other or the parent. A rocket launcher will have a longer recoil time and do different damage than the machine gun, and the same is true of the laser. The parent class defines a “Shoot()” function, but all the child classes will shoot differently. Polymorphism allows us to change this class-to-class. For example, the parent class is a basic class which inherits from Monobehaviour (the default for Unity, which provides some basic functionality that we can take advantage of).

Public Class ParentClass: Monobehaviour {
   Public int ammo;
   Public int damage;
   Public float range;
   Public float rateOfFire;
   Public GameObject anotherObject;

   public virtual void Update(){
      Shoot() // Execute the shoot function every frame
   }
 
   public virtual void Shoot
   {
      //Shoot at the player
   }

Here, we have an empty parent class with a barebones structure. If a class has access to this parent, it will inherit all of its variables and functions. Polymorphism, on the other hand, requires that the functions be “Public” and “Virtual”, as we will notice in the Public Virtual Void Update() and Public Virtual Void Shoot() functions. We can override these properties in the child classes:

Public Class WeaponA: ParentClass {
   Public int ammo = 2;
   Public int damage = 10;
   Public float range = 0.5f;
   Public float rateOfFire = ;
   Public GameObject anotherObject;

   public Override void Update(){
      Shoot() // Execute the shoot function every frame
   }
 
   public Override void Shoot
   {
      //Shoot at the player
   }

Similar to above, we have identical functions and similar variables, except they are now defined. The fundamental differences are that Public Class WeaponA: ParentClass will define the class WeaponA as being a child of ParentClass. Furthermore, Public Override will force the Update and Shoot functions to override the parameters in the ParentClass – using Polymorphism.

The applications for these concepts are endless. In my previous trimester, we used them to create a physics-based game, which I called Super Hacker! a game where the player used blocks that adjust the speed and trajectory of a ball for it to reach the end of the level. The blocks inherited from the class BlockController, and each would override its functions so that they interacted with the ball differently. For the SHMUP we are developing, we are using them already for the weapon system. In future, Inheritance and Polymorphism can be used to create varying types of enemies – those with different weapons, different attack patterns, varying levels of health and speed. The applications are nearly endless.

I found Inheritance and Polymorphism extremely useful concepts to use, and there’s a guarantee that the Industry requires them. For now, we are finishing up the SHMUP game, before moving onto a First-Person survival game in-class. Is there a chance I’ll be revisiting these concepts again? Absolutely.

References:

Inheritance (C# Programming Guide). (2015). Msdn.microsoft.com. Retrieved 23 June 2016, from https://msdn.microsoft.com/en-us/library/ms173149.aspx

Polymorphism (C# Programming Guide). (2016). Msdn.microsoft.com. Retrieved 23 June 2016, from https://msdn.microsoft.com/en-us/library/ms173152.aspx

What is object-oriented programming (OOP)? – Definition from WhatIs.com. (2008). SearchSOA. Retrieved 23 June 2016, from http://searchsoa.techtarget.com/definition/object-oriented-programming

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s