- [Narrator] When scripting in Unity,
sometimes we want a variable,
that is one of a set of constants.
Consider, the points of a compass.
We could describe these using an integer;
where 0 was North, 1 was East, 2 was South,
and 3 was West.
This wouldn't be very easy to read,
or write in codes since it would mean
remembering what each number referred to.
Instead, we can create something called an enumeration.
Enumerations, which are often referred to as enums,
are a special data type, that has a specific
subset of possible values.
Here, we have a script named CardinalDirection.
Let's create a enumeration
to represent the different directions of a compass.
We can create the enumeration,
either inside or outside of a class.
Note, that we can also create a C# script,
that only contains this enum,
and instead declaring this as a class,
we've declared it as a enum.
We can then use this enum in classes
that live in other scripts,
because, it's public.
We could place a enum in a class,
if only that class needed access to it.
We need to list the various constants of the enum,
separated by commas, in braces.
First, we want North.
Then, East, South, and finally, West.
Unlike, when you normally close braces,
this is the end of a statement,
and so, needs a semicolon.
Each of the constants declared in this enum,
has a value, which by default,
is an integer counting up, from zero.
This is integrated through the set,
so, North has a value of 0,
East has a value of 1, South has a value of 2,
and West has a value of 3.
Both the type of value,
and the values themselves,
can be overwritten, if required.
If we were to declare the enum with
North = 1, then East would have
a value of 2, South a value of 3,
and West a value of 4.
Alternatively, we could declare a value
for each of the constants,
such as, North = 10,
East = 11, South = 15,
and West = 27, for example.
We can also change the
type of constants in an enum.
The constants can be changed
to any integral type.
We change the type by putting a colon
after the name of our enum,
and putting the type afterwards.
For example, we could have
Direction : short.
This would mean that the type of the enum
was short instead of int.
One of the main reasons for changing
the type of an enum, is optimization,
but generally we won't worry about it.
Once we have our enum declared,
we then make variables of the enum's type.
Let's make a direction variable,
called myDirection.
We now can treat this,
just like any other variable,
but, whereas an integer might have
a value of 5, or a float might have
a value of 97.3,
our myDirection variable
can have values of Direction.North,
Direction.East, Direction.South,
or, Direction.West.
Let's set myDirection = Direction.North,
in the Start function.
Our enum can also be parsed into,
and returned from, functions.
We simply use the name of the enum,
in our case Direction,
where would use a type.
To illustrate this, let's make
a small function to reverse a given direction.
Given North, it should
return South, and so on.
The return type for our function is Direction,
and we'll call the function ReverseDirection.
We'll give the function a Direction parameter,
which we'll call "dir"
and return the opposite.
Now, all we need to do is compare "dir"
to our various Direction constants.
We only need to check for North,
East, South and West,
because those are the only possible values.