How to Craft Items with Scriptable Objects! (Unity Tutorial)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we're going to apply scriptable objects to a bunch of systems that we made previously scriptable objects are great and they help us make our games more designer friendly so we're going to use them to define items and recipes and apply them to our inventory and crafting systems let'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 in-depth tutorials made by a professional indie game developer so if you find the video helpful consider subscribing okay so in the last video we covered the basics of scriptable objects what they are how they are defined how they are created and how they can be used if you haven't seen it yet go check the link in the description to go watch it after watching that video you should be able to easily follow this one so now that we know the basics of scriptable objects we want to apply to a concrete example so over here i have my crafting system this was fully made from scratch in another video so check it out to learn more so i have the inventory system with a bunch of items i can split them and then drag them onto the crafting system window and if the recipe is correct it crafts a more complex item so there you go with two wood i make a stick and then if i take one stick and two diamonds there you go i can make a diamond sword now i can take that diamond sword and apply it to the player and there you go now the player has a diamond sword so really nice system now the way we made this was by creating all the items and recipes through code now that approach works just fine but if you want your game to be more designer friendly then we can use some really excellent scriptable objects by using them we can keep all of our data outside of our code and completely separate it from the logic this video is made possible thanks to these awesome supporters go to patreon.com unitycodemonkey to get some perks and help keep the videos free for everyone so over here is the item class and as you can see we have an enum for the item type then based on the item type we have some functions down here to get sprite so it receives a item type then it does a switch on it and returns a sprite and another one to return a caller and we also return the name now we can group all this data in a simple scriptable object so let's do just that so in here let's create a new c sharp script we're going to call this our item scriptable object and here we are all right now again first things first replace monobehavior instead extend scriptable object and now let's see all the data that we need to represent a single item now first of all we actually have two options regarding the type so we can continue using an inum just like we used over here for the basic item so we can use this to define the type or we can use the scriptable object itself as a type so those are two interesting approaches now if we were building this system from scratch the better approach would possibly be to use the scriptable object itself as the type and not have to use the enum at all however in this case since we're upgrading an existing system it's probably going to be somewhere to keep the enum type for now so later when everything is working with scriptable objects if we want we can then work on removing the type in them so for now let's store a public item dot item type for the item type inum then let's also store a string for the item name and let's also store a sprite for the item sprite okay so we have a type a name and a sprite pretty basic now in order to create it let's add the attribute create asset menu and let's set a mini name let's put it inside a folder called scriptable objects and let's call this our item scriptable object all right so that's it we have our scriptable object definition with all the fields that we're going to need to store the data for any of our items now back in the editor let's create it so for that to keep things nice and organized let's create a new folder for our scriptable objects and inside let's go to create scriptable objects and yep let's create an item scriptable object and for this one let's start off simple with just a piece of wood so now here let's set all the data for descriptive object so first of all for the type let's select wood then for the item name let's call it wood and for the atom sprite let's also select the wood sprite alright that's it so here we have a very simple scriptable object defining our single wood item it has a type name and a simple sprite now let's go to our item class and in here let's modify this to no longer work with the inum but instead of the enum it's going to receive a scriptable object so let's replace this with a item scriptable object for the item scriptable object all right that's it and naturally we see a bunch of errors since the rest of our code is actually expecting the item to have a new field so we need to go through all the errors and replace all the instances where we were using the inum and instead now we're going to use the atom scriptable object so for example on here on the git sprite instead of having to ask for this place and get a switch we simply go into the item scriptable object and we return the actual item sprite so the sprite is no longer stored in the code but rather in the scriptable object now here for the two string instead of returning the item type we go into the script long object to return the item name all right so i fixed all the errors in here now let's go and see the inventory and yep over here we have a bunch of errors because again we're using the item type now again all this code was made fully from scratch in the previous videos where we made the inventory and crafting system so if you feel lost go re-watch those and you'll be able to easily understand the changes that we're making okay so here in the inventory we have our starting items which again are using the item type so let's comment this out for now then down here let's see the other errors okay so here we're comparing the item type so you can do pretty much exactly the same thing except we compare the atoms scriptable object all right there it is done here we're doing another comparison and right now we can just remove this function all right so all the errors have been fixed right now and over here in our base testing script we are creating the player's inventory so we can also add a field so we have a reference to our item scriptable object and just for testing we can get the player inventory in order to add an item and we create an item pressing the item scriptable object and a certain amount all right so just like this the basics for our inventory should be working so here we are and on the testing script we can see we have film for a scriptable object and just select how you would scriptable object okay let's test and see if the inventory is now working with scriptable objects and yep there it is over here we have our starting inventory and yep it does indeed have 10 pieces of wood alright so it's all working great we changed our code to be based on scriptable objects instead of enums and everything works perfect awesome now let's continue instead of making our starting inventory over here through our testing script let's once again separate the logic from the data and actually expose it in the editor so let's create a simple stroke to hone our starting items all right so here it is so we define this struct which contains a item scriptable object and then an amount it's also a system not serializable so it actually shows up in the editor then up here we've got a field for an array of starting items and then on start we simply get the player's inventory we cycle through the array and we add the items to the player inventory so there it is very simple and once again the whole purpose is to be fully databased and fully exposed in the editor so back in the editor over here we can see our starting item array and now let's say once again just start with one very simple then we take the scriptable object let's select the wood and now it's like six pieces of wood okay let's see if our inventory does start with six pieces of wood and if there you go over there we have indeed six pieces of wood all right so we have the inventory fully working on with its starting state but now it's own working with scriptable objects so again we completely took all of the data from inside our code and separated it from the logic and just to verify this is all working let's try making another scriptable object so once again right click create a new one new item let's call this a diamond and again for now we select the type in um but again in the future we're going to use the scripting logic itself as a type so we've got a basic diamond select the sprite for the diamond and now in order to add it to our starting inventory just go in here and let's add another element to our starting items and this one will be a diamond and let's say add two diamonds now let's see if it does indeed update without ever touching the code and yep there you go we have wood and diamonds in our starting inventory so once again we didn't touch our code everything is completely databased right awesome so far so good so now that the inventory is working let's make the crafting system based on scriptable objects now the one thing we need to do for that is to define a scriptable object for the recipe so let's do just that so create a new c-sharp script call this the recipe scriptable object and once again instead of modern behavior it's a scriptable object and now again let's think of what data do we need to store a actual recipe so we can inspect the crafting system that again we've only made in the previous video and over here we see our recipe dictionary and we can see a whole bunch of different recipes so we can see that we're creating a dictionary where we're going to hold the output type as the key so for this one is the recipe for a stick it's going to have this recipe and the recipe itself is essentially a 3x3 crafting matrix with all the items required so this one you can see it requires none on all these except two wood on these positions okay so we need to store the output and then our item matrix so over here in our scriptable object definition let's first define a item scriptable object for the output and then we just need to store our crafting matrix so again that's going to be of type scriptable object and then let's call it item00 and so on all right so here we have our 3x3 crafting matrix defined of type item script object so we got from zero zero all the way up to two two all right so just like this we have our recipe definition now again just add the create asset menu all right there it is now back in here on our scriptable objects let's create a new scriptable object a recipe scriptable object let's make this our stick recipe so we have our two items and then we have our recipe now this is the recipe for the stick so we also need to create the stick scriptable object so again very simple just create new scriptable object and set up on the data and now we just need to fill in our data so first of all for the output the output of this will be a stick and then for the recipe we're going to have two pieces of wood right down the middle so that's going to be on one zero a piece of wood and on one one also a piece of wood all right so there's our recipe definition and if you wanted you could combine this with a custom inspector to really make the recipes look visually great now i covered custom editors in a previous video and you can easily apply that knowledge here so in fact let me do that really quick all right so here it is the recipe with a really nice custom editor we have the output and we can easily visualize the matrix for our crafting so here is the whole editor script it's long but it's pretty simple it's essentially just copy pasted for all nine positions this is included in the project file so if you want to browse around this to see how it works you can go and download that so we have our recipe scriptable object and it looks really awesome thanks to our custom inspector okay now let's actually use it in our crafting system so here we're no longer going to have a recipe dictionary instead we're going to have is going to be just a list of recipes scriptable object and where we set it up we're creating the crafting system so let's pass it in there okay now let's remove the dictionary and now we're going to see a whole ton of errors so let's fix all of them okay the main one is just down here where we try to get the recipe output and now again we are not using the item time for rather the item scriptable object okay so here we cycling through the recipe scriptable object list and now here it's also a great way of seeing how you can store data with scriptable objects but you can also run some logic so for example we need to ask the position on x y so we can add that as a symbol function so on our recipe definition let's do that all right there you go just a pretty simple function to return the various fields depending on the x and y and now here we go into the scriptable object and we use that nice function alright so there it is pretty much the exact same logic and if the recipe is indeed complete then we're going to return the output and if not then we're going to return none which in this case is not then down here on the create output and yep there we go all right so all the errors are fixed now our recipe data is no longer written directly in the code but rather it is stored as a scriptable object now the last thing we need is just to pass in all the recipes to our crafting system so here in our testing script we have the starting items and let's also add all the recipes and we pass that into our crafting system okay now back in the editor we have our testing script with the recipe script long object list and just drag the stick recipe alright everything should be working let's try okay so here we are let's try making a stick now for the stick we need two pieces of wood so let's split that put one there then one down there and if there we go we have our stick recipe and now we can take this and drag and yep there you go we have a stick all right awesome so our recipe is working perfectly and now for example if we wanted to change the recipe using the script mall object system it's really simple so in here let's say the wood is not down the middle let's say in order to make a stick we put none there and we put a diamond over there on the corner so that's all we have to change we don't touch the code at all and let's run and if we use our previous recipe nope doesn't work but now we use the new recipe and yep there you go we have our very nice stick okay so here you can see how we have indeed converted our inventory and our crafting system to work entirely with scriptable objects there's no data stored in the code itself the code only handles logic now one big benefit of using scriptable objects as opposed to making it all through code is that adding new items and recipes is very simple so we don't need to change absolutely anything in the code all we need to do is create some new objects so let's create a new item scriptable object let's do it for the diamond sword so we have our data for the atom now let's make the recipe and again using our really nice custom editor we can make this really easily so the output won't be a diamond sword and let's say we need one diamond in here one diamond in here and a stick down here all right that's a recipe we just added to our testing so that the recipe is seen by the crafting system and now if we run okay here we are now first we need the stick so let's make that yep there you go that's a recipe so we got a stick now we put the stick we split the diamonds put one there one there and there you go we have our very nice item so you can see how literally it was super easy just to add a completely new different type of item as well as the recipe to actually make that item so let's try a different one let's say create new item for a chicken breast and then another item for some chicken nuggets all right we have our items now let's make the recipe all right here's the recipe once again we add the recipe to our list you could also dynamically populate this list and set it manually and then let's also add some chicken breast to our starting adam array okay should be working let's test and even right away we start off with some chicken in our inventory so now let's craft some nuggets so we need three stacks so put one there one there one there there you go we got some really nice chicken nuggets so there you go yup just like that so you can see how without touching the coat at all we managed to make a new item and a new recipe right awesome all right so i've created two new items for the hamilton armor and there you go just equipped a nice helmet equipped nice armor yep now let's make a diamond sword there it is the stick two diamonds there you got the item sword and equip it and there you go everything looks really awesome so again all this is completely 100 data driven now again this is just one example use case scriptable objects can be used for just about anything you can use it to store game data just like we did here or you can also use them for making some really useful custom editor tools most unity tools like shadowgraph and the scriptable render pipeline actually use crypto objects extensively to manage their data so take that as inspiration and make your games more designer friendly with better tools by using scriptable objects if you found this video helpful consider liking and subscribing this video is made possible thanks to these awesome supporters go to patreon.com unitycodemonkey to get some perks and help keep the videos free for everyone as always you can download the project files and utilities from unitycodemonkey.com subscribe to the channel for more unity tutorials post any questions have incomes and i'll see you next time you
Info
Channel: Code Monkey
Views: 71,209
Rating: undefined out of 5
Keywords: unity crafting system, unity scriptable objects, crafting system, unity scriptable objects inventory, unity create asset menu, code monkey, inventory, unity create assets, unity tutorial, unity game tutorial, unity tutorial for beginners, unity 3d, unity, game design, game development, game dev, game development unity, unity 2d, unity 3d tutorial, programming, coding, c#, code, software development, learn to code, learn programming, unity tutorials, unity game designer
Id: E91NYvDqsy8
Channel Id: undefined
Length: 19min 45sec (1185 seconds)
Published: Sun Aug 02 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.