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:03,957
[MUSIC]
2
2
00:00:03,957 --> 00:00:06,440
Next stop, we're going to discuss arrays.
3
3
00:00:06,440 --> 00:00:11,250
And I'm not talking about asking your boss
for more money, haha, bad joke there.
4
4
00:00:11,250 --> 00:00:13,840
We are talking about arrays.
5
5
00:00:13,840 --> 00:00:14,640
So what is an array?
6
6
00:00:14,640 --> 00:00:19,840
An array stores into a variable,
multiple elements of the same type.
7
7
00:00:19,840 --> 00:00:23,760
Let's have a look at the example here,
an example of how we could craft an array.
8
8
00:00:23,760 --> 00:00:26,300
We start off with our types,
so this is an integer.
9
9
00:00:26,300 --> 00:00:29,980
You can see we have full,
not full, whole numbers down here.
10
10
00:00:29,980 --> 00:00:31,820
Same thing, full numbers, whole numbers.
11
11
00:00:31,820 --> 00:00:35,860
If it was a float as our type, then we
would have decimal place numbers or
12
12
00:00:35,860 --> 00:00:37,300
numbers with decimal places.
13
13
00:00:37,300 --> 00:00:40,670
We could have a string or we could
have any other sort of type in here.
14
14
00:00:40,670 --> 00:00:45,620
We use the square brackets to
denote that this is an array.
15
15
00:00:45,620 --> 00:00:47,900
And then we have a variable name,
as we've done before.
16
16
00:00:47,900 --> 00:00:52,163
This variable is going to store these
particular numbers, these particular
17
17
00:00:52,163 --> 00:00:56,300
values, in this variable, and
it will store them at specific locations.
18
18
00:00:56,300 --> 00:01:01,774
So, at location 0, and all good
programming starts counting from 0, and
19
19
00:01:01,774 --> 00:01:07,350
you can think of it of how many steps is
it away from this initial value in here.
20
20
00:01:07,350 --> 00:01:10,310
So this is 0 steps away and
this is 2 steps away at number 5.
21
21
00:01:10,310 --> 00:01:12,790
So each of these has a position.
22
22
00:01:12,790 --> 00:01:16,870
If we wish to get access
to our number 9 in here,
23
23
00:01:16,870 --> 00:01:23,390
then we're referring to within our array,
odd numbers array, the location number 4.
24
24
00:01:23,390 --> 00:01:30,970
So back into unity, let us open up our
adventure script here, adventuregame.cs.
25
25
00:01:30,970 --> 00:01:35,600
We're gonna do a little example of
an array, so we can see how it works.
26
26
00:01:35,600 --> 00:01:39,210
First of all, so
underneath my text component and
27
27
00:01:39,210 --> 00:01:43,090
starting state, I will add an integer.
28
28
00:01:44,350 --> 00:01:48,750
That is a, whoops,
wrong brackets, that is a array.
29
29
00:01:48,750 --> 00:01:51,840
So int, and then our square bracket,
oddNumbers.
30
30
00:01:51,840 --> 00:01:56,965
We're basically typing out
what we saw on our screen
31
31
00:01:56,965 --> 00:02:02,105
just a moment ago, = { 1, 3, 5, 7, 9};.
32
32
00:02:02,105 --> 00:02:06,540
And then end with semicolon.
33
33
00:02:06,540 --> 00:02:10,292
So this is the exact
thing we saw previously,
34
34
00:02:10,292 --> 00:02:13,770
int odd numbers one through to nine.
35
35
00:02:13,770 --> 00:02:17,790
So we have declared our array and
also initialized our array.
36
36
00:02:17,790 --> 00:02:21,010
We could have stopped at
this point just here, and
37
37
00:02:21,010 --> 00:02:24,130
said ignore all this stuff down below and
say there we go,
38
38
00:02:24,130 --> 00:02:29,610
we have declared an array of
type integer called oddNumbers.
39
39
00:02:29,610 --> 00:02:30,910
And then later on,
40
40
00:02:30,910 --> 00:02:35,640
we will say what the values of this
variable is, of this array is.
41
41
00:02:35,640 --> 00:02:40,870
But for now, we're going to start off by
declaring and initializing in one step.
42
42
00:02:40,870 --> 00:02:42,960
This is all code that we're just
playing around with at the moment,
43
43
00:02:42,960 --> 00:02:46,570
we're not gonna use this in our
final game, but I'm using this to
44
44
00:02:46,570 --> 00:02:50,210
illustrate how arrays work just
within our current script in here.
45
45
00:02:50,210 --> 00:02:55,350
So we have that,
now let's go down to our start method,
46
46
00:02:55,350 --> 00:02:58,320
and just add an extra
line at start method.
47
47
00:02:58,320 --> 00:03:05,560
Let's print out to our console, let's
print out number seven from the array.
48
48
00:03:05,560 --> 00:03:07,040
Let's go through this together.
49
49
00:03:07,040 --> 00:03:07,895
Debug.Log( ).
50
50
00:03:07,895 --> 00:03:16,110
Now we don't need our quotation marks
cuz we're not printing out a string.
51
51
00:03:16,110 --> 00:03:21,731
We're printing out our variables so
we type in (oddNumbers) and
52
52
00:03:21,731 --> 00:03:26,520
then within that, we open our square
brackets, and we say number 3,
53
53
00:03:26,520 --> 00:03:29,400
or we add number 3, and then semicolon.
54
54
00:03:29,400 --> 00:03:34,520
So what this is saying is, print out
Debug.Log oddNumbers variable, and
55
55
00:03:34,520 --> 00:03:37,140
whatever is at position number 3.
56
56
00:03:37,140 --> 00:03:37,948
Now, we're counting from 0, so 0, 1, 2, 3.
57
57
00:03:37,948 --> 00:03:43,394
This is will be number 7,
if all goes planned,
58
58
00:03:43,394 --> 00:03:47,488
so Save over into Unity, hit Play.
59
59
00:03:52,040 --> 00:03:56,760
Look at the console and
it should say 7, as it does, fantastic.
60
60
00:03:56,760 --> 00:04:01,710
Likewise, if we wanted to print out
a string, then let's change up,
61
61
00:04:01,710 --> 00:04:06,830
here's a mini challenge for
you again, change this rray
62
62
00:04:06,830 --> 00:04:11,390
from oddNumbers to days of the week.
63
63
00:04:11,390 --> 00:04:17,620
And then use Debug.Log to print out,
say, Tuesday into the console.
64
64
00:04:17,620 --> 00:04:19,540
So here's a mini-challenge for you.
65
65
00:04:19,540 --> 00:04:22,663
Pause the video now and
take on that mini-challenge.
66
66
00:04:24,200 --> 00:04:25,550
Okay welcome back.
67
67
00:04:25,550 --> 00:04:27,580
Why is it many challenge and
not a challenge,
68
68
00:04:27,580 --> 00:04:31,350
well I guess because I don't have a slide
for it therefore it's a mini challenge.
69
69
00:04:31,350 --> 00:04:35,410
Okay so instead of this being an int,
we will change it to a string, so
70
70
00:04:35,410 --> 00:04:40,350
it's type string we will call it daysof.
71
71
00:04:40,350 --> 00:04:43,110
Actually I'm gonna show you a trick here,
I'll undo that.
72
72
00:04:43,110 --> 00:04:47,838
In Visual Studio,
if we click on the name of something and
73
73
00:04:47,838 --> 00:04:50,610
we go Ctrl+A A, so double A.
74
74
00:04:50,610 --> 00:04:53,030
I'm guessing it's Cmd+A,
A on the Mac, apologies,
75
75
00:04:53,030 --> 00:04:56,516
I don't know exactly what that
is on the Mac, but on PC.
76
76
00:04:56,516 --> 00:04:58,520
It is Ctrl AA,
77
77
00:04:58,520 --> 00:05:02,320
that will allow us to rename all of
the things that are called audit numbers.
78
78
00:05:02,320 --> 00:05:06,570
So, I'm going to type in daysOfTheweek.
79
79
00:05:06,570 --> 00:05:11,280
And you can see, its naming here,
and it's also renaming there.
80
80
00:05:11,280 --> 00:05:12,940
Because it knows,
this is one and the same.
81
81
00:05:12,940 --> 00:05:14,580
So that's a neat little trick there.
82
82
00:05:14,580 --> 00:05:16,230
You can click Apply.
83
83
00:05:16,230 --> 00:05:18,890
And instead of our numbers,
we are going to change this.
84
84
00:05:18,890 --> 00:05:20,710
We need our quotation marks for
each of these.
85
85
00:05:20,710 --> 00:05:25,590
We'll say, mon, whoops, I'll just type it,
monday, the abbreviated version,
86
86
00:05:25,590 --> 00:05:29,696
so we're not here forever.
87
87
00:05:29,696 --> 00:05:32,798
{"tues", "wed", "thurs", "thurs"}; and
88
88
00:05:32,798 --> 00:05:36,430
then I'm gonna leave it that
the remaining three days of the week.
89
89
00:05:36,430 --> 00:05:40,630
But I'll save ourselves the extra
30 seconds of typing that.
90
90
00:05:40,630 --> 00:05:42,610
And then, what were we going for?
91
91
00:05:42,610 --> 00:05:43,110
Tuesday.
92
92
00:05:43,110 --> 00:05:45,080
So, this is position 0,
and this is position 1.
93
93
00:05:45,080 --> 00:05:49,410
So, we will change our
reference here to 1.
94
94
00:05:49,410 --> 00:05:52,490
Save, back over into Unity.
95
95
00:05:52,490 --> 00:05:54,960
Give our script a moment to compile.
96
96
00:05:54,960 --> 00:05:55,875
Click on Play.
97
97
00:05:55,875 --> 00:05:58,490
And, then all going to plan,
it will say, Tuesday.
98
98
00:05:58,490 --> 00:05:59,170
Yes, it does.
99
99
00:05:59,170 --> 00:05:59,750
Excellent.
100
100
00:05:59,750 --> 00:06:01,740
So, that's the usage of arrays.
101
101
00:06:01,740 --> 00:06:02,780
Pretty straight forward.
102
102
00:06:02,780 --> 00:06:06,760
I'm going to remove our example
that we've put in here, for now.
103
103
00:06:06,760 --> 00:06:08,770
We don't need that for what we're doing.
104
104
00:06:08,770 --> 00:06:12,810
Remove that and
remove our Debug down here.
105
105
00:06:14,520 --> 00:06:19,680
Arrays are super useful if we want to
store multiple things in one variable,
106
106
00:06:19,680 --> 00:06:21,230
and what is the use case for that one?
107
107
00:06:21,230 --> 00:06:22,730
We're about to do a use case for that.
108
108
00:06:22,730 --> 00:06:24,762
We're about to implement that in our game.
109
109
00:06:24,762 --> 00:06:29,630
We're gonna jump here into state, and
within state, what we want to do is
110
110
00:06:29,630 --> 00:06:34,770
have a variable which stores
all of our next states.
111
111
00:06:34,770 --> 00:06:35,630
What do I mean by that?
112
112
00:06:35,630 --> 00:06:39,800
At the moment we have starting state,
let's go ahead and
113
113
00:06:39,800 --> 00:06:42,010
create two more next states.
114
114
00:06:42,010 --> 00:06:45,980
I'll right click up to create state,
what will we call this?
115
115
00:06:45,980 --> 00:06:48,650
We'll call this Room One.
116
116
00:06:49,770 --> 00:06:51,160
Not a very good name for now.
117
117
00:06:51,160 --> 00:06:54,950
We're going to go through and create
our actual story pretty soon, but for
118
118
00:06:54,950 --> 00:06:58,210
now we're doing, this is a placeholder
Room 1 and I'll create a second one.
119
119
00:06:58,210 --> 00:07:01,590
Right click, Create,
State, call this Room 2.
120
120
00:07:01,590 --> 00:07:04,110
Again, we have stunning state.
121
121
00:07:04,110 --> 00:07:05,460
We have Room 1 and Room 2.
122
122
00:07:05,460 --> 00:07:10,350
Room 1, I'll just write a little bit
of text in here, I am in Room 1.
123
123
00:07:11,520 --> 00:07:18,550
And then Room 2, we'll say,
okay, this looks like room two.
124
124
00:07:18,550 --> 00:07:21,680
This is placeholder, so feel free to
type whatever you like in there, just so
125
125
00:07:21,680 --> 00:07:22,850
we've got something.
126
126
00:07:22,850 --> 00:07:26,990
If we end up, whoops, printing this out on
the screen, we know that it's accurately
127
127
00:07:26,990 --> 00:07:29,810
portraying the right particular
state we're going for.
128
128
00:07:29,810 --> 00:07:34,400
So we now have three states, starting
state, Room 1 state and Room 2 state.
129
129
00:07:34,400 --> 00:07:37,630
These are assets,
these are our scriptable object assets.
130
130
00:07:37,630 --> 00:07:38,810
And we have these three.
131
131
00:07:38,810 --> 00:07:42,180
So if we wanted to say
from starting state,
132
132
00:07:42,180 --> 00:07:45,170
your options are to go to Room 1 or
Room 2.
133
133
00:07:45,170 --> 00:07:47,910
We wish to store that information in
134
134
00:07:47,910 --> 00:07:52,140
a variable that has
the concept of next state.
135
135
00:07:52,140 --> 00:07:54,545
and when we look over here and states,
136
136
00:07:54,545 --> 00:07:59,283
what that will require is that we
serialize a variable called next states,
137
137
00:07:59,283 --> 00:08:04,190
and then we create a new public method
where we are returning the next states.
138
138
00:08:04,190 --> 00:08:06,740
And that is going to be your challenge.
139
139
00:08:06,740 --> 00:08:10,560
This is a little bit of a deeper
challenge, so feel free to stop the video.
140
140
00:08:10,560 --> 00:08:13,540
Take time to look through what
we've worked through previously.
141
141
00:08:13,540 --> 00:08:15,510
Have a look at how arrays work, and
142
142
00:08:15,510 --> 00:08:17,900
have a good think about it,
I'm gonna give you a few hints.
143
143
00:08:17,900 --> 00:08:21,000
So if you don't want the hints,
feel free to pause the video and
144
144
00:08:21,000 --> 00:08:22,440
dive in as soon as you're ready.
145
145
00:08:22,440 --> 00:08:23,750
The summary of what we're trying to do,
146
146
00:08:23,750 --> 00:08:27,400
is create a public method as
we've previously done in here.
147
147
00:08:27,400 --> 00:08:31,560
This was our previous public method
we've already created when called,
148
148
00:08:31,560 --> 00:08:34,160
returns an array of next states.
149
149
00:08:34,160 --> 00:08:37,270
So this one when it was called,
it returned a string.
150
150
00:08:37,270 --> 00:08:41,830
So this variable's a string and
the return type is a string.
151
151
00:08:41,830 --> 00:08:45,310
What we're looking to do is to
return an array of next states.
152
152
00:08:45,310 --> 00:08:46,770
So the type is states.
153
153
00:08:46,770 --> 00:08:47,520
And here are the hints.
154
154
00:08:47,520 --> 00:08:49,920
if you're ready,
if you think that's enough information,
155
155
00:08:49,920 --> 00:08:51,410
then pause the video and jump in now.
156
156
00:08:51,410 --> 00:08:54,870
The first hint,
is serialized array of type.
157
157
00:08:54,870 --> 00:08:56,580
So I'm gonna change on the fly.
158
158
00:08:56,580 --> 00:09:00,590
This should be serialized
an array not serialized.
159
159
00:09:00,590 --> 00:09:03,700
So serialized,
160
160
00:09:03,700 --> 00:09:08,900
serialize an Array of type
State called nextStates.
161
161
00:09:08,900 --> 00:09:09,740
So that's the first thing.
162
162
00:09:09,740 --> 00:09:12,799
We're creating our,
declaring our variable, nextStates,
163
163
00:09:12,799 --> 00:09:15,520
and we're serializing that,
and it's of type Array.
164
164
00:09:15,520 --> 00:09:21,685
And the public Public method that returns
nextStates is called GetStateStory.
165
165
00:09:21,685 --> 00:09:25,595
And the return type needs to be
the same as the variable type.
166
166
00:09:25,595 --> 00:09:27,535
Okay, so there are your clues.
167
167
00:09:27,535 --> 00:09:28,645
Jump in, give that a shot, and
168
168
00:09:28,645 --> 00:09:31,440
I'll see you back in here in a moment for
our solution.
169
169
00:09:31,440 --> 00:09:36,220
Okie-dokie, our first step here
is to serialize a variables.
170
170
00:09:36,220 --> 00:09:40,978
So serialize field is of type
state cuz we're looking for
171
171
00:09:40,978 --> 00:09:44,340
states, that will be within array.
172
172
00:09:44,340 --> 00:09:49,805
We denote it as an array without square
brackets, and we'll call this nextStates.
173
173
00:09:51,055 --> 00:09:53,800
Cuz we want to know what are the next
states that could comes after,
174
174
00:09:53,800 --> 00:09:58,060
whatever our current state is,
our current state is a starting state.
175
175
00:09:58,060 --> 00:10:04,000
Now for creating our method, and
the bottom of our script here public,
176
176
00:10:04,000 --> 00:10:06,830
what is this, it's a state and
177
177
00:10:06,830 --> 00:10:12,450
we need our square brackets because it's
an array, it's an array of typeState.
178
178
00:10:12,450 --> 00:10:13,160
Space.
179
179
00:10:13,160 --> 00:10:15,920
We will call this GetNextStates.
180
180
00:10:17,870 --> 00:10:18,590
No parameters.
181
181
00:10:18,590 --> 00:10:20,080
So open and closed parentheses.
182
182
00:10:21,650 --> 00:10:24,390
Curly braces and we will type return.
183
183
00:10:24,390 --> 00:10:27,350
And what are we returning?
184
184
00:10:27,350 --> 00:10:31,070
Well, we're returning our
variable array of type states.
185
185
00:10:31,070 --> 00:10:33,475
So we're returning nextStates.
186
186
00:10:35,580 --> 00:10:37,220
And then we'll save that.
187
187
00:10:37,220 --> 00:10:41,967
So that's creating a State Array called
188
188
00:10:41,967 --> 00:10:48,050
nextStates, that is being returned when
we call GetNextStates from our code.
189
189
00:10:48,050 --> 00:10:50,830
One last step we are doing in this video,
so well done if you've got that challenge
190
190
00:10:50,830 --> 00:10:54,530
correct well done if you got close to that
as well, we'll do one last step in here
191
191
00:10:54,530 --> 00:10:59,840
and that is jump back into Unity, have a
look at an update that we should now see.
192
192
00:10:59,840 --> 00:11:05,230
If we click on any of these asset files,
our scriptable object files here,
193
193
00:11:05,230 --> 00:11:07,960
we can see that we now have
a field called nextState.
194
194
00:11:07,960 --> 00:11:09,870
It says size zero.
195
195
00:11:09,870 --> 00:11:12,180
This is our array in here.
196
196
00:11:12,180 --> 00:11:15,935
And because we've create it as
a serialized array, that means we can
197
197
00:11:15,935 --> 00:11:20,012
dynamically add, just by dragging and
dropping we can add into our array,
198
198
00:11:20,012 --> 00:11:23,532
we don't need to define all
the different aspects of our array.
199
199
00:11:23,532 --> 00:11:29,940
In our code, we can define the aspects of
our array straight here in the inspector.
200
200
00:11:29,940 --> 00:11:33,870
Now, the size we can say is four,
for example, and
201
201
00:11:33,870 --> 00:11:36,440
that will mean we're looking for
four elements.
202
202
00:11:36,440 --> 00:11:40,520
Four elements of our array
that will be the next state.
203
203
00:11:40,520 --> 00:11:42,050
We know we have 2.
204
204
00:11:42,050 --> 00:11:43,670
So, we can tie in here 2.
205
205
00:11:43,670 --> 00:11:46,820
That will give us 2,
element 0 and element 1.
206
206
00:11:46,820 --> 00:11:51,500
Room 1 we drag into element 0 and
Room 2 we drag into element 1.
207
207
00:11:51,500 --> 00:11:55,343
Again, we could click on the selector and
then we can choose Room 1 and or
208
208
00:11:55,343 --> 00:11:56,510
2 from there.
209
209
00:11:56,510 --> 00:12:01,970
Now, another even easier way to do this,
I'm just going to change this back to 0.
210
210
00:12:01,970 --> 00:12:05,190
While clicking on startingState
I can come up to the inspector.
211
211
00:12:05,190 --> 00:12:08,080
There's a little lump,
very little lump up in the top right here.
212
212
00:12:08,080 --> 00:12:12,800
If you click on that, that will lock the
inspector so no matter what else I click
213
213
00:12:12,800 --> 00:12:17,630
on, the inspector will remain
locked to the startingState.
214
214
00:12:17,630 --> 00:12:21,180
That'll allow me to easily click
on things without bouncing and
215
215
00:12:21,180 --> 00:12:22,300
going to a different location.
216
216
00:12:22,300 --> 00:12:23,960
If I had the lock off and
I'm like okay cool.
217
217
00:12:23,960 --> 00:12:25,930
Now I wanna grab Room 2.
218
218
00:12:25,930 --> 00:12:28,710
Whoops, now I'm on room
two on the inspector, and
219
219
00:12:28,710 --> 00:12:32,590
I want to stay the inspector
on starting state.
220
220
00:12:32,590 --> 00:12:37,070
So, while that lock is on, I can click
on Room 1, hold down Shift, Room 2,
221
221
00:12:37,070 --> 00:12:40,180
drag both of those over to
where it says nextStates.
222
222
00:12:40,180 --> 00:12:43,620
Let go and
it will automatically size it to 2.
223
223
00:12:43,620 --> 00:12:45,690
And add those two elements into my array.
224
224
00:12:45,690 --> 00:12:47,270
This is pretty handy, if you have lots and
225
225
00:12:47,270 --> 00:12:49,240
lots of things that you
want to add to an array.
226
226
00:12:49,240 --> 00:12:50,591
You can just go [SOUND] in one spot, and
227
227
00:12:50,591 --> 00:12:52,210
you don't have to select
them one at a time.
228
228
00:12:52,210 --> 00:12:54,971
Make sure you unlock the inspector, so
229
229
00:12:54,971 --> 00:13:00,185
that we don't accidentally stay on
on that locked on that asset fall.
230
230
00:13:00,185 --> 00:13:05,205
Now we can have a look here, and
we have our story in our startingState.
231
231
00:13:05,205 --> 00:13:08,335
We have our nextState that we
are going to go to using our array.
232
232
00:13:08,335 --> 00:13:12,878
And in the next video, we're going
to call the method that we've just
233
233
00:13:12,878 --> 00:13:17,344
created our get next stage method,
and we are going to give the player
234
234
00:13:17,344 --> 00:13:21,580
the option of going to the nextState or
the next part of our story.
235
235
00:13:21,580 --> 00:13:22,830
So great work in this video.
236
236
00:13:22,830 --> 00:13:23,770
Well done.
237
237
00:13:23,770 --> 00:13:25,540
Hopefully arrays is making sense.
238
238
00:13:25,540 --> 00:13:28,490
Again, we will be using these things over
and over through out this course, so
239
239
00:13:28,490 --> 00:13:32,230
if it's only just settling in a little
bit into your head, no problem.
240
240
00:13:32,230 --> 00:13:35,300
This will get crystal clear the longer
we go through out this course, and
241
241
00:13:35,300 --> 00:13:36,610
more we work through the contents.
242
242
00:13:36,610 --> 00:13:38,400
So great work,
I'll see you in the next video.
23863
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.