Introduction
This article will function as the practical part of the previous article on Classes in Programming. We are going to solve an exercise on class structure and implement it in C# in Unity.
If you don’t know what a class is in programming it would be good if you saw the article I wrote on the subject before continuing.
Go to the theoretical part of this article
Exercise Approach
Let’s see an example that occurred to me at the time of writing this article. It’s about modeling the behavior of a village that has inhabitants and these inhabitants can be workers or warriors.
I have highlighted what I consider to be key words in raising the problem, in principle it seems that at least we are going to need two classes to solve it, one for the village and one for the villagers, but we are going to consider the fact that both workers and warriors themselves are villagers.
Workers and Warriors will have common characteristics and behaviors that will be defined in the Inhabitant class and then the Worker and Warrior classes will inherit the characteristics and behaviors of Inhabitant.
A village has inhabitants so the Village class will have among its data one or more instances of the Villagers class.
I would like to mention that knowing the difference between classes and instance of classes is very important, but this article is not about that, just as it is not about inheritance.
At the moment the idea is that we are going to use the Inhabitant class to create all the inhabitant objects that we need, these objects will have their own values (for example different names or age) but they will respect the structure defined in the class.
Class Diagram
Then we said that we are going to use four classes, Village, Villager, Worker and Warrior. The Village class has among its data one or more instances of the Inhabitant class. Worker and Guerrero are a specific type of Inhabitant so inheritance will be used.
The class diagram in Figure 1 is based on the previous paragraph.
In the diagram we observe a “1..*” above Inhabitant, this means that Village will have one or more Inhabitant objects.
In addition, we observe that the inheritance of classes is represented by an upward arrow toward the parent class.
Object Diagram
Once the classes that will be present have been defined, we think about their possible fields and methods. In this part it is good to reflect on the nature of the object we want to model, what its characteristics are, what information it handles and what its behaviour may be.
I’m going to make up some fields and methods for the classes because this practice of programming classes is more about the approach of class structure and its later implementation in Unity in C# language, but without going into details of what is going to be done with this.
Figure 2 shows the object diagrams of the four classes.
Now I am going to explain in detail how the boxes in figure 2 are interpreted.
The Village Object
For this object I considered that every village has a name and a group of inhabitants, so for its fields I am going to use a collection of objects called “inhabitants”, a string called “name” and an integer called “population”. I place these fields at the top of the box as shown in figure 2.1.
Some methods of the object Village can be “NewInhabitant” which will be executed every time an inhabitant is born. “GetName” and “GetPopulation” will return the corresponding data (they are known as “Getters” and are useful for example to make statistics of all the villages we may have).
The “InhabitantDie” method can modify the population and remove the dead inhabitant from the collection of inhabitants.
Finally, the “Feed Inhabitants” method, which can go through the collection of inhabitants executing appropriate methods in each of them, or check the food reserve and subtract as many units as there are populations.
El objeto Habitante
Inhabitant will be the parent class (or superclass) of Worker and Warrior, in this class will be defined the behavior and data that have in common Workers and Warriors.
Every inhabitant has a name represented by a String, an integer for age, another for health and another for defense.
Among the methods of Inhabitant we have the “Getters” to obtain name and age, a private method to age and a public method for the inhabitant to move to a position.
The Worker Object
The Worker Object is a more specific case of the Inhabitant class, between its fields we have a string that indicates the name of the work and a boolean variable to know if it is working or not.
Worker methods are related to what your activities could be, for example Collect, Repair, Drill, Fish and also a private method for you to deliver the resources it has obtained. This last method can be executed internally by Worker for example when it is at the limit of its collection capacity.
The Warrior Object
The Warrior class is a more specific case of the Inhabiting class.
A Warrior may or may not be attacking for which we use the Boolean variable “attacked”, has assigned a weapon represented by a String and has certain attack points represented by an entire variable.
Among its methods are Attacking, Formation, Defending, and a private method to deliver the booty he got after a successful battle.
Implementation in Unity
Now we move on to the good part, let’s take the class diagram and the object diagram from figures 1 and 2 and use them as a reference for creating classes in Unity.
Before you start you might need to know how to use Unity, here on the page I have a couple of series, one called “Unity Fundamental Series” and the other “Labyrinth Game“, the first one has specific themes and the second one is a mini project. You can find all the articles using the navigation menu and there are also the videos in the channel.
Let’s go on, in a Unity project we created the four classes from the diagram in Figure 1.
By convention, class names begin with a capital letter and CamelCase is used if the class name has more than one word. For example the class “Long Range Weapon” could be written “Long RangeWeapon”, better still if we get used to using the English name “LongRangeWeapon”.
A clarification, as seen in the article: “What is a Class in Programming“, when creating a new class in Unity, it will inherit from MonoBehaviour, which will allow Unity to consider it in its internal cycle. Let’s leave this inheritance as it is, even if we haven’t made it explicit in the class diagram in Figure 1.
If we wanted to make a more accurate diagram we should place the MonoBehaviour class and make both Village and Inhabitant inherit from it.
As a clarification it is good to say that our classes are going to be MonoBehaviours, however it does not make sense to pose an extremely complete class diagram since MonoBehaviour inherits the Behaviour class, which in turn inherits the Component class, who finally inherits the Object class. It is not necessary to go that far, we must focus on what is important to solve our problem.
The Village class
To write the corresponding code in the Village class we must look at the Village Object diagram and simply know the syntax of the object-oriented language we are going to use, in this case C#.
In figure 4 we see how the diagram in figure 3.1 is implemented using the C# language in Unity.
Notice how the names, data types, and visibility indicated in the object diagram are respected.
This is the idea of practicing Classes in programming, creating classes in Unity based on an analysis of the problem and diagrams of classes and objects.
We are not going to write anything in the methods because it is something that depends on the purpose of the solution we are proposing.
The Inhabitant Class
The Worker Class
The Warrior Class
Conclusion
In this practice of Classes in Programming, we have seen how, from the point of view of a problem, we propose a structure of classes to model the behaviour of the elements.
We represent the structure of classes using a Class Diagram in which we show what is the relationship that exists between them.
The Classes will have their members, i.e. Fields and Methods, we can represent them using Object Diagrams, in which we show names and types of data for the Fields and the methods that the class will have. Also the visibility of each one.
All of the above is independent of the programming language, it is about intuitively modeling an object-oriented solution.
To implement it in Unity in language C# we will have to know the own syntax of the language, nevertheless it is extremely interesting to know that this same one could be reused for example for an Android application using language Java in Android Studio.