Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
1
00:00:00,000 --> 00:00:04,105
[MUSIC]
2
2
00:00:04,105 --> 00:00:08,052
In this video, we're creating a whole
bunch of depth to our code by using
3
3
00:00:08,052 --> 00:00:12,260
functions and creating our own functions
to encapsulate our statements, so
4
4
00:00:12,260 --> 00:00:15,320
that when we push up, up,
up, it all works correctly.
5
5
00:00:15,320 --> 00:00:17,353
Hit Enter, the game starts all over again.
6
6
00:00:17,353 --> 00:00:19,169
And we can hit down, down, down, Enter.
7
7
00:00:19,169 --> 00:00:23,182
And it's putting in the correct numbers
at the correct time to create a full
8
8
00:00:23,182 --> 00:00:24,050
game loop.
9
9
00:00:24,050 --> 00:00:27,800
So let's jump in and
get our game completed.
10
10
00:00:27,800 --> 00:00:30,890
When we look at our start function here,
you can see there's a lot going on.
11
11
00:00:30,890 --> 00:00:33,220
Just a reminder,
I'm gonna be using the words function and
12
12
00:00:33,220 --> 00:00:35,160
method pretty much interchangeably.
13
13
00:00:35,160 --> 00:00:36,599
For this video, I'll stick with function.
14
14
00:00:36,599 --> 00:00:39,392
So we've just got the ones who
are not bouncing around too much.
15
15
00:00:39,392 --> 00:00:41,764
But I tend to say method a bunch as well.
16
16
00:00:41,764 --> 00:00:43,220
We have our Start function.
17
17
00:00:43,220 --> 00:00:45,080
There's a lot going on here.
18
18
00:00:45,080 --> 00:00:47,760
What I'm gonna do is
create another function
19
19
00:00:47,760 --> 00:00:52,205
that allows us to encapsulate these
statements, this information.
20
20
00:00:52,205 --> 00:00:54,690
Again, we'll talk about
encapsulation a bunch in this video.
21
21
00:00:54,690 --> 00:00:57,630
So if you come down underneath your Start
function, give yourself a couple of extra
22
22
00:00:57,630 --> 00:01:04,500
spaces and type void StartGame,
capital S, capital G for StartGame.
23
23
00:01:04,500 --> 00:01:08,090
And open brackets, and
it will close them automatically.
24
24
00:01:08,090 --> 00:01:11,850
Next line, open curly braces and Enter.
25
25
00:01:11,850 --> 00:01:15,050
We have the structure and
the shape of a function.
26
26
00:01:15,050 --> 00:01:15,800
So what are we doing here?
27
27
00:01:15,800 --> 00:01:18,195
What are we doing when we're
creating a new function?
28
28
00:01:18,195 --> 00:01:19,403
Let me give you a little bit context..
29
29
00:01:19,403 --> 00:01:23,189
So here's how we write a function,
here's our function definition.
30
30
00:01:23,189 --> 00:01:25,412
Function we can think of as a contract.
31
31
00:01:25,412 --> 00:01:29,549
It's a recipe, it's a contract,
it's things that we want to have happen.
32
32
00:01:29,549 --> 00:01:31,970
And like our variables,
naming is important for
33
33
00:01:31,970 --> 00:01:33,986
functions because it's doing a thing.
34
34
00:01:33,986 --> 00:01:38,253
We wanna make sure we have doing words in
there, descriptive words that are verbs.
35
35
00:01:38,253 --> 00:01:40,470
So this is StartGame.
36
36
00:01:40,470 --> 00:01:42,450
It's telling us WHAT to do.
37
37
00:01:42,450 --> 00:01:46,570
Also, we have the () down the end
is where we put parameters.
38
38
00:01:46,570 --> 00:01:49,853
At the moment, we don't have anything
inside the (), so it's doing nothing.
39
39
00:01:49,853 --> 00:01:52,261
We're gonna talk a lot more about
parameters in upcoming sections.
40
40
00:01:52,261 --> 00:01:55,627
But for now, just know that we
don't have any parameters for
41
41
00:01:55,627 --> 00:01:58,474
our very simple definition
we're creating here.
42
42
00:01:58,474 --> 00:02:00,933
And to start, we have the word void.
43
43
00:02:00,933 --> 00:02:03,361
And the word void could be
replaced with other things.
44
44
00:02:03,361 --> 00:02:05,673
Again, in the future,
we'll talk about that a bunch more.
45
45
00:02:05,673 --> 00:02:09,570
For now, we just need to know that void
means that there's no Return value.
46
46
00:02:09,570 --> 00:02:11,830
When I shout out StartGame,
47
47
00:02:11,830 --> 00:02:15,740
it's not shouting anything back to me
saying, you wanna know the number seven?
48
48
00:02:15,740 --> 00:02:18,730
There's nothing at the moment
that is being returned.
49
49
00:02:18,730 --> 00:02:20,670
So that's why we put in void.
50
50
00:02:20,670 --> 00:02:22,870
And in future content,
we'll be having return values.
51
51
00:02:22,870 --> 00:02:23,860
But for now, we don't need that.
52
52
00:02:23,860 --> 00:02:26,354
We're just saying void, StartGame, and ().
53
53
00:02:26,354 --> 00:02:30,086
And then underneath our void StartGame,
we have our open curly braces,
54
54
00:02:30,086 --> 00:02:34,001
all the statements that we want to
contain, the ingredients of our recipe,
55
55
00:02:34,001 --> 00:02:35,736
and then closed curly brackets.
56
56
00:02:35,736 --> 00:02:38,812
So curly braces,
technically, they're called.
57
57
00:02:38,812 --> 00:02:39,501
Let's jump back over.
58
58
00:02:39,501 --> 00:02:42,270
And here I don't have a big challenge
slide for you in this lecture.
59
59
00:02:42,270 --> 00:02:43,882
I've just got a couple of mini challenges.
60
60
00:02:43,882 --> 00:02:49,251
My mini challenge here is to take all of
the content from our Start function and
61
61
00:02:49,251 --> 00:02:51,823
put it into our StartGame function.
62
62
00:02:51,823 --> 00:02:57,248
So pause the video and
transfer that information in there now.
63
63
00:02:57,248 --> 00:03:03,117
Okay, so we can just highlight, do
a Ctrl+X, or Cmd+X if you're on the Mac.
64
64
00:03:03,117 --> 00:03:05,940
And make sure we indent and
then paste in there.
65
65
00:03:05,940 --> 00:03:08,384
So now all of this
content is in StartGame.
66
66
00:03:08,384 --> 00:03:12,082
And if we save and jump back over
into Unity, start the game now,
67
67
00:03:12,082 --> 00:03:16,002
we'll see that none of our instructions
will appear on the screen.
68
68
00:03:16,002 --> 00:03:17,878
Taking a moment to think about it,
there we go.
69
69
00:03:17,878 --> 00:03:18,885
There's nothing there.
70
70
00:03:18,885 --> 00:03:25,328
And that's because we're not
calling this function anywhere.
71
71
00:03:25,328 --> 00:03:28,351
What calling it means is to say,
I want you to do this now.
72
72
00:03:28,351 --> 00:03:30,648
Literally, it's like, hey, start game.
73
73
00:03:30,648 --> 00:03:33,763
And the place that we can do
that is in our Start function.
74
74
00:03:33,763 --> 00:03:38,187
We know that Start will be called cuz
that's part of Unity's underlining system.
75
75
00:03:38,187 --> 00:03:40,470
Start always gets called
if we have Start in there.
76
76
00:03:40,470 --> 00:03:42,064
We don't need to call Start.
77
77
00:03:42,064 --> 00:03:44,226
Unity engine will call Start.
78
78
00:03:44,226 --> 00:03:47,709
But we need to call out StartGame cuz
that's a function that we've created.
79
79
00:03:47,709 --> 00:03:51,590
So we can say very simply
in here StartGame.
80
80
00:03:51,590 --> 00:03:53,780
We type it here,
we don't say it, we type it.
81
81
00:03:53,780 --> 00:03:58,870
StartGame and then ; cuz it's a statement,
so written the same way.
82
82
00:03:58,870 --> 00:04:02,370
This is saying,
I want you to do StartGame.
83
83
00:04:02,370 --> 00:04:02,970
And what is that?
84
84
00:04:02,970 --> 00:04:06,100
Well, we come down here, and
all of these statements will be executed.
85
85
00:04:06,100 --> 00:04:08,936
So it's exactly the same functionalities
when we had them all in there.
86
86
00:04:08,936 --> 00:04:11,107
But it tidies it up, it encapsulates it.
87
87
00:04:11,107 --> 00:04:14,519
So now when we look at Start,
what happens when we start our the game?
88
88
00:04:14,519 --> 00:04:15,526
It does the StartGame.
89
89
00:04:15,526 --> 00:04:18,116
It's like, yes, I know the StartGame
gives the instructions.
90
90
00:04:18,116 --> 00:04:21,227
And then if we need to go and find out
the details, we can come down here and
91
91
00:04:21,227 --> 00:04:21,789
have a look.
92
92
00:04:21,789 --> 00:04:24,424
But we're not bombarded with
them right at the very start.
93
93
00:04:24,424 --> 00:04:27,923
So it's a really lovely way for
us to organize our code,
94
94
00:04:27,923 --> 00:04:31,210
keep everything very readable and
to encapsulate.
95
95
00:04:31,210 --> 00:04:33,130
What is encapsulate, I hear you say?
96
96
00:04:33,130 --> 00:04:35,820
Moving code into a function
is "encapsulating" it.
97
97
00:04:35,820 --> 00:04:37,460
That's what we're doing with that process.
98
98
00:04:37,460 --> 00:04:39,850
And think of en-capsulate-ating.
99
99
00:04:39,850 --> 00:04:42,283
You're putting in a capsule essentially.
100
100
00:04:42,283 --> 00:04:44,096
Or we can see it as a black box.
101
101
00:04:44,096 --> 00:04:46,766
So we look back here,
here is our black box.
102
102
00:04:46,766 --> 00:04:48,616
And there's a bunch of stuff
that happens in there.
103
103
00:04:48,616 --> 00:04:51,816
If we're content that that's going well,
we don't need to dig into the black box.
104
104
00:04:51,816 --> 00:04:56,660
All we need to know is that we're saying,
start the game.
105
105
00:04:56,660 --> 00:04:58,433
And it enables us to "let go of the how".
106
106
00:04:58,433 --> 00:05:02,046
So the point I've made a couple of times
is all we need to know is that we have to
107
107
00:05:02,046 --> 00:05:02,844
start the game.
108
108
00:05:02,844 --> 00:05:04,840
And that's when all
the instructions happen.
109
109
00:05:04,840 --> 00:05:05,749
How is that happening?
Well,
110
110
00:05:05,749 --> 00:05:10,861
we either taken care of that previously or
we don't need to know about it right now.
111
111
00:05:10,861 --> 00:05:15,800
And it lets us not get swamped in all of
the details at every step of our journey.
112
112
00:05:15,800 --> 00:05:19,141
And the general process of what we're
doing here as well is called refactoring.
113
113
00:05:19,141 --> 00:05:23,762
The reason we refactor is to make
our code readable, easy to read.
114
114
00:05:23,762 --> 00:05:25,363
Code is huge in programming.
115
115
00:05:25,363 --> 00:05:27,944
Super important,
you'll see throughout this entire course.
116
116
00:05:27,944 --> 00:05:31,754
We spend a lot of time on naming and
a lot of time on having our code readable.
117
117
00:05:31,754 --> 00:05:35,614
If you have readable code,
it makes bug finding way easier,
118
118
00:05:35,614 --> 00:05:37,212
bug solving way easier.
119
119
00:05:37,212 --> 00:05:39,974
And it makes the whole process
of creating your product,
120
120
00:05:39,974 --> 00:05:41,778
creating your game more enjoyable.
121
121
00:05:41,778 --> 00:05:45,630
Now that we've created one function with
our encapsulating, let's create a second.
122
122
00:05:45,630 --> 00:05:47,037
And a really good clue for
123
123
00:05:47,037 --> 00:05:51,001
when we should look to create a function
is when you see repeated code.
124
124
00:05:51,001 --> 00:05:54,226
So this line of code is exactly
the same as that line of code,
125
125
00:05:54,226 --> 00:05:56,236
which means we could refactor that.
126
126
00:05:56,236 --> 00:05:58,075
We could factor that out.
127
127
00:05:58,075 --> 00:06:00,270
Let's scoot down the bottom of our script.
128
128
00:06:00,270 --> 00:06:03,506
The second last curly bracket, hit Enter.
129
129
00:06:03,506 --> 00:06:05,344
So we still have one curly bracket.
130
130
00:06:05,344 --> 00:06:08,648
You can see this one relates to the one
at the very top of our class up there,
131
131
00:06:08,648 --> 00:06:09,675
that one and that one.
132
132
00:06:09,675 --> 00:06:12,611
So we're putting it in between the last
and the second to last curly bracket.
133
133
00:06:12,611 --> 00:06:16,020
You're gonna start with void cuz,
again, we have no return type.
134
134
00:06:16,020 --> 00:06:19,510
We will call this NextGuess.
135
135
00:06:19,510 --> 00:06:24,490
Open and closed parentheses and
then open and closed curly brackets.
136
136
00:06:24,490 --> 00:06:27,630
And here's the next challenge I have for
you is within our code in our if
137
137
00:06:27,630 --> 00:06:33,540
statements here to take the two lines of
code to refactor them into NextGuess and
138
138
00:06:33,540 --> 00:06:37,670
then to substitute where
those lines of code were for
139
139
00:06:37,670 --> 00:06:41,390
our function of NextGuess,
just like we did at the top here.
140
140
00:06:41,390 --> 00:06:45,300
So pause the video and do that
refactoring, that encapsulating now.
141
141
00:06:47,700 --> 00:06:50,140
Okay, hopefully that went well for you.
142
142
00:06:50,140 --> 00:06:52,640
We need to take these two lines of code.
143
143
00:06:52,640 --> 00:06:55,730
I'll just x them out, Ctrl+X,
paste them down in here.
144
144
00:06:55,730 --> 00:06:58,430
Now we have our NextGuess as a function.
145
145
00:06:58,430 --> 00:07:00,650
I'll add NextGuess, and
146
146
00:07:00,650 --> 00:07:03,500
you can see the autocomplete
knows it should be autocompleted.
147
147
00:07:03,500 --> 00:07:06,587
It's got a squiggly line because
we need to have our open and
148
148
00:07:06,587 --> 00:07:10,830
closed brackets to say that there's
nothing, there's no parameters.
149
149
00:07:10,830 --> 00:07:13,726
And then a ; at the end,
we'll do exactly the same here.
150
150
00:07:13,726 --> 00:07:15,897
I don't need to copy and
paste this cuz we already have it.
151
151
00:07:15,897 --> 00:07:18,061
I don't need to go and, again,
put that down on the bottom.
152
152
00:07:18,061 --> 00:07:22,928
I can just type in,
if i do it correctly, Tab, Tab,
153
153
00:07:22,928 --> 00:07:27,816
NextGuess, it autocompleted for
me, () and ;.
154
154
00:07:27,816 --> 00:07:29,766
So now we have our NextGuess();.
155
155
00:07:29,766 --> 00:07:32,997
We have our encapsulation for
StartGame();.
156
156
00:07:32,997 --> 00:07:36,830
Let's save, hop back over into Unity,
make sure this works.
157
157
00:07:36,830 --> 00:07:39,130
Don't wanna pat ourselves
on the back just yet.
158
158
00:07:39,130 --> 00:07:42,120
Let's pat ourselves on the back
in a moment if this all works.
159
159
00:07:42,120 --> 00:07:42,709
Hit Play.
160
160
00:07:42,709 --> 00:07:44,296
You can see the instructions
are coming in.
161
161
00:07:44,296 --> 00:07:46,452
If I hit up, it's taking me up.
162
162
00:07:46,452 --> 00:07:47,916
Hit down, it's taking me down.
163
163
00:07:47,916 --> 00:07:50,118
And it's giving us
the instructions as we want it to.
164
164
00:07:50,118 --> 00:07:53,180
So this process we've made
our code a lot cleaner.
165
165
00:07:53,180 --> 00:07:55,527
And we're developing
a really good habit for
166
166
00:07:55,527 --> 00:07:59,664
how we can make beautiful code moving
forward when our projects get gigantic and
167
167
00:07:59,664 --> 00:08:04,030
huge, which I know yours will as you make
bigger and huger more awesome games.
168
168
00:08:04,030 --> 00:08:07,090
Last thing we wanna do in this lecture,
I'll push through this nice and quickly,
169
169
00:08:07,090 --> 00:08:12,670
so bear with us here, is to see what's
going on when we try to restart our game.
170
170
00:08:12,670 --> 00:08:16,980
So let's say up, up, up, and then Enter,
you got it correct, and then that's it.
171
171
00:08:16,980 --> 00:08:19,574
Up and down, it's still trying
to guess from the same place.
172
172
00:08:19,574 --> 00:08:23,853
What we need to do is a question for
you in terms of our challenge,
173
173
00:08:23,853 --> 00:08:25,769
what do we need to do in here?
174
174
00:08:25,769 --> 00:08:29,596
What would we do to have
our game start again,
175
175
00:08:29,596 --> 00:08:35,490
when you say I have won the game,
when you say you've got it correct?
176
176
00:08:35,490 --> 00:08:38,680
What would we need to call
to start the game again?
177
177
00:08:39,990 --> 00:08:42,916
Okay, if you guessed StartGame,
then you would be correct.
178
178
00:08:42,916 --> 00:08:48,347
So I'm gonna trot all the way down
the bottom here to my else if for
179
179
00:08:48,347 --> 00:08:55,703
did you get it correct where we have our
key code return, and type in StartGame();.
180
180
00:08:55,703 --> 00:09:02,655
And then save, jump back over into Unity,
click on Play.
181
181
00:09:02,655 --> 00:09:04,744
And then up, up, up, Enter.
182
182
00:09:04,744 --> 00:09:06,529
And then we get all our
instructions over again.
183
183
00:09:06,529 --> 00:09:07,698
It's a lot to look at in here.
184
184
00:09:07,698 --> 00:09:09,758
The console's not really made for
this kinda stuff.
185
185
00:09:09,758 --> 00:09:12,735
It's so that we can practice our C#
without having to worry about all
186
186
00:09:12,735 --> 00:09:14,080
the Unity interface just yet.
187
187
00:09:14,080 --> 00:09:18,140
But we can see that that is successful
that our instructions are being reprinted.
188
188
00:09:18,140 --> 00:09:21,930
But tell me if your number is higher and
lower than 938 and
189
189
00:09:21,930 --> 00:09:25,860
the highest number you can pick is 1,001,
that doesn't make sense.
190
190
00:09:25,860 --> 00:09:27,780
So success on getting
instructions in there,
191
191
00:09:27,780 --> 00:09:31,320
but not success in terms of
having our numbers correct.
192
192
00:09:31,320 --> 00:09:34,335
Let's jump back into our script and
see what's going on here.
193
193
00:09:34,335 --> 00:09:38,740
If we look at the logic of what's
happening here, when we start our Start
194
194
00:09:38,740 --> 00:09:43,238
function and say StartGame,
we're going through all of our intro text.
195
195
00:09:43,238 --> 00:09:45,728
And we're saying print out the max.
196
196
00:09:45,728 --> 00:09:50,377
And the max, when we very first declare
them and initialize them, is 1000.
197
197
00:09:50,377 --> 00:09:53,190
But throughout our game,
we're changing what the max is.
198
198
00:09:53,190 --> 00:09:57,303
So in here, we're saying, the max =
the guess, if we've pushed up a few times.
199
199
00:09:57,303 --> 00:10:01,512
And then when we say, StartGame again,
it will say, okay, cool.
200
200
00:10:01,512 --> 00:10:04,920
I'll start the game, and
it's remembering our max level.
201
201
00:10:04,920 --> 00:10:06,660
So then, within StartGame,
202
202
00:10:06,660 --> 00:10:10,509
we need to be putting our values back to
where we want them in the original sense.
203
203
00:10:12,220 --> 00:10:17,346
So let's give ourselves
a little bit of space at
204
204
00:10:17,346 --> 00:10:22,470
the top of StartGame and say, max = 1000,
205
205
00:10:22,470 --> 00:10:28,560
min = 1, and guess, you guessed it = 500.
206
206
00:10:28,560 --> 00:10:30,880
And just a quick note when we're
defining our variables in here,
207
207
00:10:30,880 --> 00:10:32,330
we don't need to say int again.
208
208
00:10:32,330 --> 00:10:34,970
We've already declared,
it already knows that this is an integer,
209
209
00:10:34,970 --> 00:10:39,600
we've already told it that up
here at our member variables.
210
210
00:10:39,600 --> 00:10:44,125
So all we need to do down here in our
local variables is just to say, well, max,
211
211
00:10:44,125 --> 00:10:47,639
which we already know,
is of type int, should be 1000.
212
212
00:10:47,639 --> 00:10:50,712
We could remove this up here and
say, well,
213
213
00:10:50,712 --> 00:10:54,629
we don't actually need to
have any values at the start.
214
214
00:10:54,629 --> 00:10:59,125
Because we will be defining them
straight away in StartGame or hit Save,
215
215
00:10:59,125 --> 00:11:02,010
jump back over into our build,
click on Play.
216
216
00:11:03,280 --> 00:11:05,360
And just make sure you have
collapse turned off or
217
217
00:11:05,360 --> 00:11:06,870
else everything gets bunched up together.
218
218
00:11:06,870 --> 00:11:07,824
It's hard to see what's going on.
219
219
00:11:07,824 --> 00:11:09,363
We'll hit up, up, up, Enter.
220
220
00:11:09,363 --> 00:11:10,574
And it starts everything again.
221
221
00:11:10,574 --> 00:11:16,598
1001, 500 great, down, down,
down, Enter 1150, fantastic.
222
222
00:11:16,598 --> 00:11:20,107
So we've created a full game
loop here where our game starts,
223
223
00:11:20,107 --> 00:11:23,500
gives us the correct numbers,
we can replay it again.
224
224
00:11:23,500 --> 00:11:26,600
We've talked about having
our variables within our
225
225
00:11:26,600 --> 00:11:28,670
newly created StartGame function.
226
226
00:11:28,670 --> 00:11:31,330
We're calling our StartGame
function from Start.
227
227
00:11:31,330 --> 00:11:33,874
We've created a new function for
NextGuess.
228
228
00:11:33,874 --> 00:11:38,420
That's allowed us to encapsulate this
information, these statements down here.
229
229
00:11:38,420 --> 00:11:42,239
Makes our else statements, our if and
our else statements cleaner.
230
230
00:11:42,239 --> 00:11:45,570
So now we're just saying NextGuess and
StartGame.
231
231
00:11:45,570 --> 00:11:47,724
And our whole loop is complete,
so well done.
232
232
00:11:47,724 --> 00:11:50,786
Huge congratulations on
working through this section.
233
233
00:11:50,786 --> 00:11:53,086
Give yourself a huge high five and
a pat of the back.
234
234
00:11:53,086 --> 00:11:55,568
Cuz you have created something
that is very excellent.
235
235
00:11:55,568 --> 00:11:59,116
You've created working code and
a working game mechanic.
236
236
00:11:59,116 --> 00:11:59,749
Great work!
237
237
00:11:59,749 --> 00:12:01,290
I'll see you in an upcoming video.
23852
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.