Classes in Programming Practice: "The Village" — Implementation in Unity

Introduction

This arti­cle will func­tion as the prac­ti­cal part of the pre­vi­ous arti­cle on Class­es in Pro­gram­ming. We are going to solve an exer­cise on class struc­ture and imple­ment it in C# in Unity.

If you don't know what a class is in pro­gram­ming it would be good if you saw the arti­cle I wrote on the sub­ject before continuing.

Go to the theoretical part of this article

MOST SEARCHED VIDEOS FROM MY CHANNEL

ABOUT UNITY

ABOUT BLENDER

Exercise Approach

Let's see an exam­ple that occurred to me at the time of writ­ing this arti­cle. It's about mod­el­ing the behav­ior of a vil­lage that has inhab­i­tants and these inhab­i­tants can be work­ers or war­riors.

I have high­light­ed what I con­sid­er to be key words in rais­ing the prob­lem, in prin­ci­ple it seems that at least we are going to need two class­es to solve it, one for the vil­lage and one for the vil­lagers, but we are going to con­sid­er the fact that both work­ers and war­riors them­selves are villagers.

Work­ers and War­riors will have com­mon char­ac­ter­is­tics and behav­iors that will be defined in the Inhab­i­tant class and then the Work­er and War­rior class­es will inher­it the char­ac­ter­is­tics and behav­iors of Inhabitant.

A vil­lage has inhab­i­tants so the Vil­lage class will have among its data one or more instances of the Vil­lagers class. 

I would like to men­tion that know­ing the dif­fer­ence between class­es and instance of class­es is very impor­tant, but this arti­cle is not about that, just as it is not about inheritance. 

At the moment the idea is that we are going to use the Inhab­i­tant class to cre­ate all the inhab­i­tant objects that we need, these objects will have their own val­ues (for exam­ple dif­fer­ent names or age) but they will respect the struc­ture defined in the class.

Class Diagram

Then we said that we are going to use four class­es, Vil­lage, Vil­lager, Work­er and War­rior. The Vil­lage class has among its data one or more instances of the Inhab­i­tant class. Work­er and Guer­rero are a spe­cif­ic type of Inhab­i­tant so inher­i­tance will be used.

The class dia­gram in Fig­ure 1 is based on the pre­vi­ous paragraph.

simple class diagram to exemplify the relationships between classes
Fig. 1: Exam­ple of what a class dia­gram might look like in a project

In the dia­gram we observe a "1..*" above Inhab­i­tant, this means that Vil­lage will have one or more Inhab­i­tant objects.

In addi­tion, we observe that the inher­i­tance of class­es is rep­re­sent­ed by an upward arrow toward the par­ent class. 

Object Diagram

Once the class­es that will be present have been defined, we think about their pos­si­ble fields and meth­ods. In this part it is good to reflect on the nature of the object we want to mod­el, what its char­ac­ter­is­tics are, what infor­ma­tion it han­dles and what its behav­iour may be.

I'm going to make up some fields and meth­ods for the class­es because this prac­tice of pro­gram­ming class­es is more about the approach of class struc­ture and its lat­er imple­men­ta­tion in Uni­ty in C# lan­guage, but with­out going into details of what is going to be done with this.

Fig­ure 2 shows the object dia­grams of the four classes. 

simple example of uml class diagram
Fig. 2: Using these box­es we indi­cate the attrib­ut­es and meth­ods, pub­lic and pri­vate, of the classes.

Now I am going to explain in detail how the box­es in fig­ure 2 are interpreted.

The Village Object

For this object I con­sid­ered that every vil­lage has a name and a group of inhab­i­tants, so for its fields I am going to use a col­lec­tion of objects called "inhab­i­tants", a string called "name" and an inte­ger called "pop­u­la­tion". I place these fields at the top of the box as shown in fig­ure 2.1.

Some meth­ods of the object Vil­lage can be "NewIn­hab­i­tant" which will be exe­cut­ed every time an inhab­i­tant is born. "Get­Name" and "Get­Pop­u­la­tion" will return the cor­re­spond­ing data (they are known as "Get­ters" and are use­ful for exam­ple to make sta­tis­tics of all the vil­lages we may have). 

The "Inhab­i­tant­Die" method can mod­i­fy the pop­u­la­tion and remove the dead inhab­i­tant from the col­lec­tion of inhabitants. 

example of object diagram for a class called village, fields and methods of the class are shown
Fig. 2.1: Object dia­gram for the Vil­lage class, where we see the fields and meth­ods of the class.

Final­ly, the "Feed Inhab­i­tants" method, which can go through the col­lec­tion of inhab­i­tants exe­cut­ing appro­pri­ate meth­ods in each of them, or check the food reserve and sub­tract as many units as there are populations.

El objeto Habitante

Inhab­i­tant will be the par­ent class (or super­class) of Work­er and War­rior, in this class will be defined the behav­ior and data that have in com­mon Work­ers and Warriors.

Every inhab­i­tant has a name rep­re­sent­ed by a String, an inte­ger for age, anoth­er for health and anoth­er for defense.

example of an object diagram for a class called inhabitant, fields and methods of the class are shown
Fig. 2.2: Object dia­gram for the Inhab­i­tant class, where we see the fields and meth­ods of the class. 

Among the meth­ods of Inhab­i­tant we have the "Get­ters" to obtain name and age, a pri­vate method to age and a pub­lic method for the inhab­i­tant to move to a position.

The Worker Object

The Work­er Object is a more spe­cif­ic case of the Inhab­i­tant class, between its fields we have a string that indi­cates the name of the work and a boolean vari­able to know if it is work­ing or not.

Work­er meth­ods are relat­ed to what your activ­i­ties could be, for exam­ple Col­lect, Repair, Drill, Fish and also a pri­vate method for you to deliv­er the resources it has obtained. This last method can be exe­cut­ed inter­nal­ly by Work­er for exam­ple when it is at the lim­it of its col­lec­tion capacity.

example of object diagram for a class called villager, fields and methods of the class are shown
Fig. 2.3: Object dia­gram for the Vil­lage class, where we see the fields and meth­ods of the class. 

The Warrior Object

The War­rior class is a more spe­cif­ic case of the Inhab­it­ing class.

A War­rior may or may not be attack­ing for which we use the Boolean vari­able "attacked", has assigned a weapon rep­re­sent­ed by a String and has cer­tain attack points rep­re­sent­ed by an entire variable.

Among its meth­ods are Attack­ing, For­ma­tion, Defend­ing, and a pri­vate method to deliv­er the booty he got after a suc­cess­ful battle. 

example of object diagram for a class called warrior, fields and methods of the class are shown
Fig. 2.4: Object dia­gram for the War­rior class, where we see the fields and meth­ods of the class. 

Implementation in Unity

Now we move on to the good part, let's take the class dia­gram and the object dia­gram from fig­ures 1 and 2 and use them as a ref­er­ence for cre­at­ing class­es in Unity.

Before you start you might need to know how to use Uni­ty, here on the page I have a cou­ple of series, one called "Uni­ty Fun­da­men­tal Series" and the oth­er "Labyrinth Game", the first one has spe­cif­ic themes and the sec­ond one is a mini project. You can find all the arti­cles using the nav­i­ga­tion menu and there are also the videos in the channel.

Let's go on, in a Uni­ty project we cre­at­ed the four class­es from the dia­gram in Fig­ure 1. 

c sharp scripts to implement a simple class diagram
Fig. 3: Cre­ate the nec­es­sary C# scripts accord­ing to the class dia­gram in fig­ure 8.

By con­ven­tion, class names begin with a cap­i­tal let­ter and Camel­Case is used if the class name has more than one word. For exam­ple the class "Long Range Weapon" could be writ­ten "Long RangeWeapon", bet­ter still if we get used to using the Eng­lish name "Lon­gRangeWeapon".

A clar­i­fi­ca­tion, as seen in the arti­cle: "What is a Class in Pro­gram­ming", when cre­at­ing a new class in Uni­ty, it will inher­it from MonoBe­hav­iour, which will allow Uni­ty to con­sid­er it in its inter­nal cycle. Let's leave this inher­i­tance as it is, even if we haven't made it explic­it in the class dia­gram in Fig­ure 1. 

If we want­ed to make a more accu­rate dia­gram we should place the MonoBe­hav­iour class and make both Vil­lage and Inhab­i­tant inher­it from it. 

As a clar­i­fi­ca­tion it is good to say that our class­es are going to be MonoBe­hav­iours, how­ev­er it does not make sense to pose an extreme­ly com­plete class dia­gram since MonoBe­hav­iour inher­its the Behav­iour class, which in turn inher­its the Com­po­nent class, who final­ly inher­its the Object class. It is not nec­es­sary to go that far, we must focus on what is impor­tant to solve our problem.

The Village class

To write the cor­re­spond­ing code in the Vil­lage class we must look at the Vil­lage Object dia­gram and sim­ply know the syn­tax of the object-ori­ent­ed lan­guage we are going to use, in this case C#.

In fig­ure 4 we see how the dia­gram in fig­ure 3.1 is imple­ment­ed using the C# lan­guage in Unity.

Notice how the names, data types, and vis­i­bil­i­ty indi­cat­ed in the object dia­gram are respected. 

example of object diagram for a class called village, fields and methods of the class are shown
Fig. 3.1: Object dia­gram for the Vil­lage class, where we see the fields and meth­ods of the class.

practical implementation of class programming in sharp c sharp according to uml diagram, unity
Fig. 4: We define the nec­es­sary attrib­ut­es and meth­ods, tak­ing into account the box­es in fig­ure 9.

This is the idea of prac­tic­ing Class­es in pro­gram­ming, cre­at­ing class­es in Uni­ty based on an analy­sis of the prob­lem and dia­grams of class­es and objects. 

We are not going to write any­thing in the meth­ods because it is some­thing that depends on the pur­pose of the solu­tion we are proposing.

The Inhabitant Class

example of an object diagram for a class called inhabitant, fields and methods of the class are shown
Fig. 2.1: Object dia­gram for the Inhab­i­tant class, where we see the fields and meth­ods of the class.

practical implementation of class programming in sharp c sharp according to uml diagram, unity
Fig. 5: We define the nec­es­sary attrib­ut­es and meth­ods, tak­ing into account the box­es in fig­ure 9.

The Worker Class

example of object diagram for a class called villager, fields and methods of the class are shown
Fig. 2.1: Object dia­gram for the Vil­lage class, where we see the fields and meth­ods of the class.

practical implementation of class programming in sharp c sharp according to uml diagram, unity
Fig. 6: We define the nec­es­sary attrib­ut­es and meth­ods, tak­ing into account the box­es in fig­ure 9.

The Warrior Class

example of object diagram for a class called warrior, fields and methods of the class are shown
Fig. 2.1: Object dia­gram for the War­rior class, where we see the fields and meth­ods of the class.

implementation of class in c sharp according to uml diagram, unity
Fig. 7: We define the nec­es­sary attrib­ut­es and meth­ods, tak­ing into account the box­es in fig­ure 9.

Conclusion

In this prac­tice of Class­es in Pro­gram­ming, we have seen how, from the point of view of a prob­lem, we pro­pose a struc­ture of class­es to mod­el the behav­iour of the elements. 

We rep­re­sent the struc­ture of class­es using a Class Dia­gram in which we show what is the rela­tion­ship that exists between them.

The Class­es will have their mem­bers, i.e. Fields and Meth­ods, we can rep­re­sent them using Object Dia­grams, in which we show names and types of data for the Fields and the meth­ods that the class will have. Also the vis­i­bil­i­ty of each one.

All of the above is inde­pen­dent of the pro­gram­ming lan­guage, it is about intu­itive­ly mod­el­ing an object-ori­ent­ed solution. 

To imple­ment it in Uni­ty in lan­guage C# we will have to know the own syn­tax of the lan­guage, nev­er­the­less it is extreme­ly inter­est­ing to know that this same one could be reused for exam­ple for an Android appli­ca­tion using lan­guage Java in Android Studio.

Scroll to Top
Secured By miniOrange