Objective: Understand and implement class inheritance.
Relax, your great-aunt Flo is fine. In Object Oriented Programming (O.O.P.), inheritance enables new objects to take on the properties of existing object. In this tutorial I will show you exactly what this means and how to use it in your benefit.
We are going to start by creating a custom class for Item.
Every item will have a name, Item ID, and an icon. These are the base characteristics of an item.
For the sake of this tutorial let’s say you want to have some weapons items (axe, dagger, spear) and food items (bread, soup, fish). It would not make since to populate the Item class with these if they aren’t going to be used by the other items. Instead, we can get into class inheritance where we define weapons as an offset to Item.
Create a custom class for Weapon. In this custom class we are going to define all the common traits that a weapon will have. However, before we can start defining weapons, the weapon is still going to be an item. So, instead of the weapon inheriting MonoBehavior, it needs to inherit Item.
By doing this, this weapon is now an item and now has all the items properties. This means if we create a method on the weapon class you can still access the name, item id, and icon from the item class because weapon inherit the item and those properties.
This means we could overwrite what is on the item class however, that is not the point to the weapon class. The point to the weapon class is to define the traits of the weapon.
This is how a weapon differentiates itself from the traditional items. Now we can create another base class for the food items.
Now we can start creating items. Create an empty object in Unity to act as the item database. Within the database we need to create a new C# script, this is just a MonoBehavior script that houses all the items in the game.
In the item database we can start adding items. Let’s begin by making a random item. Let’s go with a map.
Let’s create a weapon.
Now, let’s create a food item.
It is important to remember each of these is inheriting from the item class. We want to see these items in the inspector so serialize the item class.
You’ll notice in the inspector that the map is the only item being shown. This is because we only serialized the item class, we need to serialize the weapons and food to see them respectively.
Now with all items being shown you will notice that the Map has the three defining traits we set earlier. The weapon has those same traits as well as the traits in the Weapon class. And of course, the bread has the item traits as well as the consumable traits.
Using class inheritance allows us to create and categorize our weapons and consumables and items so the item class isn’t a cluttered mess. It doesn’t make since for the item class to accommodate for every item in the game. So we break it apart using class inheritance to signify different types of objects allowing us to create much more maintainable and neatly written code. As i always say, don’t be afraid to experiment with your code, and I’ll see you in the next tutorial.