How To Set Up An Advanced Ammo Inventory - Unreal Engine 5 Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up guys mizo frizzo here and in this tutorial I'm going to be showing you how to set up a full ammunition inventory system complete with Reserve ammo reloading the lot this is quite a complex system so this is going to be a big one but if I walk over to this weapon for example which has uh 9 mil by9 ammunition you can see I've got my current ammo and my Reserve ammo down the bottom right I can reload and it's going going to set it to the magazine size plus one that was remaining in the chamber if there was not one remaining in the chamber it will just reload to the magazine size then I'd have to reload again to get that one extra and if I walk over to a weapon uh with an ammo type that I already have in my inventory you'll see it will do a nice little impulse and add ammunition and you can see up the top left there a print string telling me how much ammunition I've added and now that I'm at max ammo this is not picking up anymore so 500 is the max Reserve ammo for this ammo type it's not going to pick up anymore until I use some and reload and now my Reserve is going to top up again and these pickups if they did not have any more remaining it will not pick up anymore So eventually um I've set these ones to have quite a lot in reserve so this is topping me up quite a few times uh but eventually it's going to run out I'm not sure what I set this to quite a lot um yeah still got more to go I think I may have set it to 200 so I'd have to use 200 ammo and there we go it has run out of ammunition now and you can see now it's not going to pick up anymore so like I said guys this there's quite a lot that goes into this this is going to take some time but I promise you it will be easy enough to to follow along and if you do follow along I guarantee you'll be able to replicate this in your project just before we get started guys please uh take a moment to consider donating to the channel via the YouTube thanks function these tutorials take no small amount of work so any donations go a long way to keeping me going and keeping me bringing you guys some quality content without further Ado let me show you how to do this all righty guys now managing an inventory is basically just finding a way to store and access and set um different data um so basically an inventory is just a way of storing data there's several different ways you can go about this um but we are going to use an array of structures so with that in mind let's go ahead and create our structure in in our main folder I'm going to just go into props actually and weapons and just in here I'm going to rightclick and under blueprint and structure I'm going to call this struct underscore ammo um I'm going to open this up and this is going to contain basically a set of variables and and that's it um now the first one we can just make an enumeration actually so let's back out and in our folder here I can just duplicate one of these enumerations and I'm going to call this enum ammo type going to open this up and we're going to so I've got five different ammo types that I'm working work with here I've got six weapons and two of them basically share the same type so I'm going to go ahead and create the ammo types for these weapons but uh the principles will be the same so whatever ammo types you want to have um you know you can set that up how you like so the first ammo type is going to be 9x9 mil the second MMO type is going to be uh 545 by 39 mil the third one is going to be 762 by 39 mil the next one's going to be my 9 by 39 mil and then I'm going to have 556 556 by 45 mil I'm going to scrap these other enumerators that I don't need so these are the five ammo types that I'm working with okay so with that enumeration created I can close that and then in my struct ammo my first variable is going to be ammo type ammo typo more like and then I'm going to set this to my enum ammo type that I've just created okay um the next variable I'm going to have here is an index so I'm going to call it ammo index and I'm going to make it an integer basically we're going to make this one a reference to which ammo type it is it's just going to be basically an integer that denotes uh the ammo type you'll see why this is Handy uh very shortly uh the next one I am going to put is reserve so Reserve ammo and again integer one more will be Max Reserve ammo also of type integer the next one I am going to make a Boolean called has question mark so this is going to be set as true once we have thato type and that should be all we need for now we are going to add this struct to our weapon Blueprints and our pickup Blueprints and also we're going to add an array of structs to our characters blueprint but as I said uh this is just a different way of managing data um storing data and we're not actually going to use every single one of these variables in each instance where we add these structs we're going to you know most of the this is going to be used in our characters blueprint but then in the weapon blueprint and the pickup blueprints for example we're only going to use ammo type and ammo index just to check what type of ammo that weapon is using so with that in mind we can save this and close this and let's go into our characters blueprint which for me is third person character and the first thing I'm going to do is start adding a few variables so I'm going to add my ammo type variable and I'm going to look for that struct ammo that we just created and then I'm going to right click here and change this to an array or you can also drop down this menu up here and select array so this variable is an array of structures so um it's going to have some indices and within each indic is going to be a different uh struct a different copy of that struct with a different set of variables if that makes any sense so uh this will actually make more sense if we compile now and then over here in the default values we can add five indices for our five different structs okay and so in here is where you will store most of the data of your ammunition inventory so we can go ahead and for starters just change the ammo type and the ammo index so this index will coincide with this Index right here so this next one this one can be uh can stay as 9 by9 mil at index Zer and then this one's going to be the second one at index one this one's going to be the third one at index 2 so on and so forth index 3 and the last one at index 4 like so so we've got our four different Amo types H sorry our five different Amo types at our five different indexes um you also want to um so Reserve is going to be Zero by default because we haven't picked it up yet has is also going to be false by default but here is where you want to set your max reserve for each weapon um so I'm just going to make these a little bit different for each one so that we can test that everything's working um I'm going to make this one 210 I'm going to make this one 220 oh that's Reserve not Max Reserve that should be zero so we're I've done it here as well haven't I I'm going to make this zero and I'm going to change the what I think my mouse wheel is doing that sorry about that Max Reserve 210 Max Reserve 220 Max Reserve 230 Max Reserve 240 and Max Reserve 250 nice now the very first thing that we're going to do is set up um how this is set on our spawn weapon logic um so I've got my spawn weapon logic and my switch weapon logic here um and we're going to set this up so that it knows what ammo type we're picking up and sets this Boolean as true but first we just need to add these structs to our weapon Blueprints and our pickup blueprints so I'm going to go into my main props weapons and find my BP weapon master I am going to add variable emo type whoops of type struct ammo that we created like so compile and save and close that and then I'm going to want to filter by blueprint class and I want to change this in each of my weapons so like I said my pistol and my Mac are going to use the same ammo type so in both of these what we want to do is go in and in the class defaults change the the ammo type and the ammo index to make sure that's correct so that should be correct on both of those because it's the first one and it is and then I'm going to go up through my weapons in order so my ka74 U is my next one 545 by 39 ammo index 1 compile save and close my next one is my AK 762 ammo index 2 and if you want to double check you can just check in your characters blueprint what you set them as here so 762 by 39 was 2 762 by 39 2 and this one's the next one which will be 9 by 39 Ando index 3 and then and this is my last one 556 ammo index 4 it's a little bit tedious but you will have to do this with all of your weapon Blueprints and your pickup blueprints so BP weapon pickup master I also want to add ammo type find that struct compile save close and then go into each pickup so Uzi and Pistol should be 919 and zero K 74 U is 545 and one okay 762 and two my Val 939 and three and lastly 556 and four nice now anytime we spawn a weapon um and we get you know what is in the index on our current weapons we'll have ammo type right here okay so um the oh there's also one other variable I'd like to add to my my pickup uh Master while we're here weapon pickup master and it is just ammo of type integer okay and we also want to make sure that with this selected we over here click instance editable and expose on spawn like so and this is going to be the ammo in the clip of this weapon that's sitting on the ground so um instead of accessing ammo type and changing that we're just going to store the ammo that's in the clip in its own variable here because it's just easier when we spawn this pickup so this doesn't have it there um but if I just get rid of this and bring this back up again spawn actor from class you'll see it says ammo now and this means that when we spawn our pickup uh we can set how much that ammo is um if that makes any sense instead of um instead of you know breaking open the ammo struct and setting the ammo struct blah blah blah we are just able to just plug an integer in here and set the ammo of that pickup okay um we also need to pass that through on our spawn weapon right here so we can click and we we'll pass through the emmo type as well so we can click this spawn weapon here and we can add add a couple more inputs we can call One ammo of type integer and we'll add one more call it Amo type oops ammo typo again uh and that will be our struct ammo okay so we're going to pass these through on our weapon pickup so in weapon pickup Master where we've called spawn weapon we can plug in ammo type here and uh sorry ammo and ammo type like so nice all righty so this logic here is going to change a little bit um we need to basically pass that ammo and ammo type through to set the ammo uh that we're holding and um and the ammo of in in our struct so just to make things a little bit clearer um we'll create the variables now uh I'm going to create another one here called ammo and that's just going to be of type integer I'm going to right click here to change it to a single this is going to be the ammo of the gun that we're currently holding and it's going to be the number that's displayed on the screen if that makes any sense um we can also duplicate that and we'll call this one Reserve ammo and this is going to be the one that's also displayed on the screen the reserve ammo of the current weapon that we're holding it'll all make sense once we um plug it all together don't worry I'm going to duplicate this again I'm going to call this one current primary emmo and I'm going to duplicate that one and I'm going to call the other one current secondary ammo this is going to be this is going to store what the current ammo uh is of the weapons in our first slot and our second slot and we're going to use it to set the ammo displayed on the screen when we switch okay um and I'm going to get one more and I'm going to call it unspent ammo this is going to be the ammo that's left in the clip when we reload so that we can um determine how much to take off of our Reserve ammo when we reload okay so the first thing we need to do when we pick up a gun is um we actually can just take ammo here and just plug it into this ammo on the spawn actor so when we drop a gun it's going to drop holding the amount of ammo that we had of that gun that we were holding if that makes any sense um and then after we've spawned you know equipped this weapon we need to set the ammo and we're going to set it as the ammo of this weapon that we're picking up the weapon to spawn so we can plug that in there make a reroute Noe you know you could probably you could probably pass this variable through with another variable but um it's I I I really don't see a point in doing that because we don't need to calculate or anything we don't need to do anything with that variable we just need to set ammo off it so we're going to do that and we're also going to grab this ammo type right here and drag it way out to the end here as well and we're going to break struct ammo so this is how structures work they're made up of these variables that we created and you can take a struct and break it so that you can access all the variables and then you can also uh make a struct when you want to set those variables so what we want to do is uh we're basically going to use this ammo index to access what type of ammo is in so as I said ammo types here is where we're going to store most of our data and use most of our data and what we can do is we can get a copy at index the same ammo index of this one that we're setting it off um yeah this is this is going to get quite complicated guys but stick with me uh and then we can also break this one open and then we've got the variables inside of that ammo on our character so this is the one that we're actually holding this is the one that we're spawning and all we're getting from this is the ammo index and maybe the reserve ammo that's stored on it um so so while we're at it what we might do in the weapon pickup Master is go into the ammo type here and we will just set the reserve ammo as 50 by default so that's just going to change it in all of our pickups if we look at our other pickups inside the ammo type structure the reserve ammo has been set to 50 so just so it has some Reserve in there um that should be a good good number um okay so the next thing we need to do is set this reset this element and for example we need to set it as has so what we can do is get our amemo types again and we can set array element the array element that we're going to set is the same ammo index again so we can pass this over to here get a reroute node keep it neat and tidy and basically um we drag off item here and we can make struct ammo and this is basically how you set um a structure within an array we need to set it as for the most part the same as what it was here right so ammo index is always going to be the same Max Reserve ammo is always going to be the same has is not going to be the same because we want to set this as true now we now have that ammo type okay and the reserve is also not going to be the same because we're going to pick up the reserve with the ammo here that we're picking up so what we can do is make a little bit of space here and it's going to be the reserve ammo of what this is how much we had plus how much Reserve ammo was in that pickup clamped clamp integer between zero and the max Reserve ammo of that type of ammo and then plug that into Reserve like so and that has updated the ammo type inside of our character it's updated how much reserve it has it's updated um that we have it and we also just need to update the reserve ammo which is the one that will be displayed on screen okay like so um yeah so that's that's everything with regards to picking up a weapon um we also need to switch weapons so down here where we switch weapons this will be a little bit different um we need to we first need to set the current amount of uh ammunition as the current for that weapon slot so we can grab our ammo and we can set it we're going to set it twice here off of each of these These are going to go into the same one here and we're going to set ammo as our current primary ammo and our current secondary ammo like so I'm just wondering if if we'll need to also do that up here let's do a little check if we're using our primary or our secondary at the end here and set our current primary and secondary ammo um forgot to do that so we can grab our current weapons and get a copy and we can check if that's equal to the child class so get get child actor of our equipped weapon and get class and then check if those are equal make a branch and if they are equal then we are on our primary if they're not equal what we can do is we can duplicate this down here change the index here to one if they're not equal we'll check if they're equal on the other slot which they will be so if this is our primary we can set our current primary ammo as our ammo whoops whoops that changed that one my bad and our current secondary ammo down here as our ammo like so all righty and then the switching so we set that at the beginning there and then um we also need to do some things off the end we need to um so we're setting our ammo we also just need to set our Reserve ammo so we can get we're setting all of this off of the equip index um of for current weapons here so we can take our ammo type here and break that and then uh we can get our current weapons get a copy at that ammo index and we can also break that oops oh sorry ammo types my bad ammo types break that and then we just want to set our Reserve ammo as the reserve ammo of whatever ammo type we're switching to so we can start to test this now but first let's just add it to a widget um so so we'll need to create the widget but we'll also want to create a blueprint interface so I'm going to create a blueprint interface um actually a function within character data so I can open up BP character data and add a function call it iore get ammo like so and this is going to have an output called ammo type integer and another output called Reserve ammo of type integer and in our third person character under interfaces we can find our get ammo because that character data should already be added to our uh third person character if it hasn't just uh right click go to blueprints blueprint blueprint interface create a blueprint interface called character data open it up uh rename the function to get ammo um you can see I've got the left-hand ik setup here so that's why I've got this one um you can create a function called get ammo add these outputs and then in your third person character go to class settings and interfaces and add that blueprint interface which will be the character data add that compile and then under interfaces over here you'll see that get ammo function we can go into there and we can just plug in ammo and Reserve ammo like so so like I said these are the two that will store the value that's displayed on the screen and then we can in main create a new folder call it U U and in UI we can right click and under user interface widget blueprint create a new user widget call it widget main or whatever you want open this up I'm going to find a canvas panel drag it out here I'm also going to find text drag it onto canvas panel here and then I'm going to select it I'm going to Anchor it to the bottom right I'm going to zero out the location here and just move it to about there um I'm going to change the appearance a little bit I'm going to change the color to Yellow to make it stand out um I'm going to make the font size it's a little bit big 32 say um I am going to rename it XXX to denote three digits I'm going to click size to content and I'm also going to change the justification to align text to Center and now that I've got all that I am going to duplicate it I'm going to put the other one about here and make it a slash maybe make it a bit bigger make it 40 like so and I'm just going to do it like this I'm going to duplicate this one more time put this one down here here this is going to be our Reserve ammo um and now we want to bind these so what we can do is click on the first one and click bind here and create binding and I am going to call my get ammo interface function this will be get player character and I'm going to do something here that will make it um appear as a dash instead of zero when it's zero you can just take this ammo and plug it into here but what I'm going to do is first check that it's greater than zero and off of here I'm going to get a select I'm going to plug this into the index here and if it is greater than zero I'm just just going to set it as the ammo like so and if it's not greater than zero I'm going to put in a dash here or two dashes for the ammo I'll put two dashes for ammo and three dashes for Reserve ammo so that's all we have to do in here we can go back to the designer and select the reserve ammo here and also create a binding also going to I can actually go to get text here and I can duplicate at most of this so contrl C to copy and paste that in and this time I'm just going to hold control and drag these onto Reserve ammo plug this in here like so nice that should be all good to go um ah I just need to add that to the viewport so in our third person character in the event graph I'm going to find event begin play and then on the end here create widget set it to our W Main and add to viewport nice now I'm not sure what we've set um these two uh just because I'm a bit of a perfectionist I do want to go into my W Main and my Reserve ammo get text function I'm going to make this three dashes like so so we'll have two dashes for the ammo three dashes for the reserve um and also that's a little bit close to that that uh forward slash so I'm also just going to in the designer move these move this out here a little bit nice um now can't remember what I set these to uh so they won't have any ammo by default um and the reserve ammo is 50 um so just to test this out let's go into our pickups and change a few things um yeah we go into the weapon pickups I'm going to filter by blueprint class and I need to set the max of the um so basically I can set the max as the same Max in our ammo types in here um oh no because it'll be different for each weapon so it'll be the magazine size if we've set that up in our weapon master um BP weapon Master we should have magazine size but then if we've set that for each of these is's another story um a magazine size is 12 on the pistol 26 okay yeah so I'm just going to go ahead and open up all of my weapon blueprints here because it has the magazine size stored in there and then in each of the weapon pickups I'm going to go in open up the pickup check its corresponding magazine size right here in the blueprint and I'm just going to make ammo which as you recall is just going to be the ammo stored in the magazine of that of that weapon of that pickup going to make the ammo that um I'm also going to change the reserve ammo while I'm here um on each one as as I go up so I'm going to leave the pistol as 50 I'm going to close those two I am going to then open up my Uzi pickup and check the magazine size copy that set it as the ammo and I'm going to change the reserve ammo to 60 just so we can see things changing and you know differentiate what's happening close those two open up my next one check its magazine size set the ammo and set the reserve as 70 so on and so forth you know momento PO for V shouldn't be too much longer whoops ammo and I think I was up to 80 50 60 70 80 maybe it's 90 can't remember doesn't matter too much like I said it's just to change things up and differentiate and then last but not least magazine size ammo and I make this 100 nice now when we pick up a weapon it'll change to that amount of ammo so 20 30 this one should be different again 20 30 and when I switch it's changing the amount of ammo and the reserve ammo for that weapon type nice um let's go ahead and make it so that when we when we overlap with an a gun that has an ammo type that we already have that we go ahead and pick up the reserve ammo from that weapon and the first thing we're going need to do is create a blueprint interface so again we'll go into interfaces we'll right click and blueprint blueprint interface we'll call this one BPI weapon pickup cuz it's going to interface with our weapon pickup we can open this up and we're going to create a couple of functions in here we can call the first one if get get ammo index no we'll call it get ammo type and it's going to have one output called ammo type ammo type of type struct demmo and we're going to create another one called take Reserve ammo create a function interface function underscore take Reserve ammo and this one is going to have one input called ammo taken of type integer compile and save that and and that's all we need to do in here we can then go into our weapon pickup Master weapon pickup master and we can go to class defaults class settings sorry and interfaces over here and add our BPI weapon pickup compile and save and then under interfaces here you'll see the if get ammo type we can double click that and then here we can just pass through our ammo type like so and we can also doubleclick take Reserve ammo and it will create this node this event we're not going to do anything here yet we're going to set everything up in our characters blueprint first but we just needed to add this interface and especially get the Amo type so we're going to go back to our third person character and we're going to do is on event begin overlap so what we can do is grab our capsule component we can select it here and then scroll down and on uh sorry component begin overlap we can hit this little add icon and it will give us this node right here I'm just going to Move It Up near our switching and spawning logic we've got our firing logic here which we're also going to make some changes to uh when we set the ammo when we fire but we're just going to do this this part first so the first thing we can do off other actor here is check uh does implement interface does implement interface and we're going to select our BPI weapon pickup interface like so and do a branch so when we overlap with another actor um this won't this won't fire off fire off if this does if the other actor doesn't implement the interface BPI weapon pickup it won't do anything um the next thing we need to do off of other actor here is do our get ammo type if get ammo type and then this gives us access to the ammo type that the overlapped weapon and pickup is using we can break that struct now things get might get a little bit messy here but um just follow along the next thing we need to do is just get the ammo types within our character here and get a copy of the same ammo index of this weapon that we've overlapped with and then we can also break this one and we're going to do a couple of things with this we're going to make a branch that checks um a few things so let's just drag off of this execution pin here and make our branch and then on the condition of the branch let's find an and Boolean and let's add a PIN to that so we're going to have three conditions here on this uh on this Branch the first one is going to check if the pickup that we've overlapped with has any reserve ammo so we're going to check if the reserve ammo is greater than zero that's our first condition the second condition is going to be that our Reserve ammo isn't already at the maximum so we can check if the reserve is less than the max Reserve like so that's going to be our second condition and the third condition is just going to be do we have that ammo type already we want to check if we have that ammo type already um so the next thing we're going to do is take that Reserve ammo and we're also going to um set our our Reserve ammo sort of as as what the new one is so I believe we'll need the other actor so we can drag off of here and find our if take ammo take Reserve ammo and I'm going to try and keep this as neat as possible but we are going to have a lot of lines here in a moment so if all of these are true then we want to take the reserve ammo um and we're also going to set the array element um ammo taken we're going to calculate in a moment so we're going to get our ammo types and set array element and the element that we're going to set is the same ammo type of the one that we've picked up so it's going to be the same ammo index here can bring this down here drag this over here get some reroute nodes like so uh we can make so off of this item here we can make struct ammo and the things that are going to be unchanged uh ammo type obviously ammo index Max Reserve ammo is not going to change and has is not going to change but the amount of Reserve ammo is going to change the amount of Reserve ammo is going to be um The Reserve ammo that we had plus the reserve ammo from the weapon we're picking up so this one right here and that's going to be clamped between zero and the max reserve of that ammo type right there that's going to plug into the reserve and then we also need to calculate the ammo taken which we're going to it's going to set the remaining ammo reserve on that pickup so it's going to be the um whatever the output of this is so our new ammo is going to be that minus um minus the reserve that we had so this Reserve Mo right here can make a reroute node here and just pass this down to here I know this is quite spaghetti like and quite confusing uh you might have to go back over this a couple of times to get it right but it is what it is guys um plug this one in here to ammo taken and pass that through there and that's all almost everything here um what we might do is just make a function called update current Reserve ammo so we'll create a function new function update current Reserve ammo and then back on the event graph at the end of this row here we'll call that update current Reserve ammo and inside update current Reserve ammo we're going to get the current weapons here and get a copy of the equip index so whichever weapon we have currently equipped I can get the class defaults break the ammo type and then get my ammo types and get a copy at that ammo index and I'm just going to set my Reserve ammo so once more I'm going to break this struct again and set the reserve ammo as the reserve ammo of that weapon like I said guys there's a lot that goes into it but uh we'll get there in the end don't worry so I can go back to the event graph here and this is all set up but we just now need to implement the functionality on that weapon pickup for the take Reserve ammo so we can and go to our weapon pickup Master that's where we're going to be working and right here we are first going to get the ammo type of the weapon of that weapon pickup and we're going to break it and we're going to check if the reserve is greater than zero because if it isn't we don't want to do anything we've checked that in our other implementation that we just did in our character but it doesn't hurt to double check again we don't we don't want it to do anything here um if it is greater than zero then we are going to set our ammo type and here we can make struct can plug in the stuff that's not going to change so I'm just going to organize this so they're lined up a bit better um the ammo index is not going to change the max Reserve is not going to change the has is not going to change um the only thing that's going to change is the reserve and it's going to be the reserve ammo minus the ammo taken like so just going to pass this around the bottom here plug that into Reserve like so nice so we're setting the reserve um of that weapon pickup as what it was minus the ammo that was taken when we when we overlapped and we took that ammo um and what we can do to signify that something has happened here is add a little impulse and add a sound at location so we can grab our pickup item here and add impulse I'm going to check velocity change and I'm going to split this impulse into the X Y and Z and I'm going to find a random float in range because I want this to be a little bit random be a nice little effect to add here and on the X and Y I'm going to make this relatively small say minus 25 to 25 and I'm just going to duplicate that one so I don't have to type that again and on the Y AIS sorry the z-axis I'm going to make it 125 to 150 this might be different for you depending on the mass of your pickups I made the mass of all my pickups 60 K kilg just to make them sort of behave a certain way um but yeah these are the these are the figures that I found work quite well for me and and I am going to play sound at location and just get the actor location plug it in here and believe in the weapon packs there's a shotgun ammo sound shotgun ammo pickup Q I'll browse to that yeah just a little ammo pickup sound uh nice that should be everything compile and save that and I might just save all while I'm at it if I hit play now these are probably the best ones to check so if I don't have the ammo type it shouldn't do anything if I pick it up I've got 60 in my Reserve if I go over to another one it's doing the pickup and adding more and again and again and if if there's none left in that pickup or I'm at Max it won't pick up anymore I'm not actually sure if I am at Max but um there we go um let's add the reload functionality actually you know what let's add the set ammo function first so I am going to here's my firing logic right here and a couple of things are going to change right here but um the first thing I'm going to do is just below it I'm going to create a custom event and call it set ammo and then inside of this fire function I'm going to call set ammo so every time we fire it's just going to set the ammo like so okay and the setting ammo is maybe a little bit more complicated than you might think because we need to set the ammo that's displayed on screen but we also need to set the current primary and the current secondary ammo so that when we switch it knows what to switch to so we can get our ammo and we can decrement int so that's just going to subtract one off the ammo like so and there's no need to add a set node this actually sets it for us um and then what we're going to do is just check if our current weapons at index0 we've done this before is equal to the child class so get child actor whoops get child actor get class check if these are equal and if they are equal then we are on our primary weapon we can duplicate all of this and change the index to one so this is checking if we're on our primary or our secondary if we're on our primary We Want To Set current primary ammo as the ammo right here and if we're on our secondary want to set our secondary ammo as our ammo like so all righty um now we've called that in each of our fire functions but we also just need to put in some checks that we actually have some ammo um and the first thing I'm going to do here is I'm going to going to add custom event right here custom event and I'm going to call it cease full auto okay and on cease full auto I'm just going to duplicate this actually I might just be able to plug that right in here cease full auto is going to call that clear and invalidate timer by handle on that fire function and I'm going to do the same actually I will need a separate one up here so I'm going to add another custom event call it cease burst fire and I'm actually going to duplicate this clear and invalidate handle plug in the handle right here nice because I don't want it running this other logic afterwards I just want it interrupting that Loop um and what we want to do is we want to check if um we want to check if we can fire every time that we do fire so what I want to do is create a function check can fire create new function check can fire and in check can fire I'm just going to check if my ammo is greater than zero you can add other things to this function later but right now we're just doing ammo going to add a branch here and if it's true I'm going to leave can fire as true and if it's false I'm going to set can fire as false like so if I back out to my event graph what I can do is I can call that right here I can check can fire but obviously with the way this is set up um this this timer by event will keep firing unless I release and especially with the fully auto so we're going to need to add check can fire on let's start with fully auto just double check this is my fully auto down here I am going to check can fire right here before fire check can fire and I'm actually going to create a new variable in that check can fire I'm going to go back into check can fire just going to close these ones check can fire and I'm going to create a new variable called stop firing question mark of type Boolean and off of false here I'm going to set it as true and then in my event graph what I can do is get stop firing and a not Boolean and make a branch and if we shouldn't stop firing we should keep firing and if we should stop firing what we can do is just first set stop firing to false and uh cease full auto so it's going to clear and invalidate that handle if we run out of um if we run out of ammo we need to do a similar thing on our burst fire uh we can check on our burst fire we can check after each time we fire so I'm going to do it right here in my burst fire after fire check can fire and then make a little bit more room here because I can actually just copy paste these four right here so do a check and then if we should not stop firing we keep firing and if we should stop firing set stop firing to false reset that and call our cease burst fire this might seem like a lot but uh the the reason is um you know for example if you're doing your burst fire and you've only got two rounds left you want it to stop firing after it fires those two rounds you don't want it to fire off three rounds and and then stop you firing again you've run out of ammo so this is how I've set it up let's test this out run out of ammo and won't be able to fire again and I might just switch this to burst and if we've only got two it'll only fire twice and then it won't be able to fire again very nice okay that's working well let's Implement our reload logic and then I believe we're pretty much there guys ah I have created uh somewhat of a bug here so we don't actually want to to in our check can fire we don't actually want to set can fire as true um the reason is we're setting that as true when we finish firing anyway um we probably want to just set it after ceasefire so what we can do is just to be sure on ceas Full Auto we can set can fire as true again because that's going to stop us firing and if we've run out of ammo anyway uh we won't be able to fire again but we want to make sure that this is setting back to True um so we haven't set that in there we set it as true again off of cease fire very good the reason why that was problematic is because it would allow us to stack our burst fire and it would also uh mean that uh can fire would be left as false and then would never be set to True again you wouldn't be able to fire again so we shouldn't be able to stack burst fire we should only be able to fire at the rate of our burst fire and that's it so yeah that's all fixed now the reload logic is is more complicated than you would think um so right next to set ammo here I am just going to find the r key uh you should obviously not set up your inputs like this you should do an enhanced input but I'm not spending time in my tutorials doing an enhanced input for every single input you can work that out on your own so but basically for the record if this functionality is something that the player should be able to do with a button press then you should set up an enhanced input to fire off that logic um so what we're going to do here is just split our reload between our primary and our secondary so we can just copy actually this logic right here that checks if we're using our primary or our secondary I'm going to duplicate that over here and plug this in here and and I'm going to do it for the primary first and then we're basically going to copy paste and change a couple of things for the secondary but what we first want to do is get the class defaults so get class defaults of that slot of current weapons and then we can use the ammo type and the magazine size and whatnot um so the first thing we want to do is check if um the magazine is already full so we can get the magazine size here uh actually what we'll do is we'll get ammo and we'll check if it is less or equal to the magazine size and make a branch and this will be our first check so we'll only be able to reload if our current ammo is less than the magazine size if it's the magazine size uh plus one we won't be able to reload either so this will only reload if it's less or equal to the exact magazine size that makes any sense um the next thing we need to do is check how much unspent ammo we have in that magazine so we're going to set unspent ammo as ammo and the only reason for this variable is to hold on to that number momentarily while we do our reloading okay and just to signify that we're reloading I'm going to do some stuff here that you would probably later replace with anom notifies but um just for now to signify that we're reloading what I'm going to do is Place sound 2D so this is not at location this is basically for user interface sounds um and I'm going to find rifle reload sound rifle reload Q just going to browse to this one and yeah that'll do just a sound to signify that something has happened and we're also going to set ammo as zero so basically this is like the clip has been removed and then I'm going to put in a delay of 1 second and I'm I'm going to play that sound again so the clip has been inserted and then um and now this is where we're going to implement our reloading logic so basically I've just taken the clip out and I've Set uh I've set a variable based on how much was left in that clip okay so we can now check if this unspent ammo is um is greater than zero so greater than zero make a branch make a reroute node so this is a little bit neater that'll do um and this actually does get really quite complicated here so if this is true if we still have a bullet in the chamber we need to to set the ammo as the ammo remaining um plus one uh the magazine size plus one sorry so we'll grab magazine size here make a reroute node and pass this along the bottom here and going to make another reroute node here and I'm going to add one to the magazine size like so and this is our primary so we're also going to set our current primary ammo as that and if we didn't have one left in the in the clip we want to set the ammo as just the magazine size like so and ALS also set the current primary ammo off of that if that were the case um and after that we need to set the reserve ammo so we can set the reserve [Music] ammo as the reserve ammo minus oops subtract and try and keep this as neat as possible um we are actually going to subtract off of this the reserve the um maximum ammo + one we're going to subtract the unspent ammo off of that so unspent ammo right here can bring this down here like so and subtract that off of the reserve ammo like so if we didn't have one left in the chamber we also need to set the reserve ammo as the reserve ammo minus and one more subtract node here and this is going to come off of this one right here so the magazine size minus again the unspent ammo plug this in here so this might be very confusing but basically um if we had one left in the chamber we want to take the magazines size plus the one off of the unspent ammo where sorry take the unspent ammo off of that and set the reserve ammo as a reserve ammo minus that um yeah all very confusing little bit of spaghetti here um but yeah just if if you are confused just go ahead and and go back over this as many times as you need to um there's there there might be better ways to do this by the way this is a lot of math especially for blueprints um you know math is is better done in C++ but I'm not doing C++ tutorials I'm doing blue blueprint tutorials for beginners to intermediates so this is this is where we are this is it is what it is um um the next thing I'm going to do is update the reserve ammo in our ammo types variable so what I can do is grab the ammo type from the class defaults here and I'm going to put this out to the bottom here and break it make a reroute node to bring this along the bottom here try and keep it as neat and tidy as I possibly can and we're also going to get the ammo Types on our character whoops that's a set uh get ammo types get a copy at that ammo index so we're checking at the ammo index of the gun that we're reloading um and we can break that and up here off of the end of each of these we are going to set our Amo types so get uh ammo types here and set array element and I'm going to duplicate that make sure I plug in the Target array this one up here this one down here and make a little bit of space here because we also need to we need to plug in the index uh from this one here the same index here so I'm going to grab a re reroute node put that down there this over here get another reroute node and plug in both of these like so and then off of item we need to make the struct and going to plug in this here and drop this down make two of these plug into each of these set array elements and everything that's the same you can plug right in so everything except for Reserve so ammo index plug into both Max Reserve plug that into both has plug that into both and then Reserve is just going to be the reserve ammo right here so plug in the reserve here to the reserve here like so all righty that is that I believe so let's hit play let's use some ammo hit reload H that's not actually working why isn't that working ah we didn't set it up for secondary yet my bad um let's just test that it's working for primary primaries really quick before we copy paste all that so that's reloading it's going up to 31 because it had one left in the chamber if we use all of it it'll stop firing and then reload it'll only go to 30 we can reload again and go to 31 let's check that it's taking the right amount off of the reserve and it is and we can pick up more Reserve like so everything seems to be working correctly very nice so let's basically copy paste this for our secondary but we're obviously need we're obviously going to need to change one or two things and make sure everything's plugged in correctly that's off the true we need to get the class default s here we need to that's the ammo type and just double check that this is the magazine size right here and we need to check uh so this will be current secondary ammo we can alt drag current secondary ammo right onto that node and change it to current secondary ammo Reserve ammo and all of that should work fine because it's running off the ammo index changing the index of that one um that should be everything right there so the ultimate test I guess would be using someon reloading obviously picking up another weapon reloading and and switching between them and it should switch the ammo and the reserve ammo like so 18 and 62 31 and 70 sorry 21 and 62 did I oh yeah cuz I reloaded it 21 and yep yep cool I do believe that's everything we set out to do guys so like I said it is a lot but we got there in the end if this tutorial has been of any use or value to you whatsoever please hit like And subscribe and guys if you haven't already please consider making a small donation to the channel via the YouTube super thanks function and while I'm at it on that note a massive massive thank you to the people who already have made donations to the channel you know who you are and your donations have been very generous and very appreciated indeed it does help me crack on and continue to make quality content I'm very excited with where the Channel's going and the stuff I have lined up um so yeah uh thanks again and I will see you on the next one
Info
Channel: MizzoFrizzo
Views: 1,203
Rating: undefined out of 5
Keywords: ue5, tutorial, animation, video game development, blueprint, unreal engine 5, tute, true first person, shooter, shooting game, realistic, unreal engine 5.2 first person shooter tutorial, how to make a fps game, fps tutorial, TFPS, ue5 weapon system, unreal engine tutorial, unreal engine 5 tutorial, unreal engine 5 weapon system, unreal engine gun system, unreal engine 5 fps tutorial, ue5 fps tutorial, unreal engine fps tutorial, ue5 fps, ue5 first person shooter, how to add a gun
Id: 40kBSMxhvSs
Channel Id: undefined
Length: 80min 16sec (4816 seconds)
Published: Thu Jan 25 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.