Drop Inventory Items into the World - Unreal Engine 5 - Inventory System #7

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone what is going on today I'm going to show you how you can drop the items from your inventory here's the preview so I'm back in our inventory system picking some items up and then I can press I click on an item and press e and I can do this multiple times and as you can see my items just dropped here back in our project the first thing we want to do is go to our input actions and create create a new input mapping context call it imcore menu controls add another one called IMC inventory controls this will be used for activating and deactivating um key bindings inside the inventory or inside the normal gameplay open up your game input and then let's copy the IIA inventory and let's move it to the menu controls add a mapping here open this up save paste as you can see it moved this one here and let's copy this one and paste it too make a new and then paste it there all like this don't forget to remove the I from the game input so we have the menu controls open the IMC inventory and here will be our drop so for that we want a new input action so go to input and type in unpo action uncore drop item open this one up and inside here first thing we want to check trigger when paus because our game is paused when we're inside the uh menu and then we want here again the press and releas least one like so and then let's go to the inventory controls and here let's get the drop item and I will press the E key with the select the key value selected click on it and press the E key now let's create a player controller where we map those things so go into the um into the word settings and then create a new player controller I will call this piece ccore game you can give it a name like your project or something like that so inside here we go to the event craft and from the event begin play we also need an enhanced input local player subsystem keep in mind this is only for 5.3 in upwards so if you have an older version you need to go with the um classic way but for the new newer system we want to do it like this so we add here some mapping contexts so we say when we start the player controller we add the uh default we want the the menu controls to be assigned and we also want to have the game input the inventory controls uh we want to uh map them when we need them so at the moment we can't press e to drop the items and that's exactly what we want because what we want to do here is we want to have a function which is called register context and we want to have a function which is called the register context we can call these two functions when we open our inventory or whatever system you have and we can then say remove context so for example the player can't uh trace for new items when he's in the inventory so we just remove it same goes for uh the register context we copy this over and there we say add mapping context you will see in a few seconds what I uh try to achieve with that go to the character where we have our open and close inventory here and what we want to do here is get the player control roller we then cast to the pcore game and then we want to register and de register some contexts so at the moment when we press e we uh pick up an item but we want to drop an item when we're inside the inventory so we register this new inventory controls which has the drop item e and we deregister the context that has the uh pickup logic which is this one so here we have game input and I just saw that we have to remove here the IIA inventory with the game pad button uh I missed this one so uh remove it and then what we want to do here is uh copy this go back to when we actually uh close the um inventory and then we want to switch this up so here we want to have game input now and here we want to have the uh inventory controls so yeah and for the communication go to widgets inventory and inside here we now want to have our input action um drop item like so now we need a new variable called player ref will be from type BP third person character like so and from that we get our in AC inventory and then we want to go to the AC inventory and here we want to have a new function called remove single item and back into our inventory uh we call this function like so and make sure to connect it to the completed one also we want to make sure to have this on instance editable and expose on spawn so we can set this in our BP third person character refresh the notes and as you can see we have now the player riff and this one will be self compile and go back to the inventory okay now let's work on the remove or drop item um here we want to have first an if branch and we want of course have our inventory and we want to find the item that uh we want to remove like this and if we find it we want to do something break the found item and get the amount we want to check if it's greater than than one so if it's greater than one we yeah we decrease the item amount and if it's one then we want to remove the item completely from the inventory so for me the fastest way is to set members that member in s item data and click here on the amount from the amount we want to uh decrease this one or substract one and then we get the inventory and we add again and what the ad does it says here that if something already uses the key it will be overwritten so we use this small trick here and the one we want to add is the key so use this local variable this that comes from here and then just get the struct out and put it in so it will be overwritten with one um amount decreased and then I like to return with these Staters like uh item fully removed which will be false and then we want to have the new item data for the inventory so we don't need to um refresh the whole inventory system on the uh yeah on the uh widget like so and for the fully removed part um we want to remote this to a a local variable deleted item so we have it uh saved somewhere in case we need it then we want first to remove the item from the uh item inventory and again you also use the key and like before we want to have the return node this time we use item fully removed and your item data will be null go back to your inventory and here now let's get in is valid go back to your inventory and here let's get the last click slot and let's get the is valid and let's check if this one is valid before we remove the item so now what we want to do is we want to get the item data and then we want to break this and we want to connect the name to the key then from the item fully removed if the item was fully removed we want to um get our scroll box with the item container and we want to remove child from it so we can remove the item slot item connect this to the true and this one will be our last click slot like so and for the next part we need to go to the item slot again go to the event craft and inside here add a custom event call this update item data can also make a function but it doesn't really matter so get the item data and from that we want to set it from the outside compile and save go back to the inventory and inside here we want to um get the less click slot and from that one we want to update date item data and this one will be our false branch and the item data we want to update is the new item data from the remove single item so the item that has the amount decreased by one and what we also want to do is change the visibility of the vertical box item info so we set visibility and when we remove the item we want to set it back to Hidden what you also can do is after you have removed the uh item you can set the last C slot to null so we removed it so yeah just make it null and after that we want to set the inventory or we want to update the inventory with the inventory from the AC inventory get AC inventory and then get the inventory like so and last but not least we want to update the selected item when we update also the clicked item so get the selected item here which is the item that uh we use for the info box and we also paste in the new item data then one important thing I forgot to mention is go to your player controller and inside here search for tick and make a check on the tick even when paused so we can actually add context while we in our inventory because we paused the game there compile and one thing I also saw while testing is that I uh picked the wrong Branch for the false part so we need the second one if it's less than one we want to remove it and not if we didn't um actually find an item so this was wrong and now we can compile and start and test it out open your inventory click on the iron press two * e and as you can see it got removed and also removed everything else and the last part of this video should be the item uh drop into the word so for that uh let's go back to the inventory open our item data and add a variable called item class and we pick our pick up uh Master item as class reference then go back to the inventory and inside here we want a new function called Trace from above and for that we make a sphere Trace by Channel like so and we get the connected actor and from that we get the mesh on which we get the world [Music] location and this one gets an ADD and this will be our start position so what we also want to get is the follow [Music] camera and from that we want to get the forward Vector uh we multiply this with um I would do a float value here uh which I will pick for 200 so this will get uh the player location as the start point and the forward Vector from the camera uh plus 200 as the uh position where we drop so let's break the vector and I want to uh split this one so we can add the X and Y value and for the Z we can choose an own value so this will be our start position in front and above the character and our end Position will be uh the start position with the same X and Y values but here we want to trace beneath the player so something like minus 1,000 this will be our end point Point Let's Get in radius of five and let's make for duration and if we hit something we want to break whoops not this one but we want to oh I see I have a multisphere change it to sphere Trace by Channel solo so we don't get here uh problems so like this and now we can get a single break hit result like so don't forget to change those values again and then let's return when we hit something we want to give the impact Point position and if we don't hit something we want to give the trace end this is just an example you can use your own Trace mechanic um I will put this in here so that we can see it in action so play pick some items up and now let's drop this item and as you can see there was the impact point and there the item will spawn so the last thing we want to do here now is get uh a new function called spawn [Music] item and inside here um we have a spawn actor from class we connect the class to the spawn item and for that we make transform oh not this one make transform this one and here we can get the uh position in so call this position make sure that it is a vector and then we want to add uh a little Z offset so we don't clip into the ground and then back here we can now get our spawn item we connect the impact point and then from here we click the item and we get our new item class we can copy this and then we can paste this here in like so and the same goes for here connect this and then we um have to edit our weapon Master go to the uh sorry not the weapon Master but the pickup master and inside here we need a new class or new variable called item class and this will be our BP H sorry aore Master pickup item but as a reference like this connect [Music] those H yeah make it instable editable instance editable and expose on spawn and then we go back to our iron and inside here we can now give it a reference to self like so and you have to do it for all of your items um but we will just now test it this will take too much time so open your inventory click on the iron press e and as you can see the item drops in front of me keep in mind that these Trace from above is not suitable for a game where we have a lot of um houses or other things where we can hide under because item would spawn above the player which is not correct so yeah this is just for testing and in a future video I think I will uh yeah make some uh traces that uh make random spawns around the player and we are finally finished if you have any question leave them down in the comments if you have any suggestions for future tutorials come join our Discord and share your ideas with us have a nice day bye
Info
Channel: Pyro Dev
Views: 1,428
Rating: undefined out of 5
Keywords: Unreal, engine, ue5, inventory, series, system, drop, items, world, Drop items into world, IMC, Tick when paused, trace
Id: fSg4GwOg2aA
Channel Id: undefined
Length: 20min 54sec (1254 seconds)
Published: Thu Nov 23 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.