Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
0
1
00:00:00,030 --> 00:00:06,070
In this example, we will have three different states: the idle state, the move state, and the fire
1
2
00:00:06,120 --> 00:00:06,600
state.
2
3
00:00:06,930 --> 00:00:10,940
These are basically just some scripts with a little code.
3
4
00:00:10,950 --> 00:00:12,050
They're nothing special.
4
5
00:00:12,060 --> 00:00:16,560
But of course you can imagine that this is an AI because it can be
5
6
00:00:16,560 --> 00:00:22,110
an actual AI here. Every state will be connected with each other and will trigger transitions when
6
7
00:00:22,110 --> 00:00:23,580
specific conditions are met.
7
8
00:00:23,610 --> 00:00:26,910
Let's configure the finite state machine example first.
8
9
00:00:27,000 --> 00:00:30,690
This will be the main screen which currently has two buttons.
9
10
00:00:31,960 --> 00:00:34,270
Is moving button and is firing button.
10
11
00:00:34,570 --> 00:00:38,410
Basically what they'll do is have a special signal.
11
12
00:00:39,470 --> 00:00:44,750
That this called in this debug script and this debug script.
12
13
00:00:44,750 --> 00:00:53,720
What it does it has two signals from is moving button and is firing button and it was set up two states
13
14
00:00:53,900 --> 00:01:02,450
and will set up inside the custom finite state machine that we created. The database with the is_moving or
14
15
00:01:02,450 --> 00:01:07,730
is_firing and will also reflect this in the button text itself.
15
16
00:01:07,730 --> 00:01:11,990
Also, we need to show the current state in a label.
16
17
00:01:12,770 --> 00:01:19,310
So if we go back, the current state will be visible in this label to be able to properly debug it.
17
18
00:01:19,820 --> 00:01:25,610
Now let's go back a little to the idle state because we need to make some changes here because we will
18
19
00:01:25,610 --> 00:01:29,180
have some connections between idle state, move state and fire state.
19
20
00:01:29,180 --> 00:01:31,190
If the finite state machine...
20
21
00:01:32,930 --> 00:01:38,480
By the way, in this implementation, in the check conditions, we will create the transitions for various
21
22
00:01:38,480 --> 00:01:38,940
states.
22
23
00:01:39,410 --> 00:01:43,550
So we need to get the value and is moving.
23
24
00:01:44,700 --> 00:01:48,300
Be mindful that this value is actually set in this debug script.
24
25
00:01:48,300 --> 00:01:49,250
So is_moving.
25
26
00:01:49,260 --> 00:01:50,400
It needs to be the same.
26
27
00:01:50,880 --> 00:01:53,670
I can just copy paste it to make sure is the same.
27
28
00:01:53,790 --> 00:01:58,650
Otherwise what I recommend you to do is create a globals file.
28
29
00:01:58,830 --> 00:02:06,300
You can do that by using a singleton in Godot and basically create a variable that has this string
29
30
00:02:06,330 --> 00:02:10,680
and use that variable instead to remove the hardcoding part.
30
31
00:02:10,680 --> 00:02:16,110
Because by just missing one character here, then it won't work anymore.
31
32
00:02:16,530 --> 00:02:18,890
This is a pro tip basically.
32
33
00:02:19,320 --> 00:02:21,000
So get value is_moving.
33
34
00:02:21,000 --> 00:02:22,290
And of course this is true.
34
35
00:02:23,550 --> 00:02:26,610
Then we need to change the state to the move state.
35
36
00:02:32,030 --> 00:02:33,830
How is the move state called?
36
37
00:02:33,860 --> 00:02:35,960
Well, it's actually the same name as here.
37
38
00:02:36,230 --> 00:02:41,930
And, if you remember from the implementation, we get every state from the children.
38
39
00:02:42,880 --> 00:02:46,270
We create the dictionary where we keep our values.
39
40
00:02:46,270 --> 00:02:50,590
So based on the name actually written here, we actually get the state as well.
40
41
00:02:51,910 --> 00:02:54,070
So this is how we go to the move state.
41
42
00:02:54,070 --> 00:02:59,350
And now, of course, we don't want to continue this because we already changed the finite state machine
42
43
00:02:59,350 --> 00:03:00,070
to the move state.
43
44
00:03:00,580 --> 00:03:07,660
But if it was not moving, then it might be a possibility for it to be shooting or is_firing.
44
45
00:03:09,420 --> 00:03:12,260
And if that's the case, then we need to change it.
45
46
00:03:14,400 --> 00:03:15,180
FireState.
46
47
00:03:16,380 --> 00:03:20,040
And by changing to the fire state, then of course, this is a new state.
47
48
00:03:20,460 --> 00:03:26,760
And in the move state as well as in the fire state, we see very similar code, but we just check for
48
49
00:03:26,760 --> 00:03:29,280
the values in the other direction.
49
50
00:03:29,340 --> 00:03:32,880
Just be mindful that some changes here are really important.
50
51
00:03:32,880 --> 00:03:35,820
So as you can see in the move state, it's a little different.
51
52
00:03:36,300 --> 00:03:41,130
The if statements are embedded as well as in the fire state.
52
53
00:03:41,700 --> 00:03:45,690
This is important to prevent unusual transitions in the state machine.
53
54
00:03:45,900 --> 00:03:50,550
Before we can run this, I need to set up the entry state in the custom finite state machine.
54
55
00:03:50,550 --> 00:03:53,040
And of course I want it to be IdleState.
55
56
00:03:54,540 --> 00:03:59,430
So the entry state will be running. By the way, if we run the state machine about this, it'll crash.
56
57
00:03:59,610 --> 00:04:02,490
So make sure to actually put this here.
57
58
00:04:03,410 --> 00:04:09,620
If we run this, then of course, the current state is idle state and we entered the idle state.
58
59
00:04:10,460 --> 00:04:15,590
If the enemy is moving, then the variable is_moving will be set up.
59
60
00:04:15,740 --> 00:04:19,010
In this case, I'm just going to set it up by pressing the button.
60
61
00:04:19,880 --> 00:04:24,980
But as you can see, we exited the idle state and we entered the move state.
61
62
00:04:24,980 --> 00:04:27,230
And the current state is of course, the move state.
62
63
00:04:27,860 --> 00:04:29,480
If I put in the firing.
63
64
00:04:30,950 --> 00:04:37,100
As you can see here, the moves state is still going because of the way conditions are created.
64
65
00:04:37,580 --> 00:04:40,820
So it will go to firing only if I disable is_moving.
65
66
00:04:41,270 --> 00:04:47,360
And basically what it does here, it goes back to idle and then checks: is firing online?
66
67
00:04:48,080 --> 00:04:55,610
And as you can see here in the moving so exited move state. Entered idle state exited idle state
67
68
00:04:56,000 --> 00:04:57,590
and entered fire state.
68
69
00:04:57,590 --> 00:04:59,840
And this is how we end up in the fire state.
69
70
00:04:59,930 --> 00:05:06,110
Be careful how you create your transitions because this will affect your whole logic of the finite state
70
71
00:05:06,110 --> 00:05:06,590
machine.
71
72
00:05:06,620 --> 00:05:09,980
Basically, this is all about finite state machines.
72
73
00:05:10,100 --> 00:05:18,530
Now let's find out in the next module how to get an AI from a position A to a position B using pathfinding.
7465
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.