Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
So let's talk finite state machines now.
This is a term that if you don't have a computer science background, you probably haven't really encountered
or if you have, it sounds like jargon and mumbo jumbo.
Congratulations if you have heard of it.
Well, what does it actually mean?
Why is it here?
Well, the idea of a finite state machine is that you have a finite number of states, funnily enough,
and you want to be able to transition between them.
So this is great for systems where you have different states that you could be in, for example, locomotion.
Here we've got a Granite State crashing state, an entire state, and they're mutually exclusive.
You can't be in more than one state this time.
It's also useful things like A.I. where you might have an ally that might have a running for cover state,
an attacking state are looking for ammo state, all those sorts of things.
And you, again, mutually exclusive.
You can't be in more than one of those at the time, but you can transition between them and the transitions.
The key here in any given state, if you get some sort of input into the finite state machine, it will
behave differently, go into a different state, depending on their input.
So for example, if you're in the grand state and you receive either a pressing jump event or a falling
event, you're going to move in to the entire state.
Similarly, if you are in the inner state and you receive a land event, you're going to go into the
grounded state.
But if you press jump or fall while you're in the air, it's not going to transition you anywhere.
It's just going to stay in that.
And similarly, if you get a land event while you're on the ground, it's not going to transition you
anywhere.
Now we could fill out this state machine.
For example, if we pressed Crouch, we go into the crafting states, we could transition back by either
pressing crouch or pressing jump.
So this is kind of a toggle.
I press crouch, I crouch, a press crouch, I'm crouch, or I could press crouch and then I could press
jump and I go back to being crouched.
Now, finally, if I fall while I'm crouched, I also go into the inner state.
But if I press jump, you can see the behaviors different.
I don't go straight from crouching to jumping.
At least not in my finite state machine.
You could obviously change that depending on how you want to set up your locomotion, essentially.
So how do we implement this sort of finite state machine in code?
The very useful we kind of want to use them in a lot of places.
How can we do it?
So let's have a look at this locomotion finite state machine, not a behavior that I've put together
here.
Now the cool of it is this enum of state.
So here we go.
On enum, it can either be grounded in our or crouching, and then we have a private variable of current
state, which is initialized starts off in the grounded state.
But we can transition between those states at will so we can receive any of our public functions.
Now, if I go ahead and collapse down these public functions, you can see what they all are.
We've got a jump for land and crouch, which corresponds with the actions we could see on our finite
state machine over here.
You can check it for yourself.
Basically, just for actions can be received by the finite state machine, and it's going to react differently
depending on what state we're in.
So the classic way to do that is with a Switch statement.
So in here in jump, you can see we are switching based on the current state.
So if we are in the ground it state, we are going to move to the entire state.
If we're in the crouching state will move to the ground state.
Now usually you would do more than this.
You may, in a state transition, have to enact some code.
So if you're moving from jumping to an air, you play a little jump animation.
You may need to change some other stuff there, too.
You also might need to run some code every frame while you're in the air.
So there's other stuff that you can do around the state machine.
But this is the kind of core of it in a core is being able to transition those states in the way represented
by the state machine itself.
And then you can see we can then do the same thing with the full finite state machine.
You can see if we are in the ground state.
We've moved to an air force in the Crouch state.
We also moved to an air.
But crucially, we are neglecting the other states here because if you fall while you're already in
the air, it doesn't change your state.
You stay in the state that you were already in.
So you will notice that the land and crouch methods are empty here.
And that's because I'd like you to fill out the remaining transitions.
So we've already dealt with the press and fall coming out of grounded.
We've also dealt with press and full coming out of crouching.
Now is your turn to try the crouching and land transitions, pulls the video and have it go.
OK, welcome back.
So let's give this a shot.
So the first thing is we're going to put a switch statement in here.
It's going to switch on again.
The current state, then we're going to need a case of state dots and let's have a look.
So we've got land, where can we transition land only one place when we're in air?
So that's nice and easy.
We're just going to say states dot in and we're going to set the current state from an ad to you.
States dot ground it.
It's like so and then we can simply go ahead and break, and that is land implemented.
Now let's have a look at Crouch Wiggins again.
Switch based on the current state, we're going to say a case the first one.
Crouch from ground it, so states dots grounded.
We're going to set the current state to state dot crouching and break afterwards.
And then we're going to put the case where we were already crouching and we pressed Crouch.
Then we're going to set the current state to state and ground it.
So it just toggles between the two of these and that is how you implement a finite state machine.
Now, hopefully, you can see that this isn't the nicest way of implementing a finite state machine,
because using all of these switch statements doesn't exactly lead to very legible, human readable code.
I'm just kind of trying to wrap my head around this.
The natural way for me to try and think about state machine is really state centric, not transition
centric.
This is focused on the transitions.
Is saying everything to do with the jump transition is under here.
What if I wanted to flip that around and say, what transitions can we have out of the grounded state?
Well, that's very unclear in this setup, and that is where the next programming pattern comes in the
state pattern.
And that, I think, is a much cleaner way of implementing a finite state machine, and we'll talk about
that in the next lecture.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.