Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:04,120 --> 00:00:06,820
So let's talk finite state machines now.
2
00:00:07,090 --> 00:00:12,820
This is a term that if you don't have a computer science background, you probably haven't really encountered
3
00:00:12,820 --> 00:00:15,340
or if you have, it sounds like jargon and mumbo jumbo.
4
00:00:15,460 --> 00:00:16,810
Congratulations if you have heard of it.
5
00:00:17,200 --> 00:00:18,650
Well, what does it actually mean?
6
00:00:18,670 --> 00:00:19,990
Why is it here?
7
00:00:20,320 --> 00:00:25,120
Well, the idea of a finite state machine is that you have a finite number of states, funnily enough,
8
00:00:25,120 --> 00:00:27,910
and you want to be able to transition between them.
9
00:00:27,910 --> 00:00:35,800
So this is great for systems where you have different states that you could be in, for example, locomotion.
10
00:00:35,830 --> 00:00:40,400
Here we've got a Granite State crashing state, an entire state, and they're mutually exclusive.
11
00:00:40,420 --> 00:00:42,460
You can't be in more than one state this time.
12
00:00:42,820 --> 00:00:49,570
It's also useful things like A.I. where you might have an ally that might have a running for cover state,
13
00:00:49,570 --> 00:00:53,920
an attacking state are looking for ammo state, all those sorts of things.
14
00:00:54,160 --> 00:00:56,110
And you, again, mutually exclusive.
15
00:00:56,110 --> 00:01:01,570
You can't be in more than one of those at the time, but you can transition between them and the transitions.
16
00:01:01,600 --> 00:01:08,290
The key here in any given state, if you get some sort of input into the finite state machine, it will
17
00:01:08,290 --> 00:01:11,290
behave differently, go into a different state, depending on their input.
18
00:01:11,320 --> 00:01:17,800
So for example, if you're in the grand state and you receive either a pressing jump event or a falling
19
00:01:17,800 --> 00:01:21,340
event, you're going to move in to the entire state.
20
00:01:21,520 --> 00:01:26,440
Similarly, if you are in the inner state and you receive a land event, you're going to go into the
21
00:01:26,440 --> 00:01:27,280
grounded state.
22
00:01:27,610 --> 00:01:32,770
But if you press jump or fall while you're in the air, it's not going to transition you anywhere.
23
00:01:32,780 --> 00:01:34,030
It's just going to stay in that.
24
00:01:34,050 --> 00:01:37,570
And similarly, if you get a land event while you're on the ground, it's not going to transition you
25
00:01:37,570 --> 00:01:37,960
anywhere.
26
00:01:38,110 --> 00:01:40,240
Now we could fill out this state machine.
27
00:01:40,240 --> 00:01:45,220
For example, if we pressed Crouch, we go into the crafting states, we could transition back by either
28
00:01:45,220 --> 00:01:47,020
pressing crouch or pressing jump.
29
00:01:47,050 --> 00:01:48,430
So this is kind of a toggle.
30
00:01:48,430 --> 00:01:53,200
I press crouch, I crouch, a press crouch, I'm crouch, or I could press crouch and then I could press
31
00:01:53,200 --> 00:01:55,790
jump and I go back to being crouched.
32
00:01:56,310 --> 00:02:01,000
Now, finally, if I fall while I'm crouched, I also go into the inner state.
33
00:02:01,000 --> 00:02:03,190
But if I press jump, you can see the behaviors different.
34
00:02:03,190 --> 00:02:05,350
I don't go straight from crouching to jumping.
35
00:02:05,530 --> 00:02:07,660
At least not in my finite state machine.
36
00:02:07,670 --> 00:02:12,700
You could obviously change that depending on how you want to set up your locomotion, essentially.
37
00:02:13,090 --> 00:02:17,060
So how do we implement this sort of finite state machine in code?
38
00:02:17,060 --> 00:02:19,960
The very useful we kind of want to use them in a lot of places.
39
00:02:20,680 --> 00:02:21,400
How can we do it?
40
00:02:22,180 --> 00:02:26,560
So let's have a look at this locomotion finite state machine, not a behavior that I've put together
41
00:02:26,560 --> 00:02:26,800
here.
42
00:02:27,130 --> 00:02:31,030
Now the cool of it is this enum of state.
43
00:02:31,270 --> 00:02:31,990
So here we go.
44
00:02:31,990 --> 00:02:38,230
On enum, it can either be grounded in our or crouching, and then we have a private variable of current
45
00:02:38,230 --> 00:02:42,100
state, which is initialized starts off in the grounded state.
46
00:02:42,490 --> 00:02:49,140
But we can transition between those states at will so we can receive any of our public functions.
47
00:02:49,150 --> 00:02:53,200
Now, if I go ahead and collapse down these public functions, you can see what they all are.
48
00:02:53,530 --> 00:02:59,140
We've got a jump for land and crouch, which corresponds with the actions we could see on our finite
49
00:02:59,140 --> 00:03:00,340
state machine over here.
50
00:03:00,550 --> 00:03:01,660
You can check it for yourself.
51
00:03:02,050 --> 00:03:06,760
Basically, just for actions can be received by the finite state machine, and it's going to react differently
52
00:03:06,760 --> 00:03:08,200
depending on what state we're in.
53
00:03:08,500 --> 00:03:12,230
So the classic way to do that is with a Switch statement.
54
00:03:12,250 --> 00:03:16,480
So in here in jump, you can see we are switching based on the current state.
55
00:03:16,480 --> 00:03:22,210
So if we are in the ground it state, we are going to move to the entire state.
56
00:03:22,630 --> 00:03:25,810
If we're in the crouching state will move to the ground state.
57
00:03:26,140 --> 00:03:29,230
Now usually you would do more than this.
58
00:03:29,230 --> 00:03:32,580
You may, in a state transition, have to enact some code.
59
00:03:32,590 --> 00:03:37,060
So if you're moving from jumping to an air, you play a little jump animation.
60
00:03:37,390 --> 00:03:40,180
You may need to change some other stuff there, too.
61
00:03:40,210 --> 00:03:43,810
You also might need to run some code every frame while you're in the air.
62
00:03:43,810 --> 00:03:46,870
So there's other stuff that you can do around the state machine.
63
00:03:47,380 --> 00:03:54,370
But this is the kind of core of it in a core is being able to transition those states in the way represented
64
00:03:54,550 --> 00:03:56,050
by the state machine itself.
65
00:03:56,440 --> 00:04:01,630
And then you can see we can then do the same thing with the full finite state machine.
66
00:04:01,630 --> 00:04:03,340
You can see if we are in the ground state.
67
00:04:03,340 --> 00:04:05,110
We've moved to an air force in the Crouch state.
68
00:04:05,120 --> 00:04:06,250
We also moved to an air.
69
00:04:06,550 --> 00:04:12,040
But crucially, we are neglecting the other states here because if you fall while you're already in
70
00:04:12,040 --> 00:04:14,140
the air, it doesn't change your state.
71
00:04:14,140 --> 00:04:16,180
You stay in the state that you were already in.
72
00:04:16,420 --> 00:04:21,220
So you will notice that the land and crouch methods are empty here.
73
00:04:21,250 --> 00:04:24,370
And that's because I'd like you to fill out the remaining transitions.
74
00:04:24,370 --> 00:04:28,300
So we've already dealt with the press and fall coming out of grounded.
75
00:04:28,660 --> 00:04:32,180
We've also dealt with press and full coming out of crouching.
76
00:04:32,860 --> 00:04:39,370
Now is your turn to try the crouching and land transitions, pulls the video and have it go.
77
00:04:42,210 --> 00:04:43,350
OK, welcome back.
78
00:04:43,770 --> 00:04:45,520
So let's give this a shot.
79
00:04:45,540 --> 00:04:49,260
So the first thing is we're going to put a switch statement in here.
80
00:04:49,440 --> 00:04:51,240
It's going to switch on again.
81
00:04:51,570 --> 00:04:56,220
The current state, then we're going to need a case of state dots and let's have a look.
82
00:04:56,220 --> 00:05:02,010
So we've got land, where can we transition land only one place when we're in air?
83
00:05:02,250 --> 00:05:03,480
So that's nice and easy.
84
00:05:03,510 --> 00:05:09,330
We're just going to say states dot in and we're going to set the current state from an ad to you.
85
00:05:09,330 --> 00:05:12,030
States dot ground it.
86
00:05:12,590 --> 00:05:20,220
It's like so and then we can simply go ahead and break, and that is land implemented.
87
00:05:20,230 --> 00:05:23,010
Now let's have a look at Crouch Wiggins again.
88
00:05:23,310 --> 00:05:29,040
Switch based on the current state, we're going to say a case the first one.
89
00:05:29,820 --> 00:05:34,910
Crouch from ground it, so states dots grounded.
90
00:05:36,210 --> 00:05:43,410
We're going to set the current state to state dot crouching and break afterwards.
91
00:05:43,800 --> 00:05:48,750
And then we're going to put the case where we were already crouching and we pressed Crouch.
92
00:05:48,990 --> 00:05:52,860
Then we're going to set the current state to state and ground it.
93
00:05:52,860 --> 00:05:59,190
So it just toggles between the two of these and that is how you implement a finite state machine.
94
00:05:59,370 --> 00:06:04,740
Now, hopefully, you can see that this isn't the nicest way of implementing a finite state machine,
95
00:06:04,740 --> 00:06:11,150
because using all of these switch statements doesn't exactly lead to very legible, human readable code.
96
00:06:11,160 --> 00:06:13,710
I'm just kind of trying to wrap my head around this.
97
00:06:13,950 --> 00:06:21,180
The natural way for me to try and think about state machine is really state centric, not transition
98
00:06:21,180 --> 00:06:21,660
centric.
99
00:06:21,660 --> 00:06:23,640
This is focused on the transitions.
100
00:06:23,640 --> 00:06:27,930
Is saying everything to do with the jump transition is under here.
101
00:06:28,410 --> 00:06:33,900
What if I wanted to flip that around and say, what transitions can we have out of the grounded state?
102
00:06:34,290 --> 00:06:40,080
Well, that's very unclear in this setup, and that is where the next programming pattern comes in the
103
00:06:40,080 --> 00:06:40,980
state pattern.
104
00:06:41,310 --> 00:06:46,950
And that, I think, is a much cleaner way of implementing a finite state machine, and we'll talk about
105
00:06:46,950 --> 00:06:48,150
that in the next lecture.
10734
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.