Minecraft Crafting System in Unity!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we're going to create the crafting system from Minecraft we're going to have our 3x3 we're in position the items according to a recipe and get our output that we can use what's begin [Music] hello and welcome I'm your code monkey and this channel is all about helping you learn how to make your own games with enough tutorials made by a professional indie game developer so if you find the video helpful consider subscribing okay so here's what we want to create we have the UI set up in here just like in Minecraft so we have a simple 3x3 grid as well as an output slot then down here is the current inventory where I have a bunch of items this is using the inventory that we made in a previous video then I can drag and drop items which again was also covered in another video and using this I can place items into the crafting window now the crafting works based on recipes those recipes are based on specific items being placed in specific positions in the crafting grid for example I place the wood right down the middle now just like this there's nothing on the output since this is not a valid recipe however now I can place this wood on the position right beneath it and there you go now we have a vellum recipe to output a stick so this is the recipe that we define in order to make a stick we have two pieces of wood one there and one in there now when I grab the stick and drop it in my inventory there you go over the piece of wood were consumed so the entire recipe has its cost and now another recipe is putting two pieces of wood on the top the other with a stick underneath it and there you go we have a wooden sword and if I now make another stick and then take these two out place the stick in there and the two diamonds on top and there you go now we have a diamond sword so here we have our fully functioning crafting system and now here on the side we also have the player equipment window this was also made in a previous video so right now my character is only attacking using punches and kicks however now I can drag and equip the wooden sword and there you go now he's using a wooden sword and I can take it out to replace it with the diamond sword that we crafted and there you go now we have this nice sword so over here you can see a whole nice ecosystem built from a bunch of separate systems so we have our inventory then we drag and drop items to place them into the crafting system the crafting system receives items and according to a recipe makes something and then we can take our crafting items and equip them into our character everything is written in a clear way so it is very easy to add more recipes and more items this video is made possible thanks to these awesome supporters go to patreon.com/scishow to code monkey to get some perks and help keep the videos free for everyone alright so this is our goal let's get to it okay so here's our starring scene I have my player character that I can move around and I can attack over here is the inventory and the equip you want right now there's nothing where you can equip so let's craft some items let's begin by making our crafting system script so we make a new script and code our crafting system now in here and let's get rid of mana behavior since we're not going to be using these functions instead let's make a simple constructor so in crafting system okay and now let's see how we're going to represent our grid for example we could use the grid system we created previously if I was making this system to use in one of my games that's exactly what I would do however in order to keep this video simple let's make a simple basic array in step so up here we make an array so we're going to have a private it's going to be of type item so an item array and call it our item array the atom class is in here this is the class that we created when we made the inventory system so make sure you watch that video so here we have our atom array however it's going to be a multi-dimensional array and now on the constructor we're going to initialize it and for the size let's define a constant so we have our grid size and we initialize alright alright so here we have our 3x3 crafting grid now let's make some helper functions to help work with this grid all right so here we have a bunch of functions to interact with our crafting system grid so you can ask if a certain position is empty we can get the item in that position certain item in that position increase the item amount decrease it remove an item and try to add an item so you first test if it's empty then we set the item if it's not empty then we test if it's the same type if so we increase the item amount and if not then we cannot add it so we should have pretty much all the actions we're going to need to implement with our crafting system now let's test this out so let's go back into the editor here we already have a testing game object with a testing script this is where we're making the connection between the player and the UI inventory and the character equipment and the UI character equipment now here let's construct a crafting system then let's create a new item so we have a new item and then we go into the crafting system and let's set the item and finally we do a debug log on the gift item on this position and now it's s and we should be able to see this time and located on the item on the zero zero position here we are any of the consumed essay we have a diamond placed on the zero zero position awesome so we have set up the basic logic for our cutting system now let's make our UI visual so here in the canvas let's build it so we make our game object call it our UI crafting system all right so here we have our crafting system UI we just have a simple background then we have a output slot here on the right and our 3x3 grid of input slots as you can see they are all named 0 0 1 0 2 0 and so on and then on top we have an item container in this item container is where we're going to in Senshi 8 the items that are placed on the grid okay so now let's handle the code let's make a new script for our UI like crafting system and we drag it in there okay now in here let's make a grid and locate all of our grid positions all right so here we have our references for all the various slots as well as the output slot now let's make a function to be able to spawn an item on a grid position so here we're going to instantiate that prefab so let's add a film for that reference and back in the editor we drag it okay alright so we have a function to create an item we instantiate our prefab wien located right on top of the slot transform position and we simply call the function to set the item okay so just like this we can now create an item on any position in our grid and now let's also make one to create an item on the output and just like that okay so now let's test it out let's go up here and let's create some items all right so we should be able to see a diamond a piece of wood and a sword made of wood let's see any of there we have our items correctly placed in our grid so we got a diamond there wooden there and a sword in there all right awesome and again since we're using the prefab that we made previously we can simply grab and drag and drop these items all right so far so good now my 10 on being able to take an item and drop it on to the crafting window so first let's make a script out to our slots that won't handle the item drop so here we make a new C sharp script color our UI crafting items like and let's write the script onto every single one of our Smuts so crafting Adam son yep right now in here we're going to do just like we did on the equip inventory video so check that out if you haven't seen it yet essentially we're going to capture the eye drop an alert event so inside unity engine dot event systems so here is the script that we made in the character equipments what now we could probably make this more generic and use it for both scenarios but in this case let's just copy face the same code so we're going to have the same behavior alright so on drop we're simply going to fire off an event okay there it is very simple now let's go back into the main UI script and now in here we need to know on which word position we dropped an item so in here for example we could subscribe individually to every single item slot however that would result in a ton of duplication code so instead of let's use a single event but we're going to pass in the X&Y into our slot so first let's go into our slot script and in here let's receive the X and the y okay we have this function test them and now over here on our item drop event Rx and let's add some more so let's set a min for the X and then for the Y okay so we now have this event being fired and it also tells us the X&Y and now we can go back into our crafting system and in here we can go into our slot transform array we get the component of type UI crafting slot and in there we subscribe to the on item dropped event all right so anytime we drop something into any slot it will fire off this event and over here we have the Adam dropped event args so this contains the X and the y and here just for testing let's do a simple debug log and we also need to call the set X Y function so in here alright so we can't set X Y we subscribe to the event then on the event we do a debug log which shows the item the X and one alright let's test ok here we are let's pick up this diamond and drop it in there and if there you go it says we dropped an item on position to zero which is indeed that one now drop a piece of wood in there and there your wood - - alright so we have everything working correctly awesome now that we have the visuals working let's interact with the crafting system so we're here on our UI script let's have a function to receive our crafting system all right we just set it like this and now we can go back into our testing script and here we can make our connections so we first make the crafting system now let's make a sterilized feel for our UI crafting system and we concept and we trust in our crafting system all right so now we have the UI being aware of the crafting system and now over here when we capture any dropped let's tell the crafting system to try to add that item into that position so we can use the function that we made previously try add item and we pass in the e-dot item into this X and one alright so just like this it should be working try to add an item into our crafting system on a specific position however if we do just like this then we won't actually see anything so we need to update our visual let's make that function in order to update first let's destroy everything in our item container which is where we're spawning our items all right so first we clear off all the old items then we cycle through our entire grid we ask the crafting system if it's not empty if not then we create an item on EDX that Y and we pass in the item that is currently in there all right so this function should be working for updating our visual let's come out when we set the crafting system and now we also want to call it whenever something changes so we need to be notified of that so let's go into our crafting system and here when something changes let's fire off an event all right so here we have our event called on great change and we invoke it when we set the item or modify the amount and now we can go back into our UI and in here when we receive it let's also subscribe to the andrei change event and when that happens then we update our visual okay so all this should be working let's remove these items for testing and see if it's okay here we are in yep our crafting window is empty now let's drag the wood drop it in there and there a giveaway place to win in that position track diamond place it in there and there you go just like that so now we can drag and drop items into correct positions on our grid awesome alright so now that we have the crafting system working so that we can drop things in various positions on the grid now it's finally time to handle making our recipes now there are many ways we can define our recipes we can make it visual or code based or even have an external tool here let's go with the simplest way and then you can expand upon it to fit your own needs here on our crafting system let's define our recipe alright so here we have our stick recipe we essentially create a grid of the same grid size then we simply place the item types required so as you can see it's none on most of them except right here down the middle we have two pieces of wood alright now let's make a function to try to get a recipe output so we settled through all of our recipe positions and see if they match with what's currently on the grid all right so we cycled through our entire grid we compared it against our recipe if the recipe should have an item in this position then we test if it's empty or the item in there is different from the one that should be there then this is an invalid recipe so we return none and if you go through and all of them are valid then we return our steak recipe all right so here's how we're getting our recipes and now in our crafting system let's also make a field for the current output all right so then we have a function to create an output so we test against the recipe if the recipe has no output then the output is no if not then we create a new item for our output all right so whenever we change something in our recipe and let's try to create that so over here when you have the set item we do all this and we try to create our output so now in order to have the UI update let's make a function to get the current output and now we can go back into the yuan and here when we update our vision let's also ask if the crafting system if the get output item is not known then we can create our item output all right so just like this everything should be working and let's test ok so here we are in our crafting system start off empty ok now let's pick up this piece of wood and drop it right down the middle and yet there you go the wood is there but there are still no output so the recipe has two pieces of wood one above the other so now let's drag this one drop it in there and there you go there's our output successfully created alright awesome so over here our recipe is working correctly so now we can take this stick and drop it over here into our inventory and if there it is however you can already see an issue we crushed this stick but essentially didn't have any cost like here we still have five and five so something was crafted but nothing was consumed what's all that so here in our crafting system let's make a function to our recipe items and here what we're going to do is cycle through our entire brand and we're going to remove an amount from each of them all right that you do it so now we need to call this function and now the way that the items work in order to work with various containers is each container has a higher mulder interface here's the moments to remove item add item and can add item so when an item goes from the inventory into the crafting system or the character equipment it gets removed from the atom holder that was holding it so here that is being implemented in the crafting system and it's over here that we know that we removed the output item when that happens we create a new output and when we do then let's consume our recipe items ok so let's test ok so here we are and we have our wood so let's go and drop it in there and there you go stoned our recipe now drop it in there and there you go download recipe we have our stick and if I move it away there you go invalid recipe no stick alright so put it in there there we have our stick no let's pick it up and drop it in the inventory and there you go we have an stick in our inventory and the items were consumed so we have our sick and just four piece of wood now drag another one there we go another stick now we just got three just have to you just have one now drop it and there you go we built five sticks and we no longer have any wood so you consume all of it and we no longer have a valid recipe awesome ok so just like this we have our crafting system pretty much complete let's just set it up to handle multiple recipes so in our crafting system instead of having just one steak recipe let's have a dictionary of recipes ok so we have a dictionary with the key of item type and a value of item type all right okay so we instantiate our dictionary then we create our recipe just like we did and we set it on the dictionary on the key stick alright that should work now down here on to get our recipe output essentially we're going to second through all of our recipes all right so we're cycling through all of our keys in our dictionary then we do the same foundation as previously we have a boolean we start off with true on the complete recipe if we find something that is different then it is not a complete recipe so as we reach down here if it is complete we return this item type if not then we go into the next dictionary recipe and if we go through all of them and we don't have any return then we return none all right so this should be working on exactly the same except working with a dictionary let's just test it out to make sure it's all works okay here we are only track one there and one in there and there you go we have to stick move it to the side and no stick okay so far so good so now I just need to add all the other recipes all right so here are the new recipes to find so for the wooden sword we have a stick down the middle then two wood above it and so none on the other sides and for diamond we have a stick into diamonds okay now let's try them out okay so first of all we have our wood and diamonds so now first we're going to need a stick so much drag they wouldn't there and wouldn't there there you go we have our stick so take out the stick now to make the wooden sword let's what you would above and now a stick below it and there you go we have our wooden sword so let's drag it and pick it up and there we go we have our wooden sword now for the diamond first we need a stick so let's put and pick up another stick now take out the wood and replace it with two diamonds with a stick the knife in and if there you go we have our diamond sword alright so there it is just like this everything is working correctly okay so here's the character equipment sister that was made in a previous video so again let's create our items alright here we have the wooden sword and the damn sword and right now the character doesn't have any sword equipped so we're only attacking with punches and kicks and now let's equip the wooden sword and there you go now he's using the wooden sword and I want to equip the diamond sword and there we go we have the diamond sword alright so here we have our fully functioning crafting system so we start off with some very basic resources then we build some complex items using the crafting window and then we combine those items with a character equipment in order to equip them so again here it is the whole thing working and as you saw the code is very simple so you can easily expand this to fit whatever recipes and items you want this video is made possible thanks to these awesome supporters go to patreon.com/scishow to code monkey to get some perks and help keep the videos free for everyone as long as you can download the project family natales from unicode marcom subscribe to the channel for more unity tutorials post any questions in heaven comments and I'll see you next time [Music]
Info
Channel: Code Monkey
Views: 68,055
Rating: undefined out of 5
Keywords: unity crafting system, unity crafting tutorial, unity crafting game, unity craft minecraft, unity inventory and crafting system, unity minecaft, unity items, unity recipe, unity recipe system, code monkey, brackeys, unity tutorial, unity game tutorial, unity tutorial for beginners, unity 2d tutorial, unity 3d, unity, game development, game development unity, unity 2d, unity 3d tutorial, programming, coding, c#, code, software development, learn programming, minecraft crafting
Id: LmQ6U3YkHHk
Channel Id: undefined
Length: 22min 46sec (1366 seconds)
Published: Fri Jan 10 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.