Classes in Programming Practice: “The Village” – Implementation in Unity

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

In the following video I explain what an object-oriented programming class is from a conceptual perspective.



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.

Fig. 1: Example of what a class diagram might look like in a project

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.

Video: What is a method in Programming?

Figure 2 shows the object diagrams of the four classes.

Fig. 2: Using these boxes we indicate the attributes and methods, public and private, of the 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.

Fig. 2.1: Object diagram for the Village class, where we see the fields and methods of the class.

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.

Fig. 2.2: Object diagram for the Inhabitant class, where we see the fields and methods of the class.

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.

Fig. 2.3: Object diagram for the Village class, where we see the fields and methods of the class.



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.

Fig. 2.4: Object diagram for the Warrior class, where we see the fields and methods of the class.

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.

Fig. 3: Create the necessary C# scripts according to the class diagram in figure 8.

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.

Fig. 3.1: Object diagram for the Village class, where we see the fields and methods of the class.

Fig. 4: We define the necessary attributes and methods, taking into account the boxes in figure 9.

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

Fig. 2.1: Object diagram for the Inhabitant class, where we see the fields and methods of the class.

Fig. 5: We define the necessary attributes and methods, taking into account the boxes in figure 9.

The Worker Class

Fig. 2.1: Object diagram for the Village class, where we see the fields and methods of the class.

Fig. 6: We define the necessary attributes and methods, taking into account the boxes in figure 9.

The Warrior Class

Fig. 2.1: Object diagram for the Warrior class, where we see the fields and methods of the class.

Fig. 7: We define the necessary attributes and methods, taking into account the boxes in figure 9.

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.

Leave a Comment

Your email address will not be published. Required fields are marked *

Exit mobile version