Make a 3D Top Down Shooter with Godot - Part 3.11 Drops & Pickups (2 of 2)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay so now how do we incorporate this into our game without having this drop method here in our game well the way there's probably tons of ways to do this the way i'm going to do it though is i'm going to incorporate it into our waves so this will be another option you can have when you're designing your waves about which weapon can be dropped and when it might be dropped okay so in spawning directory let's go to our wave script and we're just going to add a few more variables here and i'll put them under this little drop section here so first off the wave might have is going to have to have a item to drop and i'll call it a drop item as well and then we're going to add a couple more options here so we're going to say we'll give it a drop chance so by default i'll set it to 1 which is 100 chance the item will drop but say you wanted to add more probability you could reduce that and then i also want to know when during the wave so we'll call this drop when and this is also gonna be a number between one and zero so where this is where 0 equals the beginning of the wave and 1.0 equals the end of the wave now we're not actually going to want to use 1.0 because that complicates things to drop at the end of a wave since the nav map is changing for the new level if you if you want something to drop at an end of a wave just make it actually drop at the beginning of the next wave that makes more sense so why don't we just set this to 0.5 now so it should drop in the middle of the wave after half the enemies have spawned i guess i think what we'll do is if there is a drop and you don't get it by the time you finish the level then the any drops left in the scene we'll have to we'll have to cue free them disappear them for the new level to take place since the where the drop item is might show up where an obstacle is in the new level as it appears so okay so we have some variables now in our wave scene here oops there we go so drop item drop chance and when i'm just going to leave these blanks as defaults because we're going to want to be specific to the levels we're working on here so in our level generator generated levels we'll go to the nav map one here so the player starts with a pistol and then the next kind of item power up is going to be the shotgun guns here so let's drop the shotgun there i'm going to say it has a 100 chance of dropping after uh there's one enemy so i'll make two enemies so it should drop after if if we write our logic correctly it should drop after the first enemy dies okay and then uh well let's just get this working first so i'll save that so let's go back to our dropper script here actually we're going to want to go to our spawner script because our spawner script is where all the wave logic is right so each time an enemy dies here we go on enemy stats you died signal we're going to want to check if an item should be dropped so what we can do is we can go if currentwave which holds the wave currently going obviously and we'll create a new method called should drop and the we'll have to pass it some info so maybe we'll pass it the number of enemies killed this wave it'll already know how many enemies are supposed to be in the wave so that way it can determine it and this will just return true or false and if it's true then we'll emit a signal a new signal and we'll call this we'll just say drop item whoever's listening whoever's capable of doing this drop the item and then we will pass it the current waves drop item variable which is a i did it in caps just because it's a packed scene that needs to be instantiated and then maybe just to check here we'll we're going to need to implement this should drop method in the wave script so let's go back to our wave script here oh wow wave's pretty boring so far isn't it so we'll create this new should drop method we're going to pass it the num killed so far so it's possible we don't want anything to drop in which case we wouldn't have given it a drop item at all so we should check if a draw drop item even exists if it does then we'll determine the fraction killed of enemies killed in this wave and it's going to equal num killed divided by num enemies now because these are both integers we don't want to divide integers because we're going to end up with an integer so we're going to convert one of them to a float here and then we should get a fraction and then we'll check if the fraction killed is greater than or equal to the drop when value so if we killed half and that's greater than equal to half then we'll drop which just means returning truth otherwise we'll return false okay let's see uh so no drops uh let's kill one guy i see the word dropping i do not see a drop however okay so it said dropping which means in our spawner script it's dropping it's emitting a signal drop item which does nothing at this point so we'll probably want to connect this signal here drop item which we didn't actually create signal all right signal drop item okay so the spawner is going to send this drop item signal let's connect it to our dropper here of course we're going to get the drop item as a packed scene and this is pretty simple here we should just be able to go drop um however the drop item on the spawner here is no longer going to be a an export variable so instead here we're going to set the drop item they're both named the same thing so this is how we would do it in python we'll set the drop item scene and then we'll uh and then we'll do it drop item all right okay so after killing one enemy should get a drop hey there it is and it's a shotgun cool oh but it does it again after we kill the second guy okay because of course after you kill the second guy uh the fraction is also greater than or equal to 0.5 so unless you want it to drop an item after every kill after your drop when fraction then we're going to need to control that with a new variable we'll call drop completed well to start it'll be false here we'll go if drop item and not drop completed then we'll check and if everything checks out then we'll set drop completed to true oops there we go okay give this a quick try kill that bad guy good there's a drop don't get another drop that one's still there i can get it i got a shotgun now oh my gosh this already is all right okay um we probably don't want these items dropping in the same place every time now we get a little predictable which it just did there again so okay so we've considered the fraction killed but we have not yet considered the drop chance so if the fraction killed is all right then the next thing we're going to want to do is pick a random number here which will create a new variable called ran a new rng random number generator which doesn't exist yet so why don't i do that right now actually ctrl k that here we'll go rng random number generator dot new and in our ready method we'll randomize that otherwise we'll get despite it being a probability that same probability will happen every time we run the game so dot rand float oops ctrl k so we'll get a random float and then if that's less than or equal to our drop chance then we will drop gotta indent that good and right now our drop chance is one but if i set my drop chance to 0.5 let's go zero point yeah 0.5 actually let's set it less let's go just to test it we'll go 0.1 but we'll have a lot of enemies and we'll just do one second between spawns here just to test this so it might not happen after the first one there's only a one percent chance um but it's it's a one sorry a 10 chance but it'll be a 10 chance after every enemy until it actually drops and then it'll be completed also set the drop when to zero so that the odds of dropping happen right away just so we can test this okay so we got 10 enemies killed two of them uh i don't i think this went to wave two what happened here is that because i didn't save it oh that's because this is in our default i did that whoopsie we need to actually go to nav map one here and set these things okay so i want to drop from the beginning and i'm going to set a drop of 10 hopefully after enough enemies and then seconds of this one will go 0.2 all show up right at once and then at some point in here i should get a drop if i can survive long enough there it is hey i don't know it appeared somewhat randomly you can output some of those values if you're concerned about the logic and the uh code here i'm happy with that so the last thing we're going to need to deal with here is at the end of a level uh when the level changes so if we go to if we delete this wave um and we just have wave one here and we go back to two enemies two seconds i have the drop chance of 100 after the first enemy and i'm not going to collect it so we need to randomize the enemy spawning too don't we i know exactly where they're coming from okay so oopsie there we go uh the that thing's like partially inside can i get it and i can't even get it because it's actually inside that block that's a problem okay so uh in the spawner here we need to go find our start next wave spawn enemy start next wave here because dropping at the beginning of the level isn't going to work because our currently it's only called after an enemy is killed to check if we want to drop so we also want to check when a wave starts if it's supposed to drop an item right at the beginning of the wave so what we can do is we can go if current wave we'll do should drop again and we'll pass an enemy kill this wave which is going to be zero if we should then we'll emit the signal here as well limit the drop item signal and we'll pass the current wave drop it okay so now uh that should be easy enough to check in our wave one here we'll set the drop when to zero and it should just drop it right at the beginning there it is okay good so the last thing we're going to want to do here is that we remove any drops when the next level starts so we'll do that in i think it's the game script that calls the next level here uh new level right here so we're doing all sorts of stuff for resetting the players position and the last thing we're going to do is we're going to reset the spawner so let's go dropper.reset and you want to make sure we reset the dropper before we reset the spawner because resetting the spawner is going to trigger a new wave which might cause you problems if you try and reset the dropper after the spawner so let's go into the dropper we'll go up here uh after the drop method we'll create a reset method and we're just going to want to check if uh draw these this drop item exists in the scene we're going to get rid of it we'll go if and then we'll use that remember this one is instance valid of our drop item so basically if we've instantiated a drop item then we'll cue free it there we go and let's just make sure that happens you know what i'm going to do on the wave in nav map one let's duplicate this let's uh duplicate this wave so two shotguns are actually gonna drop and we'll see if both of them get removed here's one shotgun and then in the second wave here oh it's doing it at the beginning of each wave uh actually that works that works uh that didn't remove though that's a problem why are you still there okay okay i think we can fix that from our dropper script let's go back to the game scene and in our dropper script here when we go to drop a new item let's just call our reset method here just to clean everything up and then that should avoid any issues we have like that in the future you could imagine having multiple drops at the same time instead of having a drop item here this could be an array and you could drop multiple items but for our purposes this little simple system okay so i'll leave that there and let's see if that fixed the air good that one's good and now one more lay let's switch levels and see if it works there we go excellent and this level doesn't have any drops one last thing i'd like to clean up here is you may have noticed our weapons here don't actually center themselves on the cell they're like off to the side because of course the weapon's origin is up front here right so when we spawn them we want them to be centered on the cell and i also want to add a little rotation animation for the pickup so we can spend a couple minutes doing that so if you remember back to the last episode we actually created a drop method for any item that could be dropped and that included the weapons so let's go to our weapons and we'll go to a gun and let's look at the shotgun here so the mesh is currently right here so if we could reset the mesh origin to zero that should center it pretty good for us and i think for the rest of the weapons as well we can do the same thing you can see this here so i better undo undo those changes ctrl z here nothing to undo that was 1.4 i think yeah okay so let's go do that in the drop method in the guns drop method which we have right here not doing anything at the moment don't need to print that so what we can do uh we can get the mesh and set its translation what script are we in here uh yeah we are in the gun script maybe if we go to the gun scene this would be a little clearer okay so i just like my auto completes that's all let's set its translation to a zero vector okay so now when the item drops should be at least centered on the cell yeah that looks a little better right the next thing i want to do is have a little rotation animation for when the item drops so we already have an animation player for our guns so let's add a new animation we can call it drop and we can do it on the mesh or the gun the mesh is going to be reset to zero but let's just let's just do it on the gun here and if we go down to the rotation we can set a keyframe at zero and then maybe we'll have a rotation period of two seconds and here we'll set the rotation about the y to 360 degrees and we'll add another keyframe there and if we go watch this we should see it doing a rotation now it looks funny here but remember before this animation plays it's going to be moving it to the center right we also want it to go in a loop so we'll take the little loopy and now we should have a little rotation so let's see if that worked go back to the beginning so one reason to rotate the gun instead of the mesh is just because the gun has no rotation already so we can go from zero to zero whereas the mash to make sure it's pointing in the right direction we've actually rotated on the y about the y ninety degrees so that it's pointing in the negative z direction for where it's shooting so that just makes it easier to rotate the gun so i'll save that we'll go back to the gun script and then after we translate it we'll play that animation so let's go get the animation player and we'll play the drop animation and this should also hopefully prevent it from reloading because it's going to stop playing the reload animation and just drop it it didn't work it reloaded what's happening okay well um let's stop that reload animation from happening right at the beginning here we'll go remove that and instead what we'll do is we'll go into the gun controller script so this is the players if we go to the player gun controller gun controller script here after they've equipped a new weapon so hand add child equipped weapon we're going to connect some stuff and then we'll reload it then and let's see if that works okay look all right it's spinning let's go get it we pick it up we reload it and we're off to the races
Info
Channel: Age of Asparagus
Views: 257
Rating: undefined out of 5
Keywords: AgeOfAsparagus
Id: 0Okn9WdaVsM
Channel Id: undefined
Length: 20min 51sec (1251 seconds)
Published: Tue Aug 10 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.