Creating a Super Simple and Flexible Inventory System #unrealengine #ue4 #ue5

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign [Music] my name is decryption and whilst I'm working on my little demo level here I need the ability to pick up objects like this sword here so when I complete quests and stuff it will be added to my inventory but I currently don't have an inventory so when I run up and pick it up no matter what I pick up it just disappears and nothing happens so I need the ability to actually hold it in an inventory now there are a countless different inventories out there but the one I'm going to go with is the Skyrim based one based on an asset pack I already have I went rexa this asset pack I got from a Humble Bundle a while ago and I really like using it I'm not an artist so I'm going to compile something like this the first part we're just going to build the logic in so it's all there then the second part we'll look at combining the UI like so so the first thing we need to do is actually get our inventory up and running and at the moment I have this item here and it's part of a pickup sword blueprint here and all this is is just to house the model like so but it inherits from another blueprint I've got called BP pickle which stores a trigger which is where we run into it and it tells the player that they've picked it up and a rotating movement here and this just literally rotates the object the event graph for this is really simple all it does is check if the player has hit it if it has it calls the pickup method the pickup method checks if it needs to complete any quest of any kind and if it doesn't then it calls finish and then in the Finish method it just destroys the blueprint that's why it disappears so we need to modify this so it houses an item that we can pick up store in our inventory so the first thing we're going to do is I'm going to come to my content drawer and inside my items I'm going to right click Blueprints and I'm going to create a new struct called s underscore item and I'm going to open this up and in here we're going to store all of our items details so the first one will store is its name and I'm going to call it name and for translation I'm going to give it a type of name the next I'm going to store a weight and that will be an integer the final thing we'll need for later is a way to actually reference it back to the object so once we destroy it we need to be able to reference it again so I'm going to add another variable here and I'm just going to call this actor and in here I can basically search for any blueprint I want and I'm just going to keep it a type of act so I'm going to search for actor here and when you scroll down you'll see actor in the object types and I'm going to click soft class reference so this soft class reference will not load into memory unless we actually try to call it it should be a lot more efficient and I'm going to save that to test if it's working I can jump into default values I can set a name I can set a quantity and in your actor you'll not be able to pick anything in the world because we will have destroyed it but you'll be able to find anything in your content drawer so if I were to search for my sword for example I will see my custom sword there so I can click it and I can spawn it whenever we need to use it so that's working perfect the next step is we actually need to tell this pickup here that you are this item use the details we've just given you so I'm going to jump into my pickup script here and where I've got item name here I'm just going to delete it because we don't need it and I'm going to drop a new variable in called detail and this will be a type of our s item and tick the little eyeball so we can set its details I will tick expose on spawn to help anything out later and we can save it now I'm going to jump into my sword here because I have a blueprint specific for the sword so we can do more stuff with it later and you will see in the class defaults we will be able to see the details here so I can open this up and start populating it so it'll be a sword the weight will be 14 randomly and the actor will be itself so pick up sword I'm going to save and compile that now we need to set the inventory up on working now I don't want the player to be the only one in the entire world with an inventory just like games like Skyrim or Grand Theft Auto each person could be holding some specific items so I need all of my NPCs to have an inventory as well so what I'm going to do is create something called an actor component now if I were to jump into my player here you will see your components down the side underneath all of your main parts you can't drag a component into the world you can't touch it or do any it was it but you can reference it if you hold the object that that holds the component so in my content drawer here in blueprints I'm going to create a new folder called components and in here I'm going to create a new blueprint and it'll be a type of actor component and I'm going to call it bpc for blueprint class inventory as you can see it's got the same icon and now that we've created this as a component we can come to the play and add it directly to them and now we can give them an inventory and I can do the same for all my NPCs as well perfect so in the inventory here I'm going to delete the starting methods the first thing an inventory needs is slots it needs a way to store items so we need a new variable for it but we need a specific variable that'll store the item and the quantity so just like we did before inside the components I'm going to create a new blueprint and it'll be a type of structure and I'm going to call it s inventory slot in here just like we did before we can add our variables so I'm going to add the first variable of item and this will be our s underscore item and then I can add another one under this and it'll be quantity just like that and we can jump back into the ACT component I can add a new variable and I'll just call this slots for inventory slots the type will be S inventory slot and I'll tip the little eyeball and then I'll set the type to be a Mac and we're going to use a map because a map contains two values a key and the the actual data the key will be our item name so it'll be unique so it'll be sword or Shield iron shield stuff like that whereas the data will be the value will be our struct so I'll set the value to be S inventory slot and I'll set the type the key to be a name just like so and then I'm going to come over here and I'm going to create a new function called app and this will be where we add to the inventory so the first thing we need to do when we call add is we need to tell it what item we're adding which is where we'll pass our item details in and how many of them we're having if you don't want to do the how many you're adding part you can just default it to one or whatever quantity you want to so the first thing I'm going to do on I'm going to click this function and I'm going to add an input and I'm I'll call it item and this will be a type of s underscore item like so and then I'll add another input called quantity and this will be a type of integer if you don't want the quantity just ignore it from here we need to check if the slots already contains the item we're referencing so I'm going to drag the slots out and I'm going to from here I'm just going to do fine like so and then fine you need to give it a key and it will tell you whether it exists or not well we already know what the key is because we've got it from our item name so I'm going to come here and I'm going to right click and I'm going to get our item because we're in a function we can reference these variables input these input variables anywhere in the event graph so I'm going to drag from the item and I'm going to do great and then I'm going to drag the name into the fine like so and this will give us a Boolean and the actual item if it finds it so I'm going to add a branch Now by holding B and clicking connect up my condition and my exit pin so if this returns true it's found the item it already exists so what I can do in that case is I can drag this item off up here and I can do set members in inventory slot here by clicking this you can tick at the side what you want to modify or change in another case we just want to change the quantity because we've got a new quantity and I'm going to connect this up to the true pin there we go so what we need to do now is we need to actually get the quantity from the slots so from this one here I'm going to drag down and I'm going to do break as well and this will basically tell us what it's already got and we've got a quantity so I'm going to drag off I'm going to do a plus and I'm going to add our new quantity from here by doing get quantity and then we can add it together and then connect it in the last thing we have to do now that we've update the quantity is add it back into the slots now you might think when we're updating it not adding it why would we add it back in the add key here the add node will also check if it exists so if the key that we give it exists then it will simply update it otherwise it will add it new so I'll drag the struct into it like so and then for the item name I'm just going to do get item which is the input variable we gave it I'm going split it and just do item name like so and then I can highlight all of this and give it a comment of add new item like so the next step is adding the item if it doesn't exist which is again super easy so I'm going to drag the slots out here because if it's hit false we already know it doesn't exist and I'm coming from here and just type add like so and then we can connect up the XX pin but the key we can just drag in the item name from there like Sim and for the item I'm going to right click it and I'm going to do split struct pin and this will give us our item and our quantity which we have both of so from the item I'm going to drag out and I'm going to do get item and then for the quantity I'm just going to do the exact same thing but for get quantity like so and that's it that will successfully add it to the inventory if you are doing a weight based game where you sh where you check if they're overweight or if they can't add more than x amount of items you could add something before or after this adding all the updating to display the message to the user but for me I'm all right for now so as a quick test to see if this is actually working I'm going to go back to my BP pickup here and where we call finish before I destroy the item I'm going to drag this across here and just under the finish I'm going to get the player pulled like so and I'm going to cast it to my BP player and if it casts successfully then I'm going to drag from here I'm going to type add inventory and under bpc inventory you can see our add function will pop up so I can drag this out format it nicely connect it all back up and you will see it is requesting an item and a quantity well the details we already have so we can add that in and for the quantity I'm just going to add another variable here just solely for the pickup we don't want to add it to the details I'm going to set it to an integer and I'm going to connect it to compile and save the last thing I'm going to do is give the quantity a default of a one so everything by default has one field one and now when we test the game and we run up to the sword you will see absolutely nothing happens the sword destroys but we don't know if it's actually in the inventory or not so we need to add a little debug window until we have done the UI so in the player here I'm going to come right down to the bottom and I'm going to type in debug I and the debug I keys are specific keys that will only work in the editor and I think it works in the development release as well but it's a key that only you can use so I'm going to search for debug key I here for inventory when it's pressed I'm going to get the inventory I'm going to get the slots and then I'm going to get the keys like so then we can connect the keys up I can run a for each Loop over this array and then from this for each loop from the loop body I'm going to drag out and do a print string this print string I'm going to drag off and I'm going to do an append like so and then in the array element I'm going to drag it into the top one which will print out sword then in here I will put a space Dash space add another pin and then from the slots I will do a fine I will connect up the array key to there so we can find the element and I will split the struct pin here where I'll drag in the quantity so this will tell us sword and it'll say you've got five or three or something and if I go back to Romancing and start the game you could see if I press I nothing will print out at the top but if I run out and pick up the sword and press I now you can see we've got one sword like so power to come and add some more swords but I'll also come in and add a different type of Sword but it should be sword two and now if we try it when we're in and we pick up the first sword and we tap the I key you see it says we've got one sword if we pick another sword up and press I it says we've got two swords three swords perfect and now we can grab this sword which is a different one and it says we've got three of Sword one and one of Sword two perfect but what if we want to drop an item so inside our inventory again here I'm gonna go to the functions and I'm going to add a new function of drop and this will be almost identical to the add function that I will add an import of type item and it will be S item and it will have a quantity to say how many you want to drop again this bit's optional and the first thing we need to do for safety is to just make sure we actually contain the item so I'm going to get the item I'm gonna break it I'm gonna split it and then just drag the item name in I'm gonna hold B and add a branch and if we do have the item then we can remove that quantity of it so I'm going to drag off from this item here and I'm going to do a break like so I'm going to drag off from the quantity and I'm going to do a minus a subtract and I'm going to add in our get quantity like so so it'll get the item and it will minus the quantity from it from this I'm going to drag off and I'm gonna do it double equals I'm going to do a less than or equal like so if it's less than or equals zero it basically means we've run out of the item we don't have it anymore so we can remove it from the inventory now so if it's true that we're less than zero I'm going to drag the slots and I'm going to do remove and in the true the key that we're going to remove is simply going to be our item name and that will completely remove it from the inventory so we don't have it anymore otherwise it'll report we've got minus one or something like that however if it's false that means we do have the item and we still have something to stop so for example if we have five we remove two we still have three we need to add it back into the inventory like we already have so I'm going to cross so the slots find here I'm going to drag this off here and I'm going to do a set members in slot just like we did before on the false I'll drag it into the set members I'll tap the quantity option here and I will drag in our new quantity from here and then from our slots we can simply do add just like before if it already exists it will simply update it I can drag that in there and then our our item details will be our struck out so and that's how we remove an item so in order to test this I'm going to jump back to my player I'm going to add a new debug key of o i I don't know why o is next to Y it makes sense to remember so the first thing I'm going to do is I'm going to go off and get our slots here and I'm going to actually check if it's got any keys because if we've got an empty inventory we don't want it to crash the game even in development so I'm going to drag from the keys and I'm going to do a length check if it's more than zero I'm going to add a branch connect it up if it hits the false on this Branch it means we've got no items we're not going to do anything this it hits true then we're just for test purpose I was going to get the first item from the inventory and just drop one of them so I'm going to come off and get us our slots here and I'm gonna do a keys connect it up to the true and then from the output we can just drag off a get node here which will get us the first item it finds and then from the slots we can do a find and we can connect up the key like so so that'll give us one item at the very top of the inventory its entire inventory slot you can break the item slot there to get the item and then we can drag our inventory in and we can do drop like so the drop wants a quantity so we'll just say we're going to drop one and then the item I will connect up the item like so and then the exit pins so now every time we come off and press the o key it will try to drop something until we have nothing so we'll come in and I'll press the o key and you can see nothing's happening because we've got an empty inventory but if I pick the first item up it'll say we have one sword so if I press I I can pick another sword up and we've got two swords if I pressed Okie it's just dropped one so now if I press I we only have one sword if I get the other two swords like so press I you can see we've got two swords and one sword two but if I had the o key and then tap the inventory again you can see we have one sword and one sword too if I press OK twice we have nothing in the inventory anymore fantastic so now it's saving our inventory we now have a way to add to the inventory and then we now have a way to drop the inventory so the only other one we can do is check function and the check function is primarily going to be used for questing and stuff like that to say have they got this item in the inventory so I'm going to add a new function called get and then in here just like before we can have two parameters the first one being item big type of s item and the next one being quantity being integer the quantity is optional and it will only check if they have that amount of the quantity or at least this quantity so it'd be good for the get before you sell an object if you say you're going to sell three objects do you have three objects I'm going to jump to the drop method and just like we keep doing I'm going to copy all of this initial branch and the break selection to say do we actually have the item in the inventory we're trying to drop this is just in case somebody's trying to exploit the game or a developers not done something it's a safety check like so so that'll actually check do we have the item if we do have the item then we need to compare the quantity and say do we have at least this quantity so from the quantity here on the break I'm just going to say greater or equal to and then the option from here will be the quantity so I'll just do a get quantity and I will plug it in now we need a way to return whether or not we have that item so I'm going to come over and click my function here and in the outputs you can expand it and add an output so I'm going to add a single output of a bull saying has item and then I'm going to set this to a Boolean like so that I'm going to add another item of had item with the quantity so then in the code you can distinguish and show specific error messages saying you have decided some but you don't have enough over the quantity and you can see it's either the return node for us so I'm going to drag this down to this bottom one that has item we already know is true so I'm going to tick it and it has item with quantity I'm just going to drag in our Boolean here and this will be connected to the true like so so if it comes down it will check if we have the item if we don't then we need to copy the return node and we'll just untick both like so so if it doesn't have the item it'll just return two falses if you do have the item on then it'll return that you have the item and then it'll check whether you've got the quantity so this entire thing has three return States everything's false everything's true or one's true and one's false just like that you can never have an item but have the quantity so that's we can ignore that state so now we can save and compile and we'll jump back to our player to do the final debug key so the only other option next to O is the P key so I'll do debug key P I will get our inventory in our keys just like so I will drag in the inventory and the slots I will do a find like so and we want to find the sword do we want to find the sword in our items so I'm going to add a branch here connect up to it and then just for dummy sake we pretend we've got an item there and then from our inventory here we can do a check and I will call the function get like so add it into the true I will break this pin here and give it the item and we want to say do they have two and then from this get I can do a print string from the in string I will append then we can drag the two variables into it and this will tell us do we have the item or not from the false tag I will just drag down add a string and say you do not have a sword so if I click P key now you'll see it says you do not have a sword I was like oh okay so if I come down here and pick a sword up like so and press the P key it will say true false so we have a sword but we don't have two it's our pixel two up you'll see it'll still say true false we only have one sword if I pick two of them up press the I key you can see we have three swords so if we press it peaky now it'll say true true because we have the swords so if you were to go up to a shot and try to sell them three swords this can turn around and say you do have three swords otherwise if we drop a sword pressing the o key it will now say you only have one sword so we can reject it and that ladies and gentlemen is a super basic concept for an inventory you can massively expand this to save and load add it to your game instance and then in the next tutorial we're going to look at adding a UI AI very similar to the Skyrim one so we'll add categories We'll add details to it and we'll try to make it look really really nice so I hope this helps ladies and gentlemen thanks for watching please like comment and subscribe and I'll see you next time [Music] thank you
Info
Channel: D3kryption
Views: 4,176
Rating: undefined out of 5
Keywords: UnrealEngine, UE4, UE5, gamedev, gamedevelopment, inventorysystem, tutorial, gaming, indiedev, indiegame, gametutorial, gamedesign, gameprogramming, gameengine, gamedevelopers, gamingcommunity, gamingtutorial, gamedevtips, gamedevhelp, gamedevs, unreal, engine, epic, five, linux, manjaro, unreal engine 5, new version, 5.1, unreal 5.1, unreal engine 5.1, ue 5.1, narrative, unreal engine, reubs, narrative by reubs, reubs's narrative, discord, doors, opening, sequence, world, dialogue, quests, text, ines, speech, lines
Id: ew82MrRd0c0
Channel Id: undefined
Length: 19min 55sec (1195 seconds)
Published: Wed May 03 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.