1 00:00:00,200 --> 00:00:05,420 In this lesson I will run you through the starting code of the bullets 2 00:00:05,450 --> 00:00:09,114 that we have in the game and the spells. 3 00:00:09,566 --> 00:00:11,093 In the test room if you play, 4 00:00:11,094 --> 00:00:13,540 you will see hands that turn around the character. 5 00:00:13,570 --> 00:00:15,900 We call that a spell in the game, 6 00:00:15,901 --> 00:00:20,386 it's a tool to fire some bullets or anything really. 7 00:00:20,920 --> 00:00:26,814 But if you click with the first spell you will see some low speed fireballs 8 00:00:26,815 --> 00:00:31,143 that go hit the walls and they can hit the enemies as well. 9 00:00:31,240 --> 00:00:33,086 These are the bullets. 10 00:00:33,290 --> 00:00:36,529 I invite you to expand the bullets directory 11 00:00:36,530 --> 00:00:38,372 and open the bullet script. 12 00:00:38,770 --> 00:00:42,186 Now the reason we have the bullets separate from the spells 13 00:00:42,187 --> 00:00:43,815 and they have their own folder 14 00:00:43,816 --> 00:00:47,343 is that the monsters can use the bullets as well differently 15 00:00:47,344 --> 00:00:49,600 like they can have cannons, things like these. 16 00:00:49,729 --> 00:00:51,428 We use them in different contexts. 17 00:00:51,429 --> 00:00:54,899 While the spells are really the hands that attach to the player. 18 00:00:54,986 --> 00:00:58,357 The bullet has a few basic properties, 19 00:00:59,001 --> 00:01:01,860 a speed, amount of damage, and the max range. 20 00:01:01,890 --> 00:01:04,299 If the bullet travels too far we destroy it 21 00:01:04,499 --> 00:01:05,867 as you will see in a second. 22 00:01:05,929 --> 00:01:07,829 This is really just a base script 23 00:01:07,830 --> 00:01:10,871 that you will extend to create other bullets. 24 00:01:10,960 --> 00:01:15,464 It has some default behavior and then you can override and extend it 25 00:01:15,465 --> 00:01:17,436 in each of the different bullets. 26 00:01:17,650 --> 00:01:19,620 This is just a script as well. 27 00:01:19,650 --> 00:01:22,020 I have to mention this because what we do 28 00:01:22,050 --> 00:01:26,698 in the ready function is that we ensure that you put the bullets in a scene 29 00:01:26,699 --> 00:01:29,228 that has an AudioStreamPlayer2D node 30 00:01:29,229 --> 00:01:30,785 just so that it can play. 31 00:01:31,200 --> 00:01:33,700 We didn't create a scene that you will inherit 32 00:01:33,701 --> 00:01:37,529 because there's not much to put in that scene. 33 00:01:37,960 --> 00:01:41,942 Every frame we call the move function of the bullet 34 00:01:41,943 --> 00:01:44,820 and this is something I wanted to highlight. 35 00:01:44,821 --> 00:01:49,634 We created some basic functions in the bullet that get called, 36 00:01:49,869 --> 00:01:52,657 like in this case we don't put the code directly 37 00:01:52,658 --> 00:01:56,028 in physics process just so that if you want your bullet 38 00:01:56,029 --> 00:01:58,557 to move differently from the base one, 39 00:01:58,558 --> 00:02:02,000 you can do that by overriding the move function. 40 00:02:02,690 --> 00:02:05,968 By default the bullet moves in a straight line, 41 00:02:06,168 --> 00:02:10,780 so it's going to use, it's transform.x multiplied by speed times delta. 42 00:02:10,810 --> 00:02:14,800 This is the same as the bullets in the tower defense series, 43 00:02:14,801 --> 00:02:19,500 and then we increase or we add that to a current position 44 00:02:19,501 --> 00:02:21,757 making the bullet move forward. 45 00:02:22,330 --> 00:02:26,328 We also calculate the travel distance every frame 46 00:02:26,528 --> 00:02:31,471 and if the total travel distance is greater than the maximum range, 47 00:02:31,472 --> 00:02:34,657 we destroy the bullet, calling the destroy function. 48 00:02:35,050 --> 00:02:39,129 The destroy function is just going to queue for the bullet 49 00:02:39,728 --> 00:02:45,334 and so there again you can override it if you want to have an explosion animation 50 00:02:45,534 --> 00:02:47,180 or something else in there, 51 00:02:47,210 --> 00:02:50,686 if you don't want the bullet to disappear instantly. 52 00:02:50,850 --> 00:02:56,612 It also has a hit body function that by default is just going to try 53 00:02:56,629 --> 00:02:58,714 to damage the thing it hit. 54 00:02:58,930 --> 00:03:03,499 Now you can see that we ensure that the thing that we hit 55 00:03:03,614 --> 00:03:05,657 has a function called TakeDamage. 56 00:03:05,770 --> 00:03:09,550 This makes it so an enemy can fire a bullet to the player 57 00:03:09,551 --> 00:03:14,264 and the player can fire a bullet to the enemy or to a prop 58 00:03:14,265 --> 00:03:19,035 as long as that thing has a function defined called TakeDamage 59 00:03:19,257 --> 00:03:21,014 that you code in a script. 60 00:03:21,250 --> 00:03:26,540 You can call that TakeDamage function and you don't care what you hit. 61 00:03:26,570 --> 00:03:29,571 This is a very convenient way to make code work 62 00:03:29,572 --> 00:03:34,142 with a number of different nodes in your game. 63 00:03:35,603 --> 00:03:38,443 Okay, at the end you have the disable function 64 00:03:38,444 --> 00:03:42,257 you can call to disable the physics on the bullet. 65 00:03:42,530 --> 00:03:46,300 Finally you have the on body entered callback. 66 00:03:46,330 --> 00:03:51,515 This bullet script is meant to attach to area 2D nodes, 67 00:03:51,516 --> 00:03:55,843 so you can use that body entered signal to know when you hit something 68 00:03:55,844 --> 00:03:59,557 and call hit body to damage the thing that you hit 69 00:03:59,743 --> 00:04:03,560 and then it will automatically destroy the bullet by default. 70 00:04:03,590 --> 00:04:08,029 But there again, if you wanted you could override this function 71 00:04:08,030 --> 00:04:09,500 in an inherited class. 72 00:04:10,130 --> 00:04:14,543 Now we have a concrete example of how to use this bullet here 73 00:04:14,544 --> 00:04:16,086 with the fireball scene. 74 00:04:16,410 --> 00:04:19,943 I'm going to open it and there you can see that we put 75 00:04:19,944 --> 00:04:21,356 a couple of things in there. 76 00:04:21,357 --> 00:04:23,657 The most important to use the bullet script 77 00:04:23,658 --> 00:04:27,243 is that the root node to which you attach Bullet.gd 78 00:04:27,371 --> 00:04:31,186 or you extend the script, has to be an area 2D. 79 00:04:31,831 --> 00:04:34,980 Then it needs to have an audio stream player 80 00:04:35,010 --> 00:04:39,100 and that will be your bullet sound when it hits something. 81 00:04:40,840 --> 00:04:43,500 Then you can put anything you want in there. 82 00:04:43,530 --> 00:04:48,529 We created some animation to make the bullet appear when shooting it. 83 00:04:49,047 --> 00:04:51,946 We have the collision shape, we have a trail sprite 84 00:04:51,947 --> 00:04:53,438 that goes behind the bullet, 85 00:04:53,638 --> 00:04:56,420 and we have some particles for when it hits something. 86 00:04:56,471 --> 00:04:58,914 Let's open the script to see that. 87 00:04:58,915 --> 00:05:00,713 It's just a couple of lines of code. 88 00:05:00,714 --> 00:05:03,056 The first one is we extend bullet, 89 00:05:03,057 --> 00:05:06,071 so we get the default motion and everything else 90 00:05:06,274 --> 00:05:10,557 and then we add a bit of code to play the animation 91 00:05:10,943 --> 00:05:14,986 and use the particles that are not used in here, 92 00:05:14,987 --> 00:05:16,900 so we can remove that line. 93 00:05:17,280 --> 00:05:21,807 You can see how in this script we override the destroy function 94 00:05:22,007 --> 00:05:23,460 from the bullet script. 95 00:05:23,480 --> 00:05:28,357 We replace it entirely because we want to play the audio 96 00:05:28,358 --> 00:05:30,357 and our destroy animation. 97 00:05:30,770 --> 00:05:34,528 That's an example of how you can replace the destroy function, 98 00:05:34,529 --> 00:05:36,586 but you could do that with the move function 99 00:05:36,587 --> 00:05:40,557 to change how the bullet moves and make very different bullets. 100 00:05:40,730 --> 00:05:42,957 Okay, next is the spell. 101 00:05:43,130 --> 00:05:48,329 I'm going to fold the bullets folder and expand the spells folder 102 00:05:48,330 --> 00:05:50,586 and open spell.tsen. 103 00:05:50,890 --> 00:05:55,714 In the case of the spell, you have both a script and a base scene 104 00:05:55,715 --> 00:05:57,686 that you want to inherit from. 105 00:05:57,970 --> 00:06:01,540 Right now we're going to look at the spell base scene. 106 00:06:01,570 --> 00:06:05,540 It has two hands sprite, it has a cool down timer. 107 00:06:05,570 --> 00:06:08,113 This is the cool down between shooting bullets 108 00:06:08,313 --> 00:06:10,228 so that you can reuse the same bullet 109 00:06:10,243 --> 00:06:14,329 on different spells, but with a different cooldown time. 110 00:06:14,700 --> 00:06:21,800 This is going to make it so the spell, the weapon fires faster or slower. 111 00:06:21,920 --> 00:06:27,220 We also have an audio stream player which we use as the shooting sound 112 00:06:27,250 --> 00:06:30,800 when we emit a bullet from the spell. 113 00:06:30,920 --> 00:06:33,729 Finally, let's look at the script. 114 00:06:34,090 --> 00:06:39,820 This one is going to fire bullets at a specific fire rate. 115 00:06:39,850 --> 00:06:43,757 We use that to change the timers cool down time. 116 00:06:43,890 --> 00:06:50,386 It has a few properties that we use to randomize the bullets a little bit. 117 00:06:50,685 --> 00:06:55,256 We have a random angle in the grids that we apply to the bullets 118 00:06:55,257 --> 00:06:57,900 so that they don't always shoot straight. 119 00:06:57,920 --> 00:07:00,596 If I go back to the test room and fire, 120 00:07:00,796 --> 00:07:04,299 you can see that the bullets have a slight angle offset 121 00:07:04,329 --> 00:07:05,529 every time they shoot. 122 00:07:05,730 --> 00:07:10,157 This is to control the accuracy of the gun. 123 00:07:10,229 --> 00:07:11,986 There again you have a maximum range 124 00:07:11,987 --> 00:07:15,243 that can override the bullet maximum range, 125 00:07:15,690 --> 00:07:19,388 and you can control the speed of the bullets as well 126 00:07:19,543 --> 00:07:21,857 using the max bullet speed property. 127 00:07:21,920 --> 00:07:27,423 All right, so in the ready function we change the call down timers wait time 128 00:07:27,529 --> 00:07:29,643 based on the fire rate property. 129 00:07:29,770 --> 00:07:32,471 This makes it easier to control the spell 130 00:07:32,943 --> 00:07:35,286 because we export those variables. 131 00:07:35,410 --> 00:07:39,420 If you select the spell in your scene, you can change the fire rate 132 00:07:39,450 --> 00:07:41,514 directly in the inspector. 133 00:07:41,571 --> 00:07:44,871 Then the main function of the spell is shoot. 134 00:07:44,920 --> 00:07:46,671 It creates a new bullet, 135 00:07:46,890 --> 00:07:51,600 we place the bullet as a child of the scene trees root node. 136 00:07:51,669 --> 00:07:54,854 This makes it so it won't be a child of the spell, 137 00:07:55,054 --> 00:07:57,020 but move completely independently. 138 00:07:57,040 --> 00:07:58,660 That's one way of doing that. 139 00:07:58,690 --> 00:08:04,340 Then we set the bullets global transform to match the spell, 140 00:08:04,360 --> 00:08:09,740 so we are going to move and rotate it to match this spell node, 141 00:08:09,770 --> 00:08:12,020 the hands of the character. 142 00:08:12,040 --> 00:08:15,057 Then we set the max range and speed of the bullet 143 00:08:15,058 --> 00:08:17,729 and we randomize the rotation. 144 00:08:17,730 --> 00:08:21,573 We created the randomized rotation on the bullet directly 145 00:08:21,574 --> 00:08:24,345 to which you can pass an angle and radiance, 146 00:08:24,571 --> 00:08:26,071 that's what we are doing here 147 00:08:26,072 --> 00:08:28,628 and that will rotate the bullet a little bit. 148 00:08:29,130 --> 00:08:34,620 Finally, we play the shooting audio and this is how the spell works. 149 00:08:34,650 --> 00:08:38,207 Now there again you have a shoot function 150 00:08:38,208 --> 00:08:41,886 defined on the spell so you can override it 151 00:08:41,887 --> 00:08:45,635 in scenes and scripts that extend the spell 152 00:08:45,636 --> 00:08:48,780 to completely change the way you shoot bullets. 153 00:08:48,810 --> 00:08:51,471 For example, you could make a function 154 00:08:51,472 --> 00:08:54,429 that is going to shoot multiple bullets at a time, 155 00:08:54,430 --> 00:08:58,200 creating some shotgun spell. 156 00:08:58,560 --> 00:09:01,380 You will do that later in the series. 157 00:09:01,410 --> 00:09:05,529 But an example of how to use the spell scene and script, 158 00:09:05,530 --> 00:09:08,171 you have the fire basic spell. 159 00:09:08,290 --> 00:09:11,505 You can open the scene to see that it inherits 160 00:09:11,705 --> 00:09:15,100 from the spell scene and we did not modify anything. 161 00:09:15,170 --> 00:09:19,305 The corresponding script extends the base spell script 162 00:09:19,505 --> 00:09:25,114 and what we have here is just code to make the spell shoot. 163 00:09:25,541 --> 00:09:28,946 We don't have that code in the parent spell script 164 00:09:28,947 --> 00:09:32,817 because you may want different spells to handle input differently. 165 00:09:32,818 --> 00:09:38,274 This one shoots automatically whenever the cool down time is stopped. 166 00:09:38,530 --> 00:09:40,872 But you might want to have some spells 167 00:09:40,873 --> 00:09:44,543 where you have to click once to shoot the spell. 168 00:09:45,286 --> 00:09:47,769 In that case you will not use physics process, 169 00:09:47,770 --> 00:09:50,414 but you would use an input function. 170 00:09:50,530 --> 00:09:54,586 This automates the shooting as long as you press the key down. 171 00:09:55,847 --> 00:10:00,780 Lastly, we use the cool down timer in that inherited scene. 172 00:10:00,810 --> 00:10:04,783 There again we don't start the timer in the parent spell script 173 00:10:04,814 --> 00:10:09,486 because you might want to handle things differently in different spells. 174 00:10:09,690 --> 00:10:12,731 Say you have a spell that has only three uses 175 00:10:12,732 --> 00:10:15,976 and you have to kill an enemy to regain a use, 176 00:10:16,414 --> 00:10:19,343 then you wouldn't want to use the cool down timer. 177 00:10:19,699 --> 00:10:23,780 With that we went through all the important base script 178 00:10:23,810 --> 00:10:26,260 to get you started in this series. 179 00:10:26,290 --> 00:10:30,560 In the next part you will start coding your own bullet and spell.