Variables in Programming

Introduction

In this arti¬≠cle we are going to explain what is a vari¬≠able in pro¬≠gram¬≠ming, what types of vari¬≠ables there are, what they are used for and show some exam¬≠ples of vari¬≠ables in C#, in the devel¬≠op¬≠ment of video games with Unity.

CHECK OUT THIS CRAZY EXPERIMENT

What is a Variable in programming?

Vari­ables are used in many dis­ci­plines, per­haps the most com­mon exam­ple is the "X" vari­able in math­e­mat­ics, which is gen­er­al­ly an unknown val­ue that we must find. In pro­gram­ming, vari­ables are a some­what dif­fer­ent concept.

To under­stand the essence of com­put­er vari­ables we need to know a lit­tle about com­put­er mem­o­ries.

Com­put­er mem­o­ries store infor­ma­tion in the form of bit reg­is­ters (i.e. sets of val­ues that can be 0 or 1). This infor­ma­tion will be inter­pret­ed and used by the com­put­er pro­grams. Com­put­er mem­o­ries con­tain many of these reg­is­ters, so each reg­is­ter has an address that allows it to be found, read and written.

Then we can say that a vari­able is a piece of infor­ma­tion stored in the mem­o­ry of the com­put­er. The iden­ti­fi­ca­tion name that we assign is asso­ci­at­ed with the address with­in the mem­o­ry and the val­ue we save is the infor­ma­tion con­tained in the variable.

Examples of variables in programming

The vari­ables can be of dif­fer­ent types, depend­ing on the infor­ma­tion we need to store. In fig­ure 1 we see dif­fer­ent types of vari­ables and their assigned value.

Fig. 1: Exam¬≠ples of vari¬≠ables, iden¬≠ti¬≠fi¬≠ca¬≠tion name and value.

The vari¬≠able "age" con¬≠tains the val¬≠ue 22, this means that when we use the word "age" in our pro¬≠gram, indi¬≠rect¬≠ly we will be using the val¬≠ue 22.

If we do the oper¬≠a¬≠tion "2*age" (2 mul¬≠ti¬≠plied by age) it will have a result of 44.

Fig. 2: Exam­ples of vari­ables with poor­ly cho­sen names.

Choosing Names for Variables

Fig­ure 2 shows vari­ables that have been defined with con­fus­ing names. This hap­pens very often when we want to solve things fast. It is impor­tant to choose rep­re­sen­ta­tive names of the vari­ables, because after a while we for­get what we did and when we have to make changes it is dif­fi­cult to understand.

The vari­able "house" in fig­ure 2 has the val­ue "false". This vari­able could have been used, for exam­ple, to know if a char­ac­ter is in his house and if it is true to restore his health.

When putting a con¬≠text it makes sense that "house" is false, this means that the char¬≠ac¬≠ter is not at home. How¬≠ev¬≠er, it may take us a while to fig¬≠ure out what the func¬≠tion of the vari¬≠able was.

We can save the effort by sim­ply defin­ing the vari­able as for exam­ple "char­ac­terAtH­ome". Now the name of the vari­able sug­gest us its function.

Types of Variables in Programming

Let's know the most basic vari­ables that we are going to use to solve our prob­lems. The exam­ples are tak­en from the Labyrinth Game Series (click on the link to go to the main page of the series).

Vari­ables type Boolean (bool)

The bool vari­ables are named after the Boole Alge­bra devel­oped by math­e­mati­cian George Boole. They can be found in 2 pos­si­ble states: True or False. We use them to solve log­i­cal operations.

In the image there is only text. It is the definition of the place object to find method present in the Game Control script of the video tutorial series "My first game in Unity".
Fig. 3: Frag­ment of the "Game­Con­trol" script from the My First Game on Uni­ty project.

In the method seen in fig¬≠ure 3, we define the bool "valid¬≠S¬≠e¬≠lec¬≠tion" and ini¬≠tial¬≠ize it with the false value. 

Inside the while loop you select a ran¬≠dom piece from the labyrinth and check if it is far enough from the character. 

If the labyrinth piece is very close to the char¬≠ac¬≠ter, valid¬≠S¬≠e¬≠lec¬≠tion remains false and the pro¬≠gram choos¬≠es anoth¬≠er labyrinth piece again.

If the labyrinth piece is far enough away, valid¬≠S¬≠e¬≠lec¬≠tion will become true and the pro¬≠gram will go ahead using that piece.

a 3d scene of a game in unity in which the stage is a labyrinth, the ground is grass with leaves, rock walls, in the middle there is a pedestal with a sword, the sword is embedded in the stone. you also see a countdown
Fig. 4: The object to be found in the labyrinth is a pedestal with a sword.

Inte­ger type vari­ables (int)

The "int" type vari­ables do not serve to store pos­i­tive and neg­a­tive inte­ger values.


In the image there is only text. It is the definition of the method place all the clocks present in the script Game Control of the series of video tutorials "My first game in Unity".
Fig. 5: Frag­ment of the "Game­Con­trol" script from the My First Game on Uni­ty project.

In the method shown in fig­ure 4, you see sev­er­al inte­gers, "nPieces", "labyrinthPieces.Length", "nClocks", the num­bers 0 and 1 and the iter­a­tion vari­able "i".

Let's take the case of the iter¬≠a¬≠tor vari¬≠able "i", it is declared and ini¬≠tial¬≠ized with val¬≠ue 0 inside the for loop and it is used to exe¬≠cute the "placeA¬≠Clock" method as many times as the val¬≠ue of nClocks is. 

For exam­ple if we want to have 10 clocks in the sce­nario, we make "nClocks" worth 10, then the for loop will exe­cute 10 times the "placeA­Clock" method.

a 3d scene of a game in unity in which the stage is a labyrinth, the ground is grass with leaves, rock walls, in the middle there is a gold clock and is surrounded by a sphere collider
Fig. 6: Watch¬≠es that appear in the maze and give the play¬≠er extra time.

Float Vari­ables

Float­ing point vari­ables are used to rep­re­sent real num­bers. That is to say, besides pos­i­tive and neg­a­tive inte­gers we can rep­re­sent num­bers with dec­i­mal part. This type of vari­ables can serve us for exam­ple to rep­re­sent phys­i­cal magnitudes.


In the image there is only text. It is the definition of the fixed update method present in the Lucy script of the video tutorial series "My first game in Unity".
Fig. 7: Frag¬≠ment of the "Lucy" script from the project My First Game at Unity. 

In the video of the Hal¬≠loween 2018 spe¬≠cial we defined a script for the NPC Lucy, a ghost that dwells in the labyrinth. Lucy chas¬≠es the char¬≠ac¬≠ter with some speed. 

To solve this behav­ior we make a lin­ear inter­po­la­tion between the posi­tion of the char­ac­ter and Lucy's posi­tion using as para­me­ter the float "veloc­i­ty" mul­ti­plied by the float "Time.deltaTime".

a 3d scene of a game in unity in which the scenario is a maze, the ground is brick, rock walls, a portal and in the middle you see a floating woman that gives an aura of terror to the game
Fig. 8: Lucy chas­ing the character.

String Vari­ables

Strings are not a vari­able in itself, but instances of a class, how­ev­er many devel­op­ment envi­ron­ments allow us to treat strings as prim­i­tive variables.

We use them to store text.

In the image there is only text. It is the definition of the show message method present in the UI Manager script of the video tutorial series "My first game in Unity".
Fig. 9: Frag¬≠ment of the script "UIMan¬≠ag¬≠er" from the project My first game at Unity.

The "showMes¬≠sage" method seen in fig¬≠ure 9 belongs to the script "UIMan¬≠ag¬≠er", this method receives as a para¬≠me¬≠ter a string called "mes¬≠sage" and is respon¬≠si¬≠ble for dis¬≠play¬≠ing it in the user inter¬≠face, as illus¬≠trat¬≠ed in fig¬≠ure 10.

you can see a door and 3d walls and an interface that shows time remaining and a message. Unity project
Fig. 10: Exam­ple of a mes­sage dis­played when inter­act­ing with cer­tain objects.

Conclusion

In this arti¬≠cle we saw what a vari¬≠able is in pro¬≠gram¬≠ming and how we can use it to rep¬≠re¬≠sent dif¬≠fer¬≠ent types of data.

A vari­able in pro­gram­ming is a ref­er­ence to a space in the mem­o­ry in which a data is stored. We can assign it the name we want and when using that name we will be refer­ring to that data stored in that par­tic­u­lar part of the memory.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
YouTube
Scroll to Top
Secured By miniOrange