All language subtitles for 2. Implementing a FSM in Godot

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranî)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian Download
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu

Original subtitles

0 Now that we have covered the basics of the finite state machine, let's look how one can be implemented 1

using GDScript. 2

There are many ways to implement the finite state machine. 3

This is but only one of them. 4

We will use two type of scripts the main finite state machine, which is responsible for changing states 5

and running them, but also keeping track of the values in a small database and the others, which are 6

the actual state implementations. 7

Let's look first at the finite state machine. And I've already created this structure, so we will go 8

inside the custom finite state machine and start working with it. 9

And of course. 10

The first thing that we need to do is create the basic structure. 11

We will use the entry_state that can be configured in the inspector of this state machine to know exactly 12

which state should be run first. 13

The current_state is the variable which holds the current state that is actually running. 14

The database keeps our values when we need to access particular information about this state machine. 15

And we also need the state list, which is also a dictionary. In the _ready function 16

we will need to do a couple of different things. 17

The first: we will parse all the children. 18

So as you can see here, we have three states and we need to get all of them. 19

And for this we can parse all the children. 20

And... 21

First we will initialize the children's finite state machine with this one. 22

And of course, we need to add the state list of the state name and the state. 23

This will basically create the key-pair value of the state name and the state in this finite state machine. 24

This will be useful, for example, when we are going to change state 25

from the entry_state. Because the entry_state is a string. 26

So it's not an actual state object, but this one is a state object. 27

And by having this dictionary, we can actually convert string names to actual state objects. 28

Now let's proceed and implement the changed state function because we currently don't have it. 29

And here we need the new state, which would be the new state where we will head into. 30

And if the current_state is not null, then that means that the current_state currently has something 31

in there. 32

Of course, we always need to check to see if the current state is a legit state. 33

And for this we will use the exit_state() because we will call for every single state the enter_state(), exit_state() 34

and update_state(). 35

So in this case, if the current state has an exit_state() (function), then of course we will need to call this 36

function. And be mindful that if the current state did not have this exit_state() method, then this will give 37

an error without 38

this if. So, it's important to put this check before. Next, 39

we need to check if the state list actually registered a state with the name that we want to change 40

with. 41

So if the state list does not have. 42

So to say, does not have, we can just negate the has(). 43

So instead of this which will return true, if it does have the state, then it will it will make 44

it false if it does have the state => return. 45

This is really important because we cannot change to a state that is invalid because it doesn't exist. 46

Now that we know for sure that the state exists. 47

We can say current state equals state list new state. 48

And since this is a string, it is important to make sure this check happens because otherwise this 49

query will give an error. 50

This is also for check. 51

But if the current state has method. 52

enter_state() 53

Then, of course, we need to call it. 54

The enter_state() and exit_state() are always called when a state gets changed. 55

This is important for one-time functions that need to run in that particular state. 56

Now let's move on with the process. 57

So in the _process() part, what we need to do is we need to check if there is a current state. 58

And if it's not. 59

We will call return because we don't want to process this any longer. 60

Of course, what we could have done instead would have been if the current state is not null and then 61

write our code here. 62

But the problem by doing this is that we will have more indentation as we go with this kind of structure. 63

So instead what we do is just check if it's null and then return to keep the indentation level. 64

So to keep this indentation as close to the left as possible. This will create a nice code structure 65

in the long run. 66

And here, of course, we will call current_state because we know it exists and if it has the method. 67

process() 68

And be mindful here 69

this process is a little different from this one because this gets automatically called by Godot, 70

and we need a custom one because we would need to call it here and we need to make sure that it only 71

gets called here. 72

So I'm just copy-pasting this because it's much easier. And I'm going to put in the delta. 73

Of course, I'll do exactly the same for the _physics_process(). 74

It's important to copy paste this to make sure that there's no error. 75

Why is it important to have _process() and _physics_process() 76

because some functions only work in _physics_process(). 77

So it's important to have both of these supported in our state machine. 78

Of course, we already have the change_state(). 79

What we need to do here is create two more options, and that is set_value(), which is a setter for 80

the key-pair database we have. 81

So value name and value. And "db" 82

the database dictionary that we created, the value name will be equal value and next one will have 83

get_value() which can just get the value name. 84

And here we can say if the database has(value_name) first because we cannot query something that doesn't 85

exist, then it will return to the database value name. 86

This is important because our state machine will need to communicate with different states to know what 87

is the current state of the whole system. 88

For example, is the player retreating? 89

Is the player attacking so and so forth. 90

So we need to have a synchronized database for all of these states. 91

Now let's move to a particular node implementation. 92

And for this I will target the idle state. 93

So first we would need to have a reference to the custom state machine that we just implemented. 94

And now we need to make sure we have: 95

enter_state() 96

And as you remember, the enter_state() is called when a state gets changed. 97

So this will be called when the idle state is changed. 98

Now, we also need the exit_state(). 99

Make sure that the names are exactly the ones used in the custom finite state machine. 100

Otherwise it won't work. 101

And here the process() is not with underscore, because this will be automatically called every single frame 102

from Godot. 103

And it's important because you have if you have multiple states, we don't want them to run without 104

any control, but instead being able to control them through this process. 105

So in order to make this happen, we need to make sure that this not with underscore. This is basically 106

you can call it whatever you like. 107

But I just called the process. Delta. 108

And here. 109

Before we actually do any process from the state, we need to check the conditions. 110

And the conditions are really important because they will tell us if we need to go from this state to 111

another one based on some values that are stored in the database. 112

We will create these and the other states in the mini example for the finite state machine. 113

Let's find out how to make them.

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.