An introduction to C#, Object-oriented programming?

Hi everyone!

This is my first post in what I hope to be a series of tutorials about Unity3D. It’s theme was a request of many people and in the end it also helped me understand many things while I was searching about.

So this tutorials will contain what I have found, my personal opinions and some of the discussion I had with some friends, so feel free to contact me if you think something is amiss.

Remember that those tutorials expects you to have some kind of programming experience, and they reflect my personal experience and may not agree with everyone.

What is Object-Oriented Programming?

OOP is a programming paradigm, which I didn’t really know what it meant at the time, I always thought it was just a way to write code.
Turns out it’s actually a classification of programming languages, or from what I understood an property that is intrinsic to the programming language.

What does that means? Well for one it implies if you are using C# you are already programming with OOP! Well, you could be badly programming in OOP but you still are! So this tutorial will also try to teach some good code practices which will greatly improve your code.

For other, I also founds things like “component oriented programming” and “interface oriented programming” are not paradigms, because they can be done with current programming languages and are not a classification of them, they are just software designs.

How do I OOP?

So OOP languages are based on the fact that you write objects who hold datas and procedures. If you don’t come from a procedural language this just means an object contains information (fields, properties) and also ways to process information (methods).

What is an Object?

In C# this would be instances of your classes, when writing code you will open it with a class declaration like:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TinyObject : MonoBehaviour {

  protected int favoriteNumber;

  public int WhatIsMyFavoriteNumber() {
    return favoriteNumber;
  }
}

This mean you are declaring a class called TinyObject, that extends MonoBehaviour (this is an Unity3D class, do not worry if you do not know it), it contains the data favoriteNumber which is an integer and a method called WhatIsMyFavoriteNumber that returns the favoriteNumber when called.

While the class is called TinyObject, the class itself is not an object, it is some kind of template for creating new objects with it, which are called instances.

So to better understand it let me give some functional examples. Let’s say you want to have containers on your game that are able to hold items inside.

We will start with a generic container class that gives functionality to hold items.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Container : MonoBehaviour {

	protected Item[] itemsInside;
	public int maxItemsCanHold = 5;

	void Awake() {
		AwakeInit();
	}

    protected virtual void AwakeInit() {
        itemsInside = new Item[maxItemsCanHold];
    }

	public bool PutItemInside(Item itemToHold) {
		for (int count = 0; count < itemsInside.Length; count++) {
			if (itemsInside[count] == null) {
				itemsInside[count] = itemToHold;
				return true;
			}
		}

                return false;
	}
}

This class would allow you in Unity3D to create things in your game that can store items inside of it.

The public keyword means anything can access this data, in Unity3D it will also automatically serialize the field. (You can learn about serialization later!)
The protected keyword means instances of this class and also classes extending from it can access this data.
The virtual keyword means this method can be overwritten in an extended class.
Anything without an access keyword is private by default, meaning only instances of the same class can access it. (In this case, only another Container)

The AwakeInit method is a workaround you have to do in Unity3D due to the magic method Awake() not being possible to overwrite, this mean that if we did any code inside an Awake() method we would have to copy and paste it inside any class extending from our Container if we wanted to keep it’s functionality.

But what if you want to have an wardrobe, a kind of container able to store a few items and also clothes, in a way that misc items can’t be stored in clothes places?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Wardrobe : Container {

	protected Cloth[] clothesInside;
	public int maxClothesInside = 10;

	protected override void AwakeInit() {
		base.AwakeInit();
		clothesInside = new Item[maxClothesInside];
	}
    	
	public bool StorePieceOfClothing(Cloth clothToHold) {
		for (int count = 0; count < clothesInside.Length; count++) {
			if (clothesInside[count] == null) {
				clothesInside[count] = clothToHold;
				return true;
			}
		}
                return false;
	}
}

This declares our Wardrobe class, it is very similar to our Container class but you can see some differences in types.

Since our Wardrobe extends from Container, it does everything a container do, so it already have an Item array and a PutItemInside method!

Since it already have our Awake() method, we don’t need to write it again, this is an important good practice: reuse code!

We overwrite our AwakeInit method and inside it we make a call to our base AwakeInit() (the method that we are overwriting) on the Container class, this makes sure that everything the Container is doing inside that method is also applied to our Wardrobe without we having to copy and paste the code, another good practice: avoid duplicate code!

Now we have a class that is able to both store items and also articles of clothing in different spaces!

We could keep extending it to have all kinds of different containers, some examples:

And you can keep extending from them, maybe extend from Refrigerator to make a Fridge? Or from a bookshelf to make one that automatically organizes your books!

In my opinion OOP is a very nice neat thing they came up with, you can even add some designs on top of it to fit your needs like MVC or Component!

In a Nutshell!

– In OOP you create classes, which will be used to create instances of it, those instances are your Objects.
– You can extend from classes to create other classes with similar functionality.
– Instances are individual, you can have two objects of the same class with different values on them.

 

Thanks for reading my Tutorial, I hope it was of help to you!

Liked it? Take a second to support TinyBird Games on Patreon!

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.