All language subtitles for 004 Limiting the Amount of Shots[UdemyIran.Com]

af Afrikaans
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bn Bengali
bs Bosnian
bg Bulgarian
ca Catalan
ceb Cebuano
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
tl Filipino
fi Finnish
fr French
fy Frisian
gl Galician
ka Georgian
de German
el Greek
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
km Khmer
ko Korean
ku Kurdish (Kurmanji)
ky Kyrgyz
lo Lao
la Latin
lv Latvian
lt Lithuanian
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mn Mongolian
my Myanmar (Burmese)
ne Nepali
no Norwegian
ps Pashto
fa Persian Download
pl Polish
pt Portuguese
pa Punjabi
ro Romanian
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
st Sesotho
sn Shona
sd Sindhi
si Sinhala
sk Slovak
sl Slovenian
so Somali
es Spanish
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
te Telugu
th Thai
tr Turkish
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
or Odia (Oriya)
rw Kinyarwanda
tk Turkmen
tt Tatar
ug Uyghur
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 1 00:00:00,080 --> 00:00:05,600 In this episode, we want to limit the player's shots and this has multiple implications. 2 00:00:05,600 --> 00:00:10,010 One of the reasons is that we want to naturally manage the cooldown of the shooting, without having 3 00:00:10,010 --> 00:00:12,020 to putting in an artificial delay. 4 00:00:12,380 --> 00:00:16,910 It's also good for risk reward because we're going to be able to shoot faster the closer we are to the 5 00:00:16,910 --> 00:00:19,400 edge of the screen, or the closer we are to enemies. 6 00:00:19,400 --> 00:00:24,110 And it's also good for performance because if I just keep on shooting, I'm just going to keep on spawning 7 00:00:24,110 --> 00:00:25,220 more and more projectiles. 8 00:00:25,220 --> 00:00:30,560 You can see on the right side of the screen it says 55 actors now, and I can just keep on shooting 9 00:00:30,560 --> 00:00:32,659 and they never become less. 10 00:00:32,659 --> 00:00:34,070 They just keep on increasing. 11 00:00:34,070 --> 00:00:40,550 And if I eject, you can see that the projectiles are still active, they're still flying around, they 12 00:00:40,550 --> 00:00:44,420 are flying around out of the screen, like where nobody actually cares about them anymore. 13 00:00:44,420 --> 00:00:48,140 So this is another reason why we want to get rid of them. 14 00:00:48,140 --> 00:00:50,660 And I'm going to show you all of that in this video. 15 00:00:51,320 --> 00:00:56,660 The despawning of projectiles will be handled differently between the enemies and the player. 16 00:00:56,660 --> 00:01:01,010 Only for the player do we want despawn them once they leave the screen for the enemies. 17 00:01:01,010 --> 00:01:05,269 It's just going to be a simple timer, but now we're just going to do it for the player first. 18 00:01:05,269 --> 00:01:11,780 So in the content blueprints projectiles, we don't want to use the projectile base. 19 00:01:11,780 --> 00:01:18,020 We want to go to player and use the player projectile base, not the regular the base. 20 00:01:18,020 --> 00:01:18,800 Open it up. 21 00:01:19,360 --> 00:01:21,190 And drag it up here. 22 00:01:21,190 --> 00:01:23,710 We can close down some of these other windows. 23 00:01:23,710 --> 00:01:25,300 We're not going to use them right now. 24 00:01:25,450 --> 00:01:28,150 And make sure to open the full blueprint editor here. 25 00:01:29,300 --> 00:01:34,940 So what we want to do here now is that on tick we basically want to check on every frame if this projectile 26 00:01:34,940 --> 00:01:39,770 is still inside of the screen or if it already left the screen, if it already left the screen, we 27 00:01:39,770 --> 00:01:41,030 just want to despawn it. 28 00:01:41,730 --> 00:01:46,350 First of all, you want to make sure that the event tick also calls the parent function. 29 00:01:46,350 --> 00:01:48,330 So add call to parent function. 30 00:01:48,480 --> 00:01:52,770 We don't really have anything inside of the BP projectile base, right. 31 00:01:52,770 --> 00:01:54,300 We can open this up as well. 32 00:01:54,300 --> 00:01:57,420 And we don't have anything on the tick event yet. 33 00:01:57,420 --> 00:02:02,850 But one thing that can happen if we have the inheritance structure that we always have, if you only 34 00:02:02,850 --> 00:02:07,680 call tick here and you don't call the parent tick, then if we have anything in here, it's just going 35 00:02:07,680 --> 00:02:08,370 to be ignored. 36 00:02:08,370 --> 00:02:09,930 And this is often the cause of bugs. 37 00:02:09,930 --> 00:02:12,510 And you're going to be like, why does this not work? 38 00:02:12,510 --> 00:02:17,280 So if we have a child class, we always just want to make sure that we call the parent tick. 39 00:02:17,280 --> 00:02:19,140 And the same also goes for Beginplay. 40 00:02:19,140 --> 00:02:25,260 If we were to use Beginplay, we also wanted to use the call to parent function first, because otherwise 41 00:02:25,260 --> 00:02:29,790 again, we might miss out on some initialization steps that we need in the parent. 42 00:02:29,790 --> 00:02:33,750 But yeah, just make sure that we have the parent call here. 43 00:02:33,750 --> 00:02:37,200 And now we can use the tick event which fires on every frame. 44 00:02:37,810 --> 00:02:41,530 The first thing we want is get player controller. 45 00:02:42,670 --> 00:02:45,880 And there's only one player, so we just want to get it off zero. 46 00:02:45,880 --> 00:02:49,450 And actually for this function, it doesn't really matter which player it is anyway. 47 00:02:49,450 --> 00:02:56,140 And here we can just convert world location to screen location. 48 00:02:56,960 --> 00:02:59,360 And this just lets us find a location in the world. 49 00:02:59,360 --> 00:03:03,590 And then it tells us on the screen in x, y coordinates where it is located. 50 00:03:03,590 --> 00:03:08,060 And this is very useful for finding if something is inside the screen or outside the screen. 51 00:03:09,030 --> 00:03:13,290 For now, we just want to print string this so we can just go here print string. 52 00:03:14,710 --> 00:03:16,270 And what location do we want? 53 00:03:16,270 --> 00:03:19,540 Well, of course the Get Actor location. 54 00:03:20,170 --> 00:03:21,850 Of this projectile. 55 00:03:24,240 --> 00:03:26,640 So this is actually not related to the player controller. 56 00:03:26,640 --> 00:03:32,100 The player controller is only needed because this function is located on it, and it kind of like tunnels 57 00:03:32,100 --> 00:03:33,510 through the player controller. 58 00:03:33,510 --> 00:03:39,210 But yeah, we care about the location of this projectile and we can then just get the screen location, 59 00:03:39,210 --> 00:03:42,930 drag it in here, it's going to be converted to a string can combine save. 60 00:03:43,540 --> 00:03:47,860 And I can shoot here and you can see it just keeps on moving to the left more and more. 61 00:03:47,860 --> 00:03:48,790 And I shoot this one. 62 00:03:48,790 --> 00:03:50,680 It keeps moving to the right more and more. 63 00:03:50,710 --> 00:03:55,810 Now we can just use these numbers to see if the projectile is actually inside of our viewport or not. 64 00:03:55,810 --> 00:03:59,470 So again, in the player projectile base we can now get rid of this print string. 65 00:03:59,470 --> 00:04:03,580 We now understand that we get the x and y coordinates here in relation to our screen. 66 00:04:04,190 --> 00:04:08,630 But in this case, actually we don't need to check for up and down so we can split struct pin. 67 00:04:08,630 --> 00:04:10,520 We only care about left and right. 68 00:04:10,520 --> 00:04:13,880 We are not going to care about deleting the projectile when going up and down. 69 00:04:13,880 --> 00:04:16,130 So we only care about the X actually. 70 00:04:17,279 --> 00:04:20,790 So now that we've simplified this a bit, let's just print string again. 71 00:04:22,640 --> 00:04:24,350 And only the X. 72 00:04:25,120 --> 00:04:31,210 Because this is going to show you that if we go like this and I shoot left, the projectile leaves the 73 00:04:31,210 --> 00:04:36,010 screen exactly when the value is zero. 74 00:04:37,100 --> 00:04:37,640 Right. 75 00:04:37,640 --> 00:04:40,730 So this is where the viewport starts in relation to this. 76 00:04:40,730 --> 00:04:42,170 So screen location. 77 00:04:43,420 --> 00:04:48,910 So this screen location starts at the left side here and then extends here. 78 00:04:48,940 --> 00:04:49,180 Right. 79 00:04:49,180 --> 00:04:54,400 So if we know that the screen location is zero, it means that it just left the screen or it's just 80 00:04:54,400 --> 00:04:55,900 coming inside of the screen. 81 00:04:55,930 --> 00:04:58,060 So this is our first condition. 82 00:04:58,060 --> 00:05:02,800 We can just check if the screen location is smaller or equal than zero. 83 00:05:02,830 --> 00:05:05,740 Well then we know that we want to destroy the projectile. 84 00:05:06,390 --> 00:05:07,740 So we can just add this here. 85 00:05:07,740 --> 00:05:09,450 Just destroy actor. 86 00:05:10,070 --> 00:05:11,000 Like this. 87 00:05:11,700 --> 00:05:15,000 And create a branch as well. 88 00:05:15,330 --> 00:05:15,750 Right. 89 00:05:15,750 --> 00:05:21,300 So if this is true, if the screen location is smaller or equal than zero, well we just want to destroy 90 00:05:21,300 --> 00:05:22,050 the actor. 91 00:05:22,050 --> 00:05:23,370 So let's just try this out. 92 00:05:23,370 --> 00:05:24,390 Compile and save. 93 00:05:24,810 --> 00:05:28,860 Go here I shoot to the left and you can see that it is destroyed. 94 00:05:28,860 --> 00:05:33,420 We actually also play the particle effect, which we don't want, but that's something we're going to 95 00:05:33,420 --> 00:05:34,290 take care of later. 96 00:05:34,290 --> 00:05:37,200 But yeah, you can see it is actually this simple. 97 00:05:37,200 --> 00:05:41,970 But the right side is a little bit more complicated because we actually need to know how big our viewport 98 00:05:41,970 --> 00:05:43,920 is to despawn on the right side. 99 00:05:44,160 --> 00:05:49,530 So back in here we then want to check for another condition. 100 00:05:49,530 --> 00:05:57,450 And to do this we need to get viewport size which is again just a nice function that Unreal Engine gives 101 00:05:57,450 --> 00:05:57,690 us. 102 00:05:57,690 --> 00:06:02,520 And we can split struct pin to only get the x because we don't care about the Y. 103 00:06:02,520 --> 00:06:07,590 And what we can do now is to check if the screen location is bigger or equal. 104 00:06:08,470 --> 00:06:10,690 Then the viewport size. 105 00:06:12,200 --> 00:06:13,820 And this is our second condition. 106 00:06:13,820 --> 00:06:16,760 If it's on the left side, we just check for smaller equal than zero. 107 00:06:16,760 --> 00:06:21,230 And on the right side, if it's bigger than the viewport size, we know it's outside of the screen. 108 00:06:21,230 --> 00:06:24,110 So here we want to use an or boolean. 109 00:06:25,510 --> 00:06:31,780 So if this is true, or if this is true, then we want to destroy the actor. 110 00:06:32,560 --> 00:06:33,700 Either or. 111 00:06:34,480 --> 00:06:34,900 Yeah. 112 00:06:34,900 --> 00:06:38,620 If either or is true, we just do this and we don't need the print string anymore. 113 00:06:38,620 --> 00:06:43,510 We can just get rid of this and connect this here again compile and save. 114 00:06:43,990 --> 00:06:45,910 And now left side still works. 115 00:06:45,910 --> 00:06:47,710 Right side also works. 116 00:06:47,710 --> 00:06:55,060 And the cool thing is if I hit shift and F1 and I drag this viewport smaller and click back in again. 117 00:06:55,630 --> 00:06:57,040 It also still works, right? 118 00:06:57,040 --> 00:07:01,690 Because it actually uses the dynamic values of our screen size. 119 00:07:02,080 --> 00:07:07,600 So this would also work for mobile games and games that have weird screen sizes and just make this bigger 120 00:07:07,600 --> 00:07:08,230 again. 121 00:07:09,580 --> 00:07:15,010 But now we want to take care of this issue, that if the bullet leaves the screen, we play this VFX, 122 00:07:15,010 --> 00:07:16,360 which we don't want. 123 00:07:16,360 --> 00:07:17,650 And why does this happen? 124 00:07:17,650 --> 00:07:25,360 Because not in the player projectile base, but in the overarching parent, the BP projectile base on 125 00:07:25,360 --> 00:07:31,810 the um event destroyed, we play this particle effect, and later on we're also going to play a sound 126 00:07:31,810 --> 00:07:32,290 effect. 127 00:07:32,290 --> 00:07:36,190 So I actually just want to collapse this to a function to make it easier. 128 00:07:36,190 --> 00:07:40,420 So we can just select all of this right click and collapse to function. 129 00:07:40,420 --> 00:07:44,470 And I'm just going to call this play Hit Effects. 130 00:07:45,470 --> 00:07:46,430 Like this. 131 00:07:47,350 --> 00:07:49,360 And why did I put this on event destroyed? 132 00:07:49,360 --> 00:07:55,660 Well, I just wanted to show you this problem that can arise if we have multiple levels of inheritance. 133 00:07:55,660 --> 00:07:57,640 And in this case, this was the right choice. 134 00:07:57,640 --> 00:08:01,300 But now this has a bad effect on our player projectile. 135 00:08:01,300 --> 00:08:06,760 And well, there's one way we could fix this is if we just have the on destroyed. 136 00:08:06,760 --> 00:08:08,560 Actually, no, it's just called destroyed. 137 00:08:09,450 --> 00:08:10,290 Destroyed. 138 00:08:10,290 --> 00:08:10,770 Event. 139 00:08:10,800 --> 00:08:11,940 Event destroyed. 140 00:08:11,940 --> 00:08:13,650 And just put nothing here. 141 00:08:14,800 --> 00:08:16,540 And this might surprise you. 142 00:08:16,720 --> 00:08:20,980 I just do this and hey, it is not playing the effect. 143 00:08:20,980 --> 00:08:29,170 And this is because if we have the event destroyed in the BP projectile base, we can actually override 144 00:08:29,170 --> 00:08:30,190 it in the child. 145 00:08:30,190 --> 00:08:33,580 So this child will just say we have this event destroyed. 146 00:08:33,580 --> 00:08:34,840 I just want to ignore the parent. 147 00:08:34,840 --> 00:08:35,980 Just do what I tell you. 148 00:08:35,980 --> 00:08:37,480 And we tell it to do nothing. 149 00:08:37,480 --> 00:08:41,140 What we could do here again is add call to parent function. 150 00:08:41,140 --> 00:08:43,659 And we could, for example, do something else here, right? 151 00:08:43,659 --> 00:08:46,780 We could print and just say like destroyed. 152 00:08:47,480 --> 00:08:52,280 And kind of do our own thing, but then also tell the parent to, hey, I still want you to execute 153 00:08:52,280 --> 00:08:53,180 this as well. 154 00:08:53,180 --> 00:08:55,040 And this is just for demonstration, right? 155 00:08:55,040 --> 00:08:56,570 So you can see the destroyed. 156 00:08:57,800 --> 00:08:59,990 And you can also see the effect play. 157 00:08:59,990 --> 00:09:02,990 And this is just something cool you can do with inheritance. 158 00:09:02,990 --> 00:09:04,460 And I just wanted to show you this. 159 00:09:04,460 --> 00:09:04,700 Right. 160 00:09:04,700 --> 00:09:07,640 So this gives you full control of what you actually want to do. 161 00:09:07,640 --> 00:09:13,430 But in this case, I think the better solution is to just go back to the BP projectile base, which 162 00:09:13,430 --> 00:09:18,200 is the overarching parent, and we just get rid of the event destroyed, and we're just going to play 163 00:09:18,200 --> 00:09:21,590 the hit effect like we did before, before we destroy the actor. 164 00:09:22,300 --> 00:09:23,950 And I think this just makes more sense. 165 00:09:23,950 --> 00:09:29,680 But I wanted to show you the other way so you can kind of see what options you have available as well, 166 00:09:29,680 --> 00:09:31,540 and just compile and save this one. 167 00:09:31,540 --> 00:09:33,430 Compile and save and just to make sure. 168 00:09:34,040 --> 00:09:37,610 The projectiles are being despawned you can see the actor count on the right. 169 00:09:38,410 --> 00:09:43,750 But we don't have this effect playing because it just would be weird if it plays every time we leave 170 00:09:43,750 --> 00:09:44,440 the screen. 171 00:09:44,620 --> 00:09:49,570 The next thing we have to tackle is the limit of three projectiles on the screen at once. 172 00:09:49,570 --> 00:09:51,640 But actually there is an exception to this. 173 00:09:51,640 --> 00:09:53,800 And later on we're going to implement the charge shot. 174 00:09:53,800 --> 00:09:58,210 And if you have a charge shot active on the screen, you actually cannot shoot any other bullets anymore. 175 00:09:58,240 --> 00:10:02,710 They can only be one charge shot or three regular shots active at the same time. 176 00:10:02,710 --> 00:10:05,950 The partially charged shot also just counts as a regular shot. 177 00:10:05,950 --> 00:10:11,170 So we want to build this system with that in mind instead of just hard coding it to three projectiles. 178 00:10:11,740 --> 00:10:17,230 And the solution I came up with is to create something on the player called shot energy. 179 00:10:17,230 --> 00:10:22,300 And shooting a regular projectile is going to use one energy, and it's also going to restore one energy 180 00:10:22,300 --> 00:10:23,260 if it's destroyed. 181 00:10:23,260 --> 00:10:28,390 And shooting a charged projectile is going to use three energy, and it's also going to restore three 182 00:10:28,390 --> 00:10:29,140 energy. 183 00:10:29,140 --> 00:10:33,610 So we can just go here and close down the projectiles. 184 00:10:33,610 --> 00:10:38,860 Go back to the blueprints characters player and the BP player. 185 00:10:38,980 --> 00:10:41,410 And in here, yeah, this is the player. 186 00:10:41,410 --> 00:10:47,260 Just so you know, in the variables I want to add a new one and it's called shot energy. 187 00:10:47,560 --> 00:10:52,960 This needs to be an integer because it's just a regular number and compile and save. 188 00:10:52,960 --> 00:10:54,820 So we can see the default value. 189 00:10:54,820 --> 00:10:57,820 And our default shot energy should be three. 190 00:10:59,970 --> 00:11:04,320 This lets us shoot three regular projectiles or one charged projectile. 191 00:11:04,740 --> 00:11:11,070 Now back in the event graph where we shoot right before we do all of this, before we set to shooting 192 00:11:11,070 --> 00:11:15,390 and before we spawn the projectile, we want to put a branch in here. 193 00:11:15,390 --> 00:11:20,370 So started we just branch again very important that it started not triggered. 194 00:11:20,890 --> 00:11:27,760 And here we want to check if shot energy is bigger than zero. 195 00:11:28,180 --> 00:11:30,340 Only then do we allow shooting. 196 00:11:31,760 --> 00:11:37,520 But this is not enough yet because we don't have anything that uses because we don't have anything that 197 00:11:37,520 --> 00:11:39,650 actually depletes our shot energy. 198 00:11:40,660 --> 00:11:44,110 Now, if all of this is true before we start shooting. 199 00:11:44,690 --> 00:11:46,550 Again, open up some space. 200 00:11:46,550 --> 00:11:54,320 We just want to get the shot energy, copy and paste and just go minus minus to decrement the integer. 201 00:11:55,230 --> 00:11:56,070 Like this. 202 00:11:56,070 --> 00:11:59,670 So it's just going to take away one shot energy from us. 203 00:12:00,210 --> 00:12:03,960 And now I can just compile and save and let's start this. 204 00:12:04,640 --> 00:12:05,510 And let's try this. 205 00:12:05,510 --> 00:12:06,230 I can shoot once. 206 00:12:06,230 --> 00:12:07,010 It's fine. 207 00:12:07,010 --> 00:12:09,800 Two times it's fine, three times it's fine. 208 00:12:09,800 --> 00:12:13,400 And now, even if I hammer the button, I cannot shoot anymore. 209 00:12:13,820 --> 00:12:15,110 And this is working fine. 210 00:12:15,110 --> 00:12:19,190 Now we just have to work on actually replenishing our shot energy again. 211 00:12:20,130 --> 00:12:25,230 So on the player, we want to create a function that replenishes the shot energy. 212 00:12:25,740 --> 00:12:28,470 So in here functions just add a new one. 213 00:12:28,830 --> 00:12:31,860 Just call the restore shot energy. 214 00:12:32,740 --> 00:12:35,170 And in here for now, we can just keep it very simple. 215 00:12:35,170 --> 00:12:39,610 Just shot energy, get it and again just do plus plus to increment it. 216 00:12:39,610 --> 00:12:43,660 And we're gonna adjust this later to also accommodate the charge shot. 217 00:12:43,660 --> 00:12:45,400 But for now this is all we want to do. 218 00:12:45,400 --> 00:12:48,400 If we restore the energy we just get one energy back. 219 00:12:49,550 --> 00:12:51,140 Now where do we call this though? 220 00:12:51,140 --> 00:12:54,620 Because the player doesn't really understand when a projectile is being destroyed. 221 00:12:54,620 --> 00:12:59,900 The projectile itself only knows, and the projectile has to tell the player before it's being destroyed 222 00:12:59,900 --> 00:13:02,480 that, hey, you can get your shot energy back. 223 00:13:03,120 --> 00:13:10,170 So in the content drawer under the blueprints projectiles player because this only needs to be in the 224 00:13:10,170 --> 00:13:11,310 player projectiles. 225 00:13:11,310 --> 00:13:13,800 The player projectile base. 226 00:13:14,310 --> 00:13:15,570 We can go in here. 227 00:13:15,960 --> 00:13:17,550 And here we have our tick. 228 00:13:17,550 --> 00:13:18,660 We have all of this. 229 00:13:19,750 --> 00:13:21,010 We have the distractor. 230 00:13:21,010 --> 00:13:26,500 So now we actually do want to use the destroyed event. 231 00:13:26,500 --> 00:13:28,210 And again this is an override. 232 00:13:28,210 --> 00:13:29,590 You can also find it here. 233 00:13:29,620 --> 00:13:31,120 The destroyed event. 234 00:13:31,720 --> 00:13:36,550 Because we want to make sure no matter how this projectile is being destroyed, we definitely get our 235 00:13:36,550 --> 00:13:37,630 shot energy back. 236 00:13:37,660 --> 00:13:42,940 Otherwise, we might have a situation where we have one stray bullet that bugs out somehow and we never 237 00:13:42,940 --> 00:13:43,990 get a shot energy back. 238 00:13:43,990 --> 00:13:45,940 Which is of course a big issue. 239 00:13:45,970 --> 00:13:51,430 So event destroyed is just the safest way of knowing that we will definitely get our shot energy back. 240 00:13:51,520 --> 00:13:52,960 So what can we do here? 241 00:13:52,960 --> 00:13:58,750 Well, as I said before on the BP player, if we go back to the event graph on the shooting event, 242 00:13:58,750 --> 00:14:04,660 when we spawn the actor, we pass the player through as an owner and as the instigator. 243 00:14:04,660 --> 00:14:07,840 So in here we can get owner. 244 00:14:08,990 --> 00:14:12,110 And this will in this case give us the player. 245 00:14:12,110 --> 00:14:17,150 But one problem here is that it doesn't know if the owner is a player or whatever, it only knows that 246 00:14:17,150 --> 00:14:18,080 it is an actor. 247 00:14:18,080 --> 00:14:24,680 So on here, we cannot call the replenish shot energy because it doesn't understand that this function 248 00:14:24,680 --> 00:14:25,790 is available. 249 00:14:27,230 --> 00:14:32,780 So here we need to use casting so we can cast to be player. 250 00:14:33,470 --> 00:14:34,490 Like this. 251 00:14:35,240 --> 00:14:40,370 And this will just tell it that, hey, this is a player, please give me everything that is available 252 00:14:40,370 --> 00:14:40,970 on it. 253 00:14:40,970 --> 00:14:46,520 And here we can just call restore shot energy, which is the function which we created here. 254 00:14:46,520 --> 00:14:47,840 Restore shot energy. 255 00:14:48,790 --> 00:14:50,650 If we destroy the projectile. 256 00:14:51,550 --> 00:14:52,960 And let's just compile and save. 257 00:14:52,990 --> 00:14:53,830 Try this out. 258 00:14:55,030 --> 00:14:58,930 I shoot one, two, three times and let's see if it works. 259 00:14:58,930 --> 00:15:01,930 And yeah, I can shoot even more times, right? 260 00:15:01,930 --> 00:15:03,220 And if I keep walking. 261 00:15:04,030 --> 00:15:09,700 We can kind of see that there's only three projectiles active at once, and this is working exactly 262 00:15:09,700 --> 00:15:10,720 as intended. 263 00:15:11,320 --> 00:15:16,240 And just to talk about the topic of casting, again, casting is a heavy operation. 264 00:15:16,240 --> 00:15:20,830 It is something you want to avoid if possible, and you could use an interface. 265 00:15:20,830 --> 00:15:26,830 But interfaces is a very complicated topic that if I were to introduce it, I think many people would 266 00:15:26,830 --> 00:15:28,120 get stuck on it. 267 00:15:28,120 --> 00:15:32,770 And actually it is something you don't have to worry about your first couple of years of making games. 268 00:15:32,770 --> 00:15:34,570 It will make the games more performant. 269 00:15:34,570 --> 00:15:38,980 It's not something that's going to make or break your game, but just to make this better, I believe 270 00:15:38,980 --> 00:15:42,010 a better pattern is to not cast on event destroyed. 271 00:15:42,010 --> 00:15:43,660 But we can just cut this out. 272 00:15:43,660 --> 00:15:47,470 Control X and we go to the begin play. 273 00:15:47,470 --> 00:15:53,920 And again we want to first add a call to the parent function just to make sure we're not ignoring anything 274 00:15:53,920 --> 00:15:55,900 and control V to put it in here. 275 00:15:57,280 --> 00:15:58,120 Like this. 276 00:15:58,120 --> 00:16:04,990 And we just want to cast one time on Beginplay, and then we can use the casted version of the player 277 00:16:04,990 --> 00:16:09,190 everywhere, instead of just casting it every single time we want to use it. 278 00:16:09,190 --> 00:16:12,400 So here we can then just save this as a variable. 279 00:16:12,400 --> 00:16:15,070 So here just owning player. 280 00:16:15,990 --> 00:16:16,980 Like this. 281 00:16:18,160 --> 00:16:21,400 And we get this one time when the projectile is being spawned, we save it. 282 00:16:21,400 --> 00:16:26,950 And then down here on event destroyed, we can just use the owning player connected here. 283 00:16:27,630 --> 00:16:33,780 Like this, but we also want to make sure that the owning player actually exists, because an issue 284 00:16:33,780 --> 00:16:39,240 that could happen is that the owning player exists when we shoot the projectile, but maybe the player 285 00:16:39,240 --> 00:16:45,960 dies, and then this reference becomes a null reference, which could cause errors and even could cause 286 00:16:45,960 --> 00:16:47,130 the engine to crash. 287 00:16:47,130 --> 00:16:53,040 So what we want to do here is we just right click and go to convert to validated get. 288 00:16:53,400 --> 00:16:58,560 So now we get this nice node which has an input and an is valid output. 289 00:16:58,860 --> 00:17:01,980 So if it is not valid let's say the player was destroyed. 290 00:17:01,980 --> 00:17:05,339 While this bullet is still active, nothing is going to happen. 291 00:17:05,339 --> 00:17:07,050 Only if the player actually exists. 292 00:17:07,050 --> 00:17:08,700 It's going to try to call this function. 293 00:17:08,700 --> 00:17:10,230 And this just prevents bugs. 294 00:17:10,230 --> 00:17:11,640 And it's a good practice to use. 295 00:17:11,640 --> 00:17:14,099 And now again we can just test this out. 296 00:17:14,099 --> 00:17:19,890 Everything still works the same, but now it's just a little bit more performant because we only cast 297 00:17:19,890 --> 00:17:20,910 here at the beginning. 298 00:17:20,910 --> 00:17:24,780 Well for now we only use it one time, but it could be that later on we want to use it in different 299 00:17:24,780 --> 00:17:27,420 places, and it just makes more sense to cast one time. 300 00:17:27,420 --> 00:17:32,490 And also we have a validated getter to just make sure that we don't have a null reference here. 301 00:17:32,670 --> 00:17:40,650 One more thing is that on the event destroyed because now we have moved the VFX of the projectile exploding. 302 00:17:40,650 --> 00:17:42,780 It is uh, somewhere else now. 303 00:17:43,680 --> 00:17:48,600 Um, we just want to make sure that we call the parent again, because later on, we might want to add 304 00:17:48,600 --> 00:17:49,710 some functionality again. 305 00:17:49,710 --> 00:17:52,230 And if we forget about this, we get some weird bugs. 306 00:17:52,800 --> 00:17:57,330 And the next lesson is going to be very exciting because now that we have the projectiles working, 307 00:17:57,330 --> 00:18:02,370 we have the shooting working, right, we have the animation transitions working, the bullets are working. 308 00:18:02,370 --> 00:18:06,690 We can finally start to create some enemies and make the game more interesting. 29481

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