1
00:00:00,000 --> 00:00:02,200
In this video, we'll make the ship boost

2
00:00:02,201 --> 00:00:05,829
when pressing a specific key
for a short amount of time.

3
00:00:05,986 --> 00:00:08,571
This will allow us to learn about signals,

4
00:00:08,572 --> 00:00:10,757
an essential feature of Godot,

5
00:00:10,758 --> 00:00:14,143
and how to make
multiple nodes work together.

6
00:00:14,171 --> 00:00:15,300
Let's get started.

7
00:00:15,657 --> 00:00:19,414
First, we're going to make
the ship increase its speed.

8
00:00:19,586 --> 00:00:21,629
We're going to create a new input action

9
00:00:21,630 --> 00:00:24,657
corresponding to our boost mechanic.

10
00:00:24,800 --> 00:00:26,914
So let's head to the Project menu,

11
00:00:27,086 --> 00:00:30,014
Project Settings and go
to the Input Map tab.

12
00:00:30,015 --> 00:00:33,457
Then click the Action bar
and type "boost".

13
00:00:33,614 --> 00:00:36,314
Press Enter to define
the new input action.

14
00:00:36,571 --> 00:00:38,657
We're going to click the plus sign

15
00:00:38,658 --> 00:00:41,771
to the right of the action
and select Physical Key.

16
00:00:41,929 --> 00:00:44,071
You can map it to whatever you want.

17
00:00:44,114 --> 00:00:46,857
We can go with the spacebar, for example,

18
00:00:47,014 --> 00:00:48,271
and close the window.

19
00:00:49,000 --> 00:00:50,900
That's our input action created.

20
00:00:51,086 --> 00:00:52,986
We can now head back to the script

21
00:00:52,987 --> 00:00:56,371
by clicking the script icon
next to the Sprite node.

22
00:00:56,529 --> 00:00:58,886
This will take us
back to the script editor

23
00:00:59,286 --> 00:01:02,457
and we're going to make the ship boost now

24
00:01:02,458 --> 00:01:04,329
using our new input action.

25
00:01:04,900 --> 00:01:08,286
Before we update the velocity on line 15,

26
00:01:08,287 --> 00:01:12,643
you want to insert a couple
of lines where we'll add a condition.

27
00:01:12,757 --> 00:01:17,429
We want to know if the player
pressed the boost key

28
00:01:17,529 --> 00:01:21,214
and we can use
our input object for that.

29
00:01:21,215 --> 00:01:24,300
So we're going to start
by writing our condition,

30
00:01:24,343 --> 00:01:26,450
"If Input.".

31
00:01:26,543 --> 00:01:28,199
And as I told you, the input object

32
00:01:28,200 --> 00:01:32,943
has various functions related
to inputs that are quite useful.

33
00:01:32,944 --> 00:01:36,800
One that's very useful
in conjunction with the process function

34
00:01:36,801 --> 00:01:40,957
is the "is_action_just pressed" function.

35
00:01:41,186 --> 00:01:43,086
It will validate the condition

36
00:01:43,087 --> 00:01:46,857
if you are just pressing
down the button, this frame.

37
00:01:46,914 --> 00:01:49,271
And want to check
for the boost mechanic.

38
00:01:49,743 --> 00:01:50,857
So this line is like…

39
00:01:50,943 --> 00:01:54,471
If this frame, the player
pressed the boost action,

40
00:01:54,472 --> 00:01:57,686
then we'll run the code
inside the condition.

41
00:01:57,929 --> 00:02:00,586
And the code is going to be
something like max speed,

42
00:02:00,871 --> 00:02:06,386
is going to be equal
to something greater than what it is.

43
00:02:06,443 --> 00:02:07,957
Right now, it's 600.

44
00:02:08,086 --> 00:02:13,071
We could write something
like 1200 or 1500, for example.

45
00:02:14,271 --> 00:02:18,614
But it's not ideal to write
numbers like this in your code

46
00:02:18,615 --> 00:02:21,586
because as you add code,
as you add mechanics,

47
00:02:21,969 --> 00:02:24,414
these values,
you end up duplicating them.

48
00:02:24,471 --> 00:02:26,943
And then every time you want to tweak one,

49
00:02:27,000 --> 00:02:30,857
you have to go to multiple places
and sometimes even multiple scripts

50
00:02:30,943 --> 00:02:32,814
to update the numbers.

51
00:02:32,871 --> 00:02:34,357
We don't want to have to do that.

52
00:02:34,358 --> 00:02:37,986
So we're going to add new
variables at the top of our script.

53
00:02:38,314 --> 00:02:39,614
We're going to add two.

54
00:02:39,657 --> 00:02:42,314
One is going to be the boost speed

55
00:02:42,514 --> 00:02:47,414
and it's going to be this value of 1500.

56
00:02:48,257 --> 00:02:51,686
And a second one is
going to be the normal speed,

57
00:02:51,687 --> 00:02:53,471
the one we'll start with.

58
00:02:53,557 --> 00:02:56,271
You can place your cursor on the line

59
00:02:56,600 --> 00:02:59,100
and press CTR-D to duplicate it.

60
00:02:59,586 --> 00:03:04,386
And then we're going to rename
the new variable, "normal_speed."

61
00:03:04,643 --> 00:03:08,471
And this one is going
to be equal to 600.0,

62
00:03:08,757 --> 00:03:11,286
our starting normal speed for the ship.

63
00:03:11,657 --> 00:03:15,057
Finally, we can replace this number here

64
00:03:15,058 --> 00:03:17,029
and this number there

65
00:03:17,257 --> 00:03:19,286
by our two new variables.

66
00:03:19,287 --> 00:03:24,600
Because variables are just labels
for the values assigned to them.

67
00:03:24,857 --> 00:03:26,657
So we select this "600"

68
00:03:26,658 --> 00:03:29,943
and we're going to replace
this with "normal_speed".

69
00:03:30,443 --> 00:03:33,986
In the process function,
we select our "1500"

70
00:03:34,057 --> 00:03:37,171
and we'll replace this by "boost_speed".

71
00:03:37,457 --> 00:03:42,500
We've made our variables
much easier to tweak later on.

72
00:03:43,130 --> 00:03:46,957
You can now play the game
by pressing F6, and move the ship.

73
00:03:46,958 --> 00:03:48,529
And if you press the spacebar,

74
00:03:48,530 --> 00:03:50,206
the ship will go much faster,

75
00:03:50,207 --> 00:03:51,286
it will boost.

76
00:03:51,457 --> 00:03:52,971
Now we just have one problem,

77
00:03:53,029 --> 00:03:54,557
it never slows down.

78
00:03:54,871 --> 00:03:58,214
And this is where
we're going to introduce signals

79
00:03:58,215 --> 00:04:01,014
and a new node called the Timer node.

80
00:04:01,514 --> 00:04:05,886
Let's head back to the scene by clicking
on the Sprite node in the scene dock

81
00:04:06,176 --> 00:04:10,319
and from there, we're going
to add a new timer node as a child of it.

82
00:04:10,429 --> 00:04:12,200
There are two ways to create node.

83
00:04:12,243 --> 00:04:14,629
You can click the plus
button in the top left

84
00:04:14,671 --> 00:04:18,543
or use the shortcut,
CTRL-A or Command-A.

85
00:04:18,800 --> 00:04:21,757
It's going to add the node as
a child of the selected one.

86
00:04:21,758 --> 00:04:24,257
So make sure that you selected the sprite.

87
00:04:24,543 --> 00:04:26,637
Then let's press Control-A

88
00:04:26,957 --> 00:04:29,614
and we're going
to look for the time in it.

89
00:04:29,943 --> 00:04:31,743
Press Enter to create it

90
00:04:31,871 --> 00:04:36,343
and your Inspector on the right
updates to show the timer's properties.

91
00:04:36,457 --> 00:04:38,329
And we need to change two of these,

92
00:04:38,457 --> 00:04:39,929
Wait Time, and One Shot.

93
00:04:40,643 --> 00:04:44,114
You can get information
about what a property does

94
00:04:44,115 --> 00:04:46,886
by hovering
over its name with the mouse.

95
00:04:47,143 --> 00:04:48,770
The information can be a bit technical,

96
00:04:48,771 --> 00:04:52,900
but the Wait Time is
basically the duration of the timer.

97
00:04:53,271 --> 00:04:54,971
So we're going to lower it a bit.

98
00:04:54,972 --> 00:04:58,614
It's going to control the duration
of our boost mechanic.

99
00:04:58,686 --> 00:05:01,071
When the timer runs out of time,

100
00:05:01,186 --> 00:05:04,586
the ship will stop moving fast.

101
00:05:05,071 --> 00:05:08,386
Then the One Shot
property we want to turn it on

102
00:05:08,971 --> 00:05:12,171
it's going to make the timer run only once

103
00:05:12,314 --> 00:05:16,086
because by default,
a timer cycles infinitely.

104
00:05:16,243 --> 00:05:18,329
We don't want that very boost mechanic.

105
00:05:18,330 --> 00:05:21,129
We want it to start
when pressing the space bar

106
00:05:21,130 --> 00:05:24,800
and to end after 0.6 seconds.

107
00:05:25,757 --> 00:05:27,729
This is where we start to use signals

108
00:05:27,730 --> 00:05:31,629
because after the timer ran out of time,

109
00:05:31,729 --> 00:05:35,729
we want to reset the ship's speed.

110
00:05:35,730 --> 00:05:36,829
How do we do that?

111
00:05:37,186 --> 00:05:39,843
We do that by heading to the node tab.

112
00:05:39,844 --> 00:05:43,143
It's right next to the inspector
so I invite you to click it.

113
00:05:43,457 --> 00:05:48,857
And this one lists some signals
attached to the selected time and node.

114
00:05:49,014 --> 00:05:53,643
A signal is like a message a node
emits when something happens to it.

115
00:05:53,743 --> 00:05:56,029
For example, the timer timing out.

116
00:05:56,030 --> 00:05:59,857
In that case,
will emit the timeout signal.

117
00:06:00,243 --> 00:06:05,543
We can connect that signal
to other nodes with a script in our scene.

118
00:06:05,671 --> 00:06:10,071
So we can connect this
timeout Signal to our Sprite node.

119
00:06:11,000 --> 00:06:15,829
Double click the timeout Signal
to open the connection window.

120
00:06:16,171 --> 00:06:18,557
Then we want to connect
it to the Sprite node.

121
00:06:19,243 --> 00:06:22,929
And here, you have something
important called the Receiver Method.

122
00:06:22,930 --> 00:06:25,086
A method is basically a function.

123
00:06:25,314 --> 00:06:28,100
This is the function that Godot will call

124
00:06:28,101 --> 00:06:30,886
when the timer emits the timeout Signal.

125
00:06:31,000 --> 00:06:34,186
And if the function does not exist
when you connect the signal,

126
00:06:34,214 --> 00:06:35,671
Godot will create it for you.

127
00:06:35,672 --> 00:06:39,743
So we can click
the connect button to create it.

128
00:06:40,157 --> 00:06:43,229
And there you see,
we are back to the script editor

129
00:06:43,657 --> 00:06:45,629
with this new function.

130
00:06:45,957 --> 00:06:49,429
Also, there's an icon
on the left that you can click

131
00:06:49,614 --> 00:06:54,329
to see which signal of which node
is connected to our sprite.

132
00:06:56,000 --> 00:06:58,786
We need to replace the pass keyword

133
00:06:58,787 --> 00:07:02,114
with what we want to happen
when the timer times out.

134
00:07:02,229 --> 00:07:05,943
And what we want then
is to reset the ship speed.

135
00:07:06,086 --> 00:07:07,329
How do we do that?

136
00:07:08,103 --> 00:07:09,500
Try to think about it.

137
00:07:10,214 --> 00:07:17,114
The way we do this is by setting
the max speed back to normal speed.

138
00:07:17,943 --> 00:07:20,757
All right, we need one
last piece of the puzzle.

139
00:07:20,758 --> 00:07:24,729
We need to start the timer
when the ship boosts.

140
00:07:24,786 --> 00:07:26,929
Otherwise, it's just never going to run.

141
00:07:26,930 --> 00:07:30,457
And it's never going
to emit that timeout signal.

142
00:07:30,800 --> 00:07:35,186
So after we change the max
speed in our process function,

143
00:07:35,286 --> 00:07:37,871
we're going to start the timer.

144
00:07:38,057 --> 00:07:39,543
But to start the timer,

145
00:07:39,544 --> 00:07:41,171
we first have to get

146
00:07:41,214 --> 00:07:45,100
a reference to this timer node
we see in our scene dock.

147
00:07:45,357 --> 00:07:50,443
The way we do that is by calling
the get and it's called node function.

148
00:07:50,714 --> 00:07:54,365
This is a function available
on every node in Godot

149
00:07:54,914 --> 00:07:58,914
and it allows you to get
a reference to another node.

150
00:07:59,014 --> 00:08:02,529
And you can see as soon as you open
the parentheses after the function,

151
00:08:02,643 --> 00:08:04,686
you get an auto-completion list,

152
00:08:04,687 --> 00:08:06,386
suggestions from Godot.

153
00:08:06,686 --> 00:08:09,671
We want to use the timer here in quotes

154
00:08:09,672 --> 00:08:13,514
so you can double-click
that to insert the text.

155
00:08:13,871 --> 00:08:19,086
But the function ones
as its argument is a path to a node.

156
00:08:19,087 --> 00:08:24,371
And that path can be relative in
the case of a direct child like the timer.

157
00:08:24,414 --> 00:08:26,743
You can just type the name of the node.

158
00:08:26,800 --> 00:08:28,659
Now, very important,

159
00:08:29,171 --> 00:08:30,586
what you have in the quote

160
00:08:30,614 --> 00:08:34,443
is the actual display name
of the node in your scene.

161
00:08:34,444 --> 00:08:37,756
So if I change
the timer's name to "MyTimer"

162
00:08:37,757 --> 00:08:39,829
by double-clicking it in the scene,

163
00:08:39,943 --> 00:08:45,600
then to get that timer node,
I have to type the name MyTimer.

164
00:08:45,971 --> 00:08:47,977
The reason it's like this

165
00:08:48,029 --> 00:08:48,729
is because…

166
00:08:48,730 --> 00:08:51,343
Well, you can have
multiple timers in your scene.

167
00:08:51,471 --> 00:08:54,257
To illustrate that,
I'm going to duplicate the timer

168
00:08:54,258 --> 00:08:57,086
and you can see,
I have now four of them, right?

169
00:08:57,500 --> 00:08:59,186
But for now we only need one.

170
00:08:59,187 --> 00:09:02,614
So I'm going to head back
to the script after deleting the node.

171
00:09:03,014 --> 00:09:05,885
And so once we call the get node function,

172
00:09:05,886 --> 00:09:09,057
we get the reference
to the node and we can directly

173
00:09:09,229 --> 00:09:13,057
access the properties
and functions of our timer.

174
00:09:13,058 --> 00:09:15,071
To do that, we can type a dot

175
00:09:15,072 --> 00:09:18,114
and then call the start
function of our timer.

176
00:09:18,857 --> 00:09:23,857
Now, this line will start the timer
when we press the boost in protection.

177
00:09:24,014 --> 00:09:26,229
And after 0.6 seconds,

178
00:09:26,314 --> 00:09:28,900
Godot will call
on_Timer_timeout,

179
00:09:29,171 --> 00:09:32,043
causing the ship's speed
to go back down.

180
00:09:32,171 --> 00:09:35,500
You can press F6 to try and move the ship,

181
00:09:35,614 --> 00:09:37,257
press space to accelerate.

182
00:09:37,371 --> 00:09:41,529
And then after a short amount of time,
you will see the ship slow down.

183
00:09:42,171 --> 00:09:45,971
In the next lesson,
you'll be making the ship move smoothly

184
00:09:46,071 --> 00:09:48,629
using a technique called steering.