[MUSIC]
Next stop, we're going to discuss arrays.
And I'm not talking about asking your boss for more money, haha, bad joke there.
We are talking about arrays.
So what is an array?
An array stores into a variable, multiple elements of the same type.
Let's have a look at the example here, an example of how we could craft an array.
We start off with our types, so this is an integer.
You can see we have full, not full, whole numbers down here.
Same thing, full numbers, whole numbers.
If it was a float as our type, then we would have decimal place numbers or
numbers with decimal places.
We could have a string or we could have any other sort of type in here.
We use the square brackets to denote that this is an array.
And then we have a variable name, as we've done before.
This variable is going to store these particular numbers, these particular
values, in this variable, and it will store them at specific locations.
So, at location 0, and all good programming starts counting from 0, and
you can think of it of how many steps is it away from this initial value in here.
So this is 0 steps away and this is 2 steps away at number 5.
So each of these has a position.
If we wish to get access to our number 9 in here,
then we're referring to within our array, odd numbers array, the location number 4.
So back into unity, let us open up our adventure script here, adventuregame.cs.
We're gonna do a little example of an array, so we can see how it works.
First of all, so underneath my text component and
starting state, I will add an integer.
That is a, whoops, wrong brackets, that is a array.
So int, and then our square bracket, oddNumbers.
We're basically typing out what we saw on our screen
just a moment ago, = { 1, 3, 5, 7, 9};.
And then end with semicolon.
So this is the exact thing we saw previously,
int odd numbers one through to nine.
So we have declared our array and also initialized our array.
We could have stopped at this point just here, and
said ignore all this stuff down below and say there we go,
we have declared an array of type integer called oddNumbers.
And then later on,
we will say what the values of this variable is, of this array is.
But for now, we're going to start off by declaring and initializing in one step.
This is all code that we're just playing around with at the moment,
we're not gonna use this in our final game, but I'm using this to
illustrate how arrays work just within our current script in here.
So we have that, now let's go down to our start method,
and just add an extra line at start method.
Let's print out to our console, let's print out number seven from the array.
Let's go through this together.
Debug.Log( ).
Now we don't need our quotation marks cuz we're not printing out a string.
We're printing out our variables so we type in (oddNumbers) and
then within that, we open our square brackets, and we say number 3,
or we add number 3, and then semicolon.
So what this is saying is, print out Debug.Log oddNumbers variable, and
whatever is at position number 3.
Now, we're counting from 0, so 0, 1, 2, 3.
This is will be number 7, if all goes planned,
so Save over into Unity, hit Play.
Look at the console and it should say 7, as it does, fantastic.
Likewise, if we wanted to print out a string, then let's change up,
here's a mini challenge for you again, change this rray
from oddNumbers to days of the week.
And then use Debug.Log to print out, say, Tuesday into the console.
So here's a mini-challenge for you.
Pause the video now and take on that mini-challenge.
Okay welcome back.
Why is it many challenge and not a challenge,
well I guess because I don't have a slide for it therefore it's a mini challenge.
Okay so instead of this being an int, we will change it to a string, so
it's type string we will call it daysof.
Actually I'm gonna show you a trick here, I'll undo that.
In Visual Studio, if we click on the name of something and
we go Ctrl+A A, so double A.
I'm guessing it's Cmd+A, A on the Mac, apologies,
I don't know exactly what that is on the Mac, but on PC.
It is Ctrl AA,
that will allow us to rename all of the things that are called audit numbers.
So, I'm going to type in daysOfTheweek.
And you can see, its naming here, and it's also renaming there.
Because it knows, this is one and the same.
So that's a neat little trick there.
You can click Apply.
And instead of our numbers, we are going to change this.
We need our quotation marks for each of these.
We'll say, mon, whoops, I'll just type it, monday, the abbreviated version,
so we're not here forever.
{"tues", "wed", "thurs", "thurs"}; and
then I'm gonna leave it that the remaining three days of the week.
But I'll save ourselves the extra 30 seconds of typing that.
And then, what were we going for?
Tuesday.
So, this is position 0, and this is position 1.
So, we will change our reference here to 1.
Save, back over into Unity.
Give our script a moment to compile.
Click on Play.
And, then all going to plan, it will say, Tuesday.
Yes, it does.
Excellent.
So, that's the usage of arrays.
Pretty straight forward.
I'm going to remove our example that we've put in here, for now.
We don't need that for what we're doing.
Remove that and remove our Debug down here.
Arrays are super useful if we want to store multiple things in one variable,
and what is the use case for that one?
We're about to do a use case for that.
We're about to implement that in our game.
We're gonna jump here into state, and within state, what we want to do is
have a variable which stores all of our next states.
What do I mean by that?
At the moment we have starting state, let's go ahead and
create two more next states.
I'll right click up to create state, what will we call this?
We'll call this Room One.
Not a very good name for now.
We're going to go through and create our actual story pretty soon, but for
now we're doing, this is a placeholder Room 1 and I'll create a second one.
Right click, Create, State, call this Room 2.
Again, we have stunning state.
We have Room 1 and Room 2.
Room 1, I'll just write a little bit of text in here, I am in Room 1.
And then Room 2, we'll say, okay, this looks like room two.
This is placeholder, so feel free to type whatever you like in there, just so
we've got something.
If we end up, whoops, printing this out on the screen, we know that it's accurately
portraying the right particular state we're going for.
So we now have three states, starting state, Room 1 state and Room 2 state.
These are assets, these are our scriptable object assets.
And we have these three.
So if we wanted to say from starting state,
your options are to go to Room 1 or Room 2.
We wish to store that information in
a variable that has the concept of next state.
and when we look over here and states,
what that will require is that we serialize a variable called next states,
and then we create a new public method where we are returning the next states.
And that is going to be your challenge.
This is a little bit of a deeper challenge, so feel free to stop the video.
Take time to look through what we've worked through previously.
Have a look at how arrays work, and
have a good think about it, I'm gonna give you a few hints.
So if you don't want the hints, feel free to pause the video and
dive in as soon as you're ready.
The summary of what we're trying to do,
is create a public method as we've previously done in here.
This was our previous public method we've already created when called,
returns an array of next states.
So this one when it was called, it returned a string.
So this variable's a string and the return type is a string.
What we're looking to do is to return an array of next states.
So the type is states.
And here are the hints.
if you're ready, if you think that's enough information,
then pause the video and jump in now.
The first hint, is serialized array of type.
So I'm gonna change on the fly.
This should be serialized an array not serialized.
So serialized,
serialize an Array of type State called nextStates.
So that's the first thing.
We're creating our, declaring our variable, nextStates,
and we're serializing that, and it's of type Array.
And the public Public method that returns nextStates is called GetStateStory.
And the return type needs to be the same as the variable type.
Okay, so there are your clues.
Jump in, give that a shot, and
I'll see you back in here in a moment for our solution.
Okie-dokie, our first step here is to serialize a variables.
So serialize field is of type state cuz we're looking for
states, that will be within array.
We denote it as an array without square brackets, and we'll call this nextStates.
Cuz we want to know what are the next states that could comes after,
whatever our current state is, our current state is a starting state.
Now for creating our method, and the bottom of our script here public,
what is this, it's a state and
we need our square brackets because it's an array, it's an array of typeState.
Space.
We will call this GetNextStates.
No parameters.
So open and closed parentheses.
Curly braces and we will type return.
And what are we returning?
Well, we're returning our variable array of type states.
So we're returning nextStates.
And then we'll save that.
So that's creating a State Array called
nextStates, that is being returned when we call GetNextStates from our code.
One last step we are doing in this video, so well done if you've got that challenge
correct well done if you got close to that as well, we'll do one last step in here
and that is jump back into Unity, have a look at an update that we should now see.
If we click on any of these asset files, our scriptable object files here,
we can see that we now have a field called nextState.
It says size zero.
This is our array in here.
And because we've create it as a serialized array, that means we can
dynamically add, just by dragging and dropping we can add into our array,
we don't need to define all the different aspects of our array.
In our code, we can define the aspects of our array straight here in the inspector.
Now, the size we can say is four, for example, and
that will mean we're looking for four elements.
Four elements of our array that will be the next state.
We know we have 2.
So, we can tie in here 2.
That will give us 2, element 0 and element 1.
Room 1 we drag into element 0 and Room 2 we drag into element 1.
Again, we could click on the selector and then we can choose Room 1 and or
2 from there.
Now, another even easier way to do this, I'm just going to change this back to 0.
While clicking on startingState I can come up to the inspector.
There's a little lump, very little lump up in the top right here.
If you click on that, that will lock the inspector so no matter what else I click
on, the inspector will remain locked to the startingState.
That'll allow me to easily click on things without bouncing and
going to a different location.
If I had the lock off and I'm like okay cool.
Now I wanna grab Room 2.
Whoops, now I'm on room two on the inspector, and
I want to stay the inspector on starting state.
So, while that lock is on, I can click on Room 1, hold down Shift, Room 2,
drag both of those over to where it says nextStates.
Let go and it will automatically size it to 2.
And add those two elements into my array.
This is pretty handy, if you have lots and
lots of things that you want to add to an array.
You can just go [SOUND] in one spot, and
you don't have to select them one at a time.
Make sure you unlock the inspector, so
that we don't accidentally stay on on that locked on that asset fall.
Now we can have a look here, and we have our story in our startingState.
We have our nextState that we are going to go to using our array.
And in the next video, we're going to call the method that we've just
created our get next stage method, and we are going to give the player
the option of going to the nextState or the next part of our story.
So great work in this video.
Well done.
Hopefully arrays is making sense.
Again, we will be using these things over and over through out this course, so
if it's only just settling in a little bit into your head, no problem.
This will get crystal clear the longer we go through out this course, and
more we work through the contents.
So great work, I'll see you in the next video.