Classes in C# are some of the foundation programming building blocks of object oriented programming. In a nutshell a class is a blueprint of an object that holds variables, methods, properties, etc. This allows you to create custom types that you can use in your game to modularize your code. So what does a class look like? Let’s look at a very basic example.
So here we have a customer class to hold customer data and then because it’s not a monobehaviour (find out more here but essentially an important Unity class) you can see we have a Constructor for automating the creation of Customer objects.
Constructors are like little factories that pump out a particular object blueprint when you ask. Automating the process so you don’t have to write tons of boiler plate code, they sometimes take input variables if made that way.
Here’s the customer database class (which attaches to a Unity object and has to derive from Monobehaviour to do that) that utilizes that constructor. Down below you can see the code displayed in the inspector.
Another powerful feature of classes is the ability to use class inheritance. Essentially letting you split up variants so not everything is cluttered up in your main class script. Lets illustrate this with an Item class and it’s inherited Weapon class.
This is the base class that Weapon will inherit form. You would put your most critical variables/functionality here.
Now this is what ties it all together, the Item Database. This derives from monobehaviour so that you can see the (assuming you make them serializable) Items/Weapons/etc in the inspector. This is where the two data container classes you made previously get used.
With both items created and being used in the Item class you can see the differences (and similarities) between the two. Weapon carries over the base Item class’ data but then adds it’s own additional data. This allows you to split up and organize your code in a way that all your unique item types are sitting in a fat Item class that’s bloated beyond belief.
Let’s put this into practice by giving our items some functionality. Here are the scripts with 3 new methods. Two for Item class that adds a Buy and Sell method that all Items have. Then the last method is exclusive to the Weapon class that lets you Upgrade it, we’ll then call the methods from the Item Database.
Now we have our new Item Database utilizing those new methods.
With the fields filled in for the items you can see the methods at work.
Another important feature of inheritance is virtual methods and overriding. Virtual methods essentially allow you to modify a base class’ methods with the override keyword in derived class. Let’s see how this works to give a better idea about it’s implementation.
Here in our Item class we changed the Sell method to a Virtual Method. What that means is simple, in the Weapon class we can create our own Sell method with the override keyword.
It automatically lets you still call the base class method functionality using “base.Sell();” but if we delete it we can remove that functionality. Here’s what it looks like in action after we changed it to say “You trade it for a hat.”
As you can see this this allows you to customize code while having a shared codebase to cutdown on boilerplate code. This is a pretty dense topic but once you digest the value of classes you begin to see the beauty of object oriented code and it’s ability to be elegant but modular. Until next time, happy coding.