Afrikaans
Akan
Albanian
Amharic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
1 1
[MUSIC] 2
2
In this video, we're creating a whole bunch of depth to our code by using 3
3
functions and creating our own functions to encapsulate our statements, so 4
4
that when we push up, up, up, it all works correctly. 5
5
Hit Enter, the game starts all over again. 6
6
And we can hit down, down, down, Enter. 7
7
And it's putting in the correct numbers at the correct time to create a full 8
8
game loop. 9
9
So let's jump in and get our game completed. 10
10
When we look at our start function here, you can see there's a lot going on. 11
11
Just a reminder, I'm gonna be using the words function and 12
12
method pretty much interchangeably. 13
13
For this video, I'll stick with function. 14
14
So we've just got the ones who are not bouncing around too much. 15
15
But I tend to say method a bunch as well. 16
16
We have our Start function. 17
17
There's a lot going on here. 18
18
What I'm gonna do is create another function 19
19
that allows us to encapsulate these statements, this information. 20
20
Again, we'll talk about encapsulation a bunch in this video. 21
21
So if you come down underneath your Start function, give yourself a couple of extra 22
22
spaces and type void StartGame, capital S, capital G for StartGame. 23
23
And open brackets, and it will close them automatically. 24
24
Next line, open curly braces and Enter. 25
25
We have the structure and the shape of a function. 26
26
So what are we doing here? 27
27
What are we doing when we're creating a new function? 28
28
Let me give you a little bit context.. 29
29
So here's how we write a function, here's our function definition. 30
30
Function we can think of as a contract. 31
31
It's a recipe, it's a contract, it's things that we want to have happen. 32
32
And like our variables, naming is important for 33
33
functions because it's doing a thing. 34
34
We wanna make sure we have doing words in there, descriptive words that are verbs. 35
35
So this is StartGame. 36
36
It's telling us WHAT to do. 37
37
Also, we have the () down the end is where we put parameters. 38
38
At the moment, we don't have anything inside the (), so it's doing nothing. 39
39
We're gonna talk a lot more about parameters in upcoming sections. 40
40
But for now, just know that we don't have any parameters for 41
41
our very simple definition we're creating here. 42
42
And to start, we have the word void. 43
43
And the word void could be replaced with other things. 44
44
Again, in the future, we'll talk about that a bunch more. 45
45
For now, we just need to know that void means that there's no Return value. 46
46
When I shout out StartGame, 47
47
it's not shouting anything back to me saying, you wanna know the number seven? 48
48
There's nothing at the moment that is being returned. 49
49
So that's why we put in void. 50
50
And in future content, we'll be having return values. 51
51
But for now, we don't need that. 52
52
We're just saying void, StartGame, and (). 53
53
And then underneath our void StartGame, we have our open curly braces, 54
54
all the statements that we want to contain, the ingredients of our recipe, 55
55
and then closed curly brackets. 56
56
So curly braces, technically, they're called. 57
57
Let's jump back over. 58
58
And here I don't have a big challenge slide for you in this lecture. 59
59
I've just got a couple of mini challenges. 60
60
My mini challenge here is to take all of the content from our Start function and 61
61
put it into our StartGame function. 62
62
So pause the video and transfer that information in there now. 63
63
Okay, so we can just highlight, do a Ctrl+X, or Cmd+X if you're on the Mac. 64
64
And make sure we indent and then paste in there. 65
65
So now all of this content is in StartGame. 66
66
And if we save and jump back over into Unity, start the game now, 67
67
we'll see that none of our instructions will appear on the screen. 68
68
Taking a moment to think about it, there we go. 69
69
There's nothing there. 70
70
And that's because we're not calling this function anywhere. 71
71
What calling it means is to say, I want you to do this now. 72
72
Literally, it's like, hey, start game. 73
73
And the place that we can do that is in our Start function. 74
74
We know that Start will be called cuz that's part of Unity's underlining system. 75
75
Start always gets called if we have Start in there. 76
76
We don't need to call Start. 77
77
Unity engine will call Start. 78
78
But we need to call out StartGame cuz that's a function that we've created. 79
79
So we can say very simply in here StartGame. 80
80
We type it here, we don't say it, we type it. 81
81
StartGame and then ; cuz it's a statement, so written the same way. 82
82
This is saying, I want you to do StartGame. 83
83
And what is that? 84
84
Well, we come down here, and all of these statements will be executed. 85
85
So it's exactly the same functionalities when we had them all in there. 86
86
But it tidies it up, it encapsulates it. 87
87
So now when we look at Start, what happens when we start our the game? 88
88
It does the StartGame. 89
89
It's like, yes, I know the StartGame gives the instructions. 90
90
And then if we need to go and find out the details, we can come down here and 91
91
have a look. 92
92
But we're not bombarded with them right at the very start. 93
93
So it's a really lovely way for us to organize our code, 94
94
keep everything very readable and to encapsulate. 95
95
What is encapsulate, I hear you say? 96
96
Moving code into a function is "encapsulating" it. 97
97
That's what we're doing with that process. 98
98
And think of en-capsulate-ating. 99
99
You're putting in a capsule essentially. 100
100
Or we can see it as a black box. 101
101
So we look back here, here is our black box. 102
102
And there's a bunch of stuff that happens in there. 103
103
If we're content that that's going well, we don't need to dig into the black box. 104
104
All we need to know is that we're saying, start the game. 105
105
And it enables us to "let go of the how". 106
106
So the point I've made a couple of times is all we need to know is that we have to 107
107
start the game. 108
108
And that's when all the instructions happen. 109
109
How is that happening? Well, 110
110
we either taken care of that previously or we don't need to know about it right now. 111
111
And it lets us not get swamped in all of the details at every step of our journey. 112
112
And the general process of what we're doing here as well is called refactoring. 113
113
The reason we refactor is to make our code readable, easy to read. 114
114
Code is huge in programming. 115
115
Super important, you'll see throughout this entire course. 116
116
We spend a lot of time on naming and a lot of time on having our code readable. 117
117
If you have readable code, it makes bug finding way easier, 118
118
bug solving way easier. 119
119
And it makes the whole process of creating your product, 120
120
creating your game more enjoyable. 121
121
Now that we've created one function with our encapsulating, let's create a second. 122
122
And a really good clue for 123
123
when we should look to create a function is when you see repeated code. 124
124
So this line of code is exactly the same as that line of code, 125
125
which means we could refactor that. 126
126
We could factor that out. 127
127
Let's scoot down the bottom of our script. 128
128
The second last curly bracket, hit Enter. 129
129
So we still have one curly bracket. 130
130
You can see this one relates to the one at the very top of our class up there, 131
131
that one and that one. 132
132
So we're putting it in between the last and the second to last curly bracket. 133
133
You're gonna start with void cuz, again, we have no return type. 134
134
We will call this NextGuess. 135
135
Open and closed parentheses and then open and closed curly brackets. 136
136
And here's the next challenge I have for you is within our code in our if 137
137
statements here to take the two lines of code to refactor them into NextGuess and 138
138
then to substitute where those lines of code were for 139
139
our function of NextGuess, just like we did at the top here. 140
140
So pause the video and do that refactoring, that encapsulating now. 141
141
Okay, hopefully that went well for you. 142
142
We need to take these two lines of code. 143
143
I'll just x them out, Ctrl+X, paste them down in here. 144
144
Now we have our NextGuess as a function. 145
145
I'll add NextGuess, and 146
146
you can see the autocomplete knows it should be autocompleted. 147
147
It's got a squiggly line because we need to have our open and 148
148
closed brackets to say that there's nothing, there's no parameters. 149
149
And then a ; at the end, we'll do exactly the same here. 150
150
I don't need to copy and paste this cuz we already have it. 151
151
I don't need to go and, again, put that down on the bottom. 152
152
I can just type in, if i do it correctly, Tab, Tab, 153
153
NextGuess, it autocompleted for me, () and ;. 154
154
So now we have our NextGuess();. 155
155
We have our encapsulation for StartGame();. 156
156
Let's save, hop back over into Unity, make sure this works. 157
157
Don't wanna pat ourselves on the back just yet. 158
158
Let's pat ourselves on the back in a moment if this all works. 159
159
Hit Play. 160
160
You can see the instructions are coming in. 161
161
If I hit up, it's taking me up. 162
162
Hit down, it's taking me down. 163
163
And it's giving us the instructions as we want it to. 164
164
So this process we've made our code a lot cleaner. 165
165
And we're developing a really good habit for 166
166
how we can make beautiful code moving forward when our projects get gigantic and 167
167
huge, which I know yours will as you make bigger and huger more awesome games. 168
168
Last thing we wanna do in this lecture, I'll push through this nice and quickly, 169
169
so bear with us here, is to see what's going on when we try to restart our game. 170
170
So let's say up, up, up, and then Enter, you got it correct, and then that's it. 171
171
Up and down, it's still trying to guess from the same place. 172
172
What we need to do is a question for you in terms of our challenge, 173
173
what do we need to do in here? 174
174
What would we do to have our game start again, 175
175
when you say I have won the game, when you say you've got it correct? 176
176
What would we need to call to start the game again? 177
177
Okay, if you guessed StartGame, then you would be correct. 178
178
So I'm gonna trot all the way down the bottom here to my else if for 179
179
did you get it correct where we have our key code return, and type in StartGame();. 180
180
And then save, jump back over into Unity, click on Play. 181
181
And then up, up, up, Enter. 182
182
And then we get all our instructions over again. 183
183
It's a lot to look at in here. 184
184
The console's not really made for this kinda stuff. 185
185
It's so that we can practice our C# without having to worry about all 186
186
the Unity interface just yet. 187
187
But we can see that that is successful that our instructions are being reprinted. 188
188
But tell me if your number is higher and lower than 938 and 189
189
the highest number you can pick is 1,001, that doesn't make sense. 190
190
So success on getting instructions in there, 191
191
but not success in terms of having our numbers correct. 192
192
Let's jump back into our script and see what's going on here. 193
193
If we look at the logic of what's happening here, when we start our Start 194
194
function and say StartGame, we're going through all of our intro text. 195
195
And we're saying print out the max. 196
196
And the max, when we very first declare them and initialize them, is 1000. 197
197
But throughout our game, we're changing what the max is. 198
198
So in here, we're saying, the max = the guess, if we've pushed up a few times. 199
199
And then when we say, StartGame again, it will say, okay, cool. 200
200
I'll start the game, and it's remembering our max level. 201
201
So then, within StartGame, 202
202
we need to be putting our values back to where we want them in the original sense. 203
203
So let's give ourselves a little bit of space at 204
204
the top of StartGame and say, max = 1000, 205
205
min = 1, and guess, you guessed it = 500. 206
206
And just a quick note when we're defining our variables in here, 207
207
we don't need to say int again. 208
208
We've already declared, it already knows that this is an integer, 209
209
we've already told it that up here at our member variables. 210
210
So all we need to do down here in our local variables is just to say, well, max, 211
211
which we already know, is of type int, should be 1000. 212
212
We could remove this up here and say, well, 213
213
we don't actually need to have any values at the start. 214
214
Because we will be defining them straight away in StartGame or hit Save, 215
215
jump back over into our build, click on Play. 216
216
And just make sure you have collapse turned off or 217
217
else everything gets bunched up together. 218
218
It's hard to see what's going on. 219
219
We'll hit up, up, up, Enter. 220
220
And it starts everything again. 221
221
1001, 500 great, down, down, down, Enter 1150, fantastic. 222
222
So we've created a full game loop here where our game starts, 223
223
gives us the correct numbers, we can replay it again. 224
224
We've talked about having our variables within our 225
225
newly created StartGame function. 226
226
We're calling our StartGame function from Start. 227
227
We've created a new function for NextGuess. 228
228
That's allowed us to encapsulate this information, these statements down here. 229
229
Makes our else statements, our if and our else statements cleaner. 230
230
So now we're just saying NextGuess and StartGame. 231
231
And our whole loop is complete, so well done. 232
232
Huge congratulations on working through this section. 233
233
Give yourself a huge high five and a pat of the back. 234
234
Cuz you have created something that is very excellent. 235
235
You've created working code and a working game mechanic. 236
236
Great work! 237
237
I'll see you in an upcoming video.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.