PROPER Way to Inventory in Unity with Scriptable Objects! #4 - Stack Splitting

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back everyone so after popular demand the inventory series is back so i don't remember exactly what we did in the last episode it's been a while but what we have currently is the ability to click and drag items around our inventory um we can also drag these apples and we can also stack these apples these can't stack on top of each other instead we swap if i click away from the inventory it jumps back to the next available slot so that's all good and all but there is no ability to currently unstack items um so what i want to do today is the ability to right click on a stack say nine and we pick up five of those we pick up half the stack that's it let's get straight into it this one is going to be a simpler one so we'll see how we go [Music] so thing you want to load up first is your inventory manager script so i'm going to open that up in my code editor now what makes our system so good is the fact that it's so expandable we have these um button click events here so when we click on a button our code does the rest we have everything sorted out and our code will just do exactly what we want it to do so all we have to do is add a another function here for begin item move so we'll say begin item move half in which case rather than setting moving slot equal to that we're going to do a little bit of modification and what we have to do is a way to detect if we've right click it instead so i'm going to change this comment to we left-clicked just so we know um and now i'm gonna put an else if input dot get mouse button down um one that's the code for right click so we right clicked and i'm basically going to copy this as is and paste that here for now so that means i'll right click and left click at the moment we'll do the exact same thing um we don't want to do that obviously so we're going to fix that up at the moment now real quick so we're going to have an end item move single so if i'm holding a stack and i right click i only want to drop one item from that stack off and then i'm going to negate that from the stack i'm currently holding um so we're going to define that for definite that function our when we right click and we're not holding an item we're going to begin item move and we're only going to pick up half the stack so i'm going to copy this function that we have here for begin item move exactly as is and i'm going to call this begin item move underscore half and i want to call this function instead now when we call um up here i want to say begin item move underscore half now we can define this function however we want so every single time we right click on a stack what do we want to happen everything in this function right so that that's why our code is so good because we're using different classes to manage different things we're able to just set the code as is and it will basically work around whatever we want to do um and this is how you should be programming as well in your own games uh that said what we want to modify here is we say original slot equals get close to slot so this is the slot we have right clicked on if it's not equal to null and the um well if it's equal to null or the item there doesn't exist that means there's no item in that slot or the slot doesn't exist in which case for example we may have clicked outside the inventory um return false we're not going to do anything now if moving slot and now if that isn't the case then we set the moving slot equal to new slot class original slots um our slot class is defined here so in that constructor here we're taking in a slot class and we're just setting the item and quantity now we also have another slot class here which we can set the item and the quantity manually and that's the one that's the constructor i want to use so i'm going to say original slot dot get item [Music] and then original slot dot get quantity but i want to say divided by two now this may give us an error because if i'm holding nine items it's the quantity is obviously going to be 4.5 right divided by 2. so the quantity is an integer so that that will round it down for us so it will just cut it off at 4. the problem is that we will actually lose an item in the process because half of nine right if i split nine into two halves i've got four point five and four point five but the int will just cut off the point five so now i have four and four which means i lost one item in that process and that's not good so i wanna say math f dot rounds to ins and i'm going to make this 2f and put that in brackets so now that's going to give us 5 of those items now i can say sorry this should be sealed to ins and that's going to round it up like that now that means if i right click i pick up five of the nine stack right um if you make this floor two inch instead that will round it down so it will pick up four out of the five stack but i'm going to say round two into four now now we say original slot dot clear because we're we've wiped all the information from that slot we've you know picked it up um that's not what i want to do i want to say dot set quantity well sub quantity actually um and subquantity is a function which basically negates quantity minus equals the quantity we want to take off and the quantity i want to take off is literally um what i have here so um if i copy paste this exactly as is that should work moving item equals true refresh ui true okay beautiful uh now if i test this this should actually work when i right click on that stack of apples it should pick up half of it and when i left click to drop it then it should drop the whatever i'm holding in my hand okay so i've got five apples here i right click there's three apples left i place and i got two so i actually picked up the smaller half which is interesting splitting the big stack of apples the four evenly works now if i have nine and i right click i will pick up four so that's interesting because i thought i programmed it the other way around um i guess not uh because i got rid of the round okay this should be clt my bad i thought i'd change that there we go okay now that should work okay so we've got five apples i right click and i picked up three right six splits evenly perfectly if i have nine i'll pick up five beautiful now if i right click and i'm um holding apples nothing happens at the moment because we haven't coded that function in so i'm going to code that function in and as you can see these functions are actually quite small they're simple um and obviously get the job done so by being able to split all our methods into different functions we're able to do a lot more now our and item function is a lot more complicated and there's a reason for that is because there's a lot more conditions and things that could go wrong so what i'm going to do is copy this entire function again how we did um previously control c that and then i'm going to control v and i'm going to name this end item move underscore single save that and then i'm gonna change the function that we call here when we right click and item move underscore single there we go beautiful now we just have to program this now there's a few things if we want to end and to move on a stack that already exists or an empty slot we don't want to actually be able to do that so this function that we're going to be writing is going to derive off this okay so what this means is now if we click off the inventory to drop an item we're not going to be able to drop that item if we right click on a uh stack on a slot that already contains an item we're not going to be able to drop the item there because we don't want to be able to drop one onto you know a stack of swords and we don't want to be able to drop one apple uh and we can't swap our hand out because we're not dropping the whole stack so this function that we're writing here might actually be a lot more easier to do and might actually we might be able to copy this bit instead because we don't have to do any of the swapping stuff here okay i'm going to do that so i'm going to delete all this extra content and paste that in cool now we just have to manage this sort of stuff so what i need to do is say moving slot dot sub quantity because moving slot exists obviously so we're going to say dot sub quantity and we're just going to sub 1 because we're just going to drop one item original uh slots dot adds quantity we're gonna add one and original slots dot set item they'll add item hold up this might be add item yeah so we wanna add the item and the item we want to add is um so we don't actually need that line the item we want to add is our moving slots dot get item and we're going to add one of it cool now there's an issue with this we only want to be able to do this if we still have items in our hand right if not then um we don't want to run this function so what i can do is after we've placed it is moving item so if moving slots dot get quantity so we've placed it with negated one quantity if the quantity is now less than one so it's equal to zero um then we wanna say is moving item equals false because we're not moving an item anymore um and otherwise then do this refresh ui either way now and i have to move do i say movingstop.clear yes i do so i'm going to do that here as well so this and then moving slot dot clear that empties the item we have in our hand now that should actually theoretically work if i did that all correctly um now the chances i did that all correctly first try is quite low considering it's been a while since i've opened this project okay we have an error in our console i think or is that just unity being unity okay so i've got five apples i can't right click to drop any at the moment interesting uh wait there is something i've done wrong with the logic here if original slot equals null then do that we don't need to check for this though now one thing i actually forgot to do in the begin item move half is that if there's only one item it should pick up the entire slot if that makes sense so i'll show you guys what i mean i just noticed that when testing it there so that's our five apples i right click and then there's two apples here if i right click this there's obviously one apple if i right click this there's zero apples and that gives me a kind of weird infinite apple sort of situation so we need to fix that obviously so let's see what we can do so end item move no not there begin item with half um what i need to do is original slot the sub quantity that if original slot dot get quantity is equal to zero so there's none left then i want to say original slot dot clear um and that means we've picked up the entire stack so [Music] let's see how this works okay so i've got four apples um one apple right click and it picks up the entire stack i can't click okay perfect now i've got five apples here what happens if i right click to drop one it drops one and i got four in my hand i just can drop another got three in my hand drop another got two one one okay cool now i can stack these up again let's say i drop picks up half okay that makes sense i drop one and then can i drop it onto existing stacks no i can't so here's the issue if i right click on a stack of apples that already exists what i need to check when i end the item move single is if the slot i'm trying to add to um already contains that item so if original slot dot get item [Music] is equal to moving slot dot get item [Music] then well this should only check if original slots so if original slot is not equal to null uh your original slot dot get item is not equal to null okay so there's an item there and it's equal to each other that's fine then i need to do a little bit of a different logic else then i can do this logic here where i just add the item there okay the logic i'm going to do here is just add quantity right quantity i'm just going to add one to it that sounds simple enough right the sub quantity from my hand either way um if it's equal to that item then add one to the stack otherwise add a new item to that position um yes that makes sense if movingslot.getquantity is less than [Music] he's moving yeah okay that means we've emptied the items we have in our hand okay let's try this out okay so i've got five apples here i can drop one two three four five and then i have nothing else to drop beautiful so that actually works so [Music] yeah that works perfectly very nice i'm happy with that um let me know if there's any issues you guys come across when testing this i've been testing this for a bit and i can't actually see anything wrong with it they still have nine apples in total yeah we're not losing any data that is good perfect so now we have the ability to split stacks and you know drop a little bit of items not all of it which is good that's exactly what we want in our inventory so that's really good i'm happy with the way this is coming along in the next episode we're going to go into saving and loading so we're going to write a tostring function that we can save to our player prefs and then we're going to write a from string function which we can load from there and load the information into our inventory so that's gonna be a little bit more complicated but a lot of fun so tune in for that one i'll catch you in the next video guys take care and peace [Music] you
Info
Channel: ErenCode
Views: 4,931
Rating: undefined out of 5
Keywords: how to create an inventory system in unity, inventory system in unity, unity inventory system, unity inventory system tutorial, unity tutorial, make an inventory system in unity, how to create an inventory system, how to code an inventory system, how to use abstract classes, how to use scriptable objects in unity, creating an RPG inventory system, RPG inventory system in unity, full inventory system tutorial, scriptable objects unity, easy unity tutorial, unity scriptable object
Id: bp2eXjLGdJA
Channel Id: undefined
Length: 14min 58sec (898 seconds)
Published: Mon Jan 17 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.