3 Interesting Ways Scriptable Objects can fix YOUR Unity3d project now!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's up jason here today i'm going to show you three ways that you can use scriptable objects to save yourself time and avoid frustration when building your unity projects if you're not using scriptable objects already definitely hang around you should be using them when i pull my audience i see that about 40 percent of people are using scriptable objects all of the time so if you are one of those people by the way if you use scriptable objects all the time before i tell you the three ways that i'm going to share please drop a comment down below and let me know what you use scriptable objects for maybe there's something that i missed or something that we could have a discussion about in the comments below let's start real quick with the list of three different uses for scriptable objects that i think are the most common these are the ones that we're going to cover today and the ones that i found myself using most often and other developers using in just about every type of project the first one is storing data or storing state something that you'll need to do if you have a multi-level game where you're not staying in a single scene this is one way to do it the second item that we'll cover is actually creating things like items or using scriptable objects to create content data this could be anything from quests to items or even maybe crafting recipes like i teach in my game architecture course and the third item we'll talk about something i also cover in that course is event messaging or using scriptable objects to send messages around and clean up your code a bit this also comes in really handy if you're working with designers so if you're on a small team make sure that you hang around for that part before we get started though i did want to mention that i've just opened up my new black friday bundle for my game architecture course and all of my other courses so if you're interested in learning game development or just want to get a deeper understanding of game architecture make sure that you check it out at the link down below i've got a bunch of really cool items that are bundled in free and some things that i never thought would have been added in there as well as a huge discount the biggest one that i've ever done last year i completely forgot about black friday so this year i wanted to go big let's start for the first use for scriptable objects and this is the easiest one to implement and the one that's probably the easiest to see a spot where it just fits right into your project that's using it to store data or persist data across multiple levels here i'm going to go through a simple example where we've got a player that plays through multiple levels and then picks up some coins the goal is to keep track of the number of coins that they've got across level loads i'm going to reuse the same level so that we can run into some of the weird issues that you might run into if you use the same level which i know i ran into and then we'll go through the solution for my demonstration i decided to go with the third person character controller starter assets from the asset store it's one of the unity packages that allows you to get a character that moves around really easily and it seemed like a great way to just kind of kick the thing off then i added in some coins and set up a system for my character to go run around and pick up the coins let's take a real quick look at how that works and then we'll dive into the scriptable object part my coins are all in a folder which is really just an empty game object and each coin has an item script on it the item script only has a single field for a rotation that allows the coin to just spin around and make it a little bit more active or interactive i can run around and pick up the coins or walk and hold shift to run part of that default starter kit and when i get up to five coins the game reloads or the level reloads and my data for how many coins i've collected goes away let's take a look at how we're saving off that data for the number of coins collected and see how we can fix that issue first let's take a look at one of these coins it has a capsule collider on it with is trigger checked and an item script now if you already know what method we're going to look for on the item drop a comment down below i'm curious to see how many people are just going to guess and already know exactly what's going to be there let's open it up so if we open up the item yep it's the on trigger enter method we're going to wait for a player to walk into this trigger and if we're able to get the player component using the other dot try get component which will return true if it gets that component it'll give us back the player right here in this player variable and then we'll use its coin collection and add our item to that coin collection then finally on line 14 we just turned this coin off let's take a look at that coin collection see how that's all working and what the code's doing we'll hit f12 f12 is just going to take us to this item collection v1 instance on our player so here we just have a reference to an item collection we're going to hit f12 again and take a look at that so our item collection is a script on the player this script is allowing us to add items to the collected item list we have a collected item list here on line 12 and then notify the ui whenever the number of items has changed so we call this changed event right here on line 10 that we're registering for in the ui then on line 19 we're just reloading the level if the count of our items collected or the number of items that we've collected count is right here you can see it's just the size or the count of this collection if that divided by five is equal to zero or has a remainder of zero so we're essentially checking to see if it's divisible by 5 or evenly divisible by 5. so if we got to 5 or 10 or 15 then we would always get we would get a reload on those cases so we could change that by adjusting this value here but let's say that we want this to persist we want to be able to keep the score across the level let's jump back into unity and look at the first option one thing that we could do is go to our player which has our player script and that item collection on it and we could maybe make our player don't destroy on load we could add in the don't destroy unload option to the player script and then when we reload the player this player would still be around but we'd run into the issue of oh there's a second player there because we reloaded it in the player's place and then we'd have to decide well do we place the player somewhere else like in another level and then load into this level or do we maybe um you know place the player every time and then copy over the data or maybe when we load in the level if a new player spawned we just instantly destroy it we have to find a way to solve that problem and the solution that i'm going to present to you really avoids having to solve that problem and makes it i think a little bit simpler and that's to change this item collection instead of it being a mono behavior that's attached to our player into a scriptable object that we can just track in our project view so let's see what that looks like and here it is you can see it looks about the same we've just got one less script on our player and we now have a coin collection object in our project let's take a look at that and see how it works first let's find the reference click on it just make sure that it's highlighting the correct one and then go click on our scriptable object you'll see here that it's got an option to reload every whatever amount and it's grayed out only for me because i'm using plastic scm so i have to check it out and then go change that value if you're not using that you won't see that change there but i'm going to go check in my change say that i changed collection loading to every 10 coins so to reload the level now on every 10 coins let's see how we've created the scriptable object though what's the difference in the code let's open up the item collection and compare it to the other item collection to the item collection v1 the most important change is the base class instead of it being a mono behavior we now use scriptable object and that's how we create a scriptable object versus a component that would go on to a game object the other thing to note is the create asset menu attribute up above we need that to be able to actually create an instance of our item collection let's take a quick look at how that's done for anybody who hasn't created a scriptable object before in your project view once you've created a scriptable object with the created asset menu and you go to right click and hit create you should see your new items or your new scriptable objects appear up there i can choose item collection and get a new item collection i'll call this gold coin collection and i'm gonna adjust the reload counter to be instead of five let's make it seven so i reload every seven gold coins that i pick up i will need to assign this to my player though so let's go select my player armature where that has my player script on it and then drag in the gold coin collection now if i just hit play after this i should expect to see an error because my new gold coin collection isn't hooked up to the ui let's take a look at that yep i've got an error trying to get the count from my coins collected account and you'll see that this is actually also referencing an item collection so i'll stop playing we'll go back to our project view and see our item collection assigned there i'll take my gold coin collection and drag it in let's take a look at how that coin counter text works by the way if i zoom in here you'll see that we've got a little bit of life cycle management stuff going on here or event registration we've got a field for our text and a field for our item collection and then in awake we register for that changed event on the item collection and update our text if that happens so if change gets called we update text on destroy we unregister so we don't get a memory leak and then on enable we just update the text again to make sure that we've got the right default value in on validate we set our text to the component that's on us and then in update text we just set the text to the count of things in that collection and remember this count is something that we were implementing ourselves and when it's not assigned or collected items is not assigned we're going to get a no reference exception so now that the error is gone our text is hooked up let's hit play and see if we now have the magic of multi-level scores so i've got a three there a four there a five there a six oh seven there we go and i reloaded into the level again or the next level it could always be a new level doesn't have to be the same level and you see that once i get up to 14 coins i should expect to see it load again let's stop playing though and play again and look at what that value is so we stop and we reload and it's at 14 and this might make you think okay well this could just be like my permanent saved data right this is where i'll just save the score and it'll just constantly persist that's not the case if we save our scene and then load the sample scene and load back into our playground scene we should see that that value is now being cleared out our data is all refreshed and re-cleared out and we no longer have that there so we want to make sure that that value gets cleared every time not just the one time when we actually reload the scene by totally loading out at different levels now one option would be to just reset that value when the game starts but how could we do that if we don't have an awake or something that we could just call right into oh we actually do let's take a quick look at our item collection script i've made a small modification to it and i've added in an awake a start and on enable and an on disabled notice that the start is grayed out because writer's so smart that it knows that start doesn't actually exist for scriptable objects so we could probably just delete it and i wanted to show that let's save now and then go hit play and see when those are called and if they're called across scene loads maybe we can make some use of this first though let's go turn down the number of coins that we need to be maybe three instead of every seven so i don't have to run around so long and then we'll hit play run in and take a look we'll open up the console window i've got on disable and on enable we're already called one so let's go grab some and let's see did anything else get called here nope go grab some more nothing was called go grab a couple more nothing was called let's stop playing and then we're going to make sure that collapse is off and play again and look we got on enable and on disable again so if we just go into here and maybe in our on enable clear or reset this collection we'll say collection is equal to new list of items or we could just do collecteditems.clear but i think just reinitializing it to a new list is good that should fix the issue and now we should be able to collect items across levels keep track of those items and um well keep our score there for our player so let's hit play running around grabbing a couple more coins and you see that the score just keeps going up up up and up and as i restart the game restarts i want to real quickly though before we dive into the next section go into debug mode and show what this data looks like because in our next section we're going to dive into some improvements to this data we'll go into debug mode here in unity and notice that all of these elements show as missing the reason for that except for these new ones that we're adding is that we're adding in items and then destroying those items as we reload the level so we have a reference to them in here but if i were just doing this i would probably switch to keeping track of an integer instead of a list instead though let's make this more complicated and let's start diving into how we can use scriptable objects to store content data we'll make different types of coins and then use those coins in this system to show them in the in our game and make it really customizable for our designers so they could create any infinite number of coins in a matter of minutes and place them around the world or whatever other type of collectible that they want here we've got a scene set up with multiple different coins that i can collect all in different colors i've got red ones blue ones green ones and little ui elements that tell me how many of each color we've collected let's take a look at how this can be accomplished with scriptable objects how it can all make things easier and how we can allow our designers to just make a new coin type and drop in a new ui element without writing any new code let's stop playing and take a quick peek at what we have here inside my scriptable objects i've created some coin types i've got a blue a green an orange and a red coin type now this is a new scriptable object let's take a look at it and see how it's set up i'll find my red coin and see that it has an item type and it only has one property or field on it an item color that's specifying the color that matches with the name the red one's red the blue one's blue and if i open up my item type script you'll see that the script is extremely simple we've got the same scriptable object base class and the create asset menu and then a public field named item color that we can read so how are we using this and how is it making things simple where's all of the magic happening let's go take a look at our item again so inside of our item we can now specify an item type on line 8 i've added a serialized field for an item type that i can now drag onto an item and then we've exposed it on line nine with a public get only property that allows us to read this but not change it let's go see what that looks like on our item so go find one of the coins those are our items go collapse the mesh renderer and the capsule collider here don't need those expanded out and then we'll take a look at the item script itself so the item itself has an item type on it this one's set to orange you can see the name matches orange this one's set to red and the name matches red if i change this one over to be a blue coin i expect the name is going to change as well that's done with a little bit of magic in on validate let's go take one quick peek at that if we scroll down here and on validate we set the game object's name to the item type name pretty simple easy way to keep names cleared up we could also make this like a placed version of the item or add in whatever string concatenation that we wanted but i like just having it match exactly let's see what else we're doing in the item in our on enable we're actually setting our color of our mesh renderer's material to that item color this is something that you could definitely be doing in your scriptable objects assigning colors or assigning gradients or randomizing colors or even assigning things like scales and adjusting those components in in on enable or at some other time the other thing that we do is we add our item to the coin collection and have the coin collection do a little bit of intelligence around which item type we're using let's go take a look at our coin collection and see how that's changed i'm going to go into the add method by holding ctrl and left clicking nice little shortcut that takes you right into a method and this add method might look very similar to the one that we had before the previous one took our collected items and added the item here though we're actually adding the item type we're no longer adding the game object that's going to be destroyed when we looked at that collection and we saw the items were all empty it was because we were loading into a new scene and we're referencing objects that were destroyed and to be honest we don't care about the specific instance we care about the item type we care about how many items of each type of the collection were picked up not the specific game object instance that was picked up so here we're adding the item type which means that our collected items has changed instead of being a list of items now a list of item type an item type is not something that's going to be destroyed we're going to be referencing it or giving it the reference to the thing that's in our project view that's around the entire time here you'll see i've also got the awake on enable and on disable methods and then in on disable i'm just clearing out the list we could also do that in on enable but here i'm doing it in non-disable we could also just reset or re-initialize the list all of those options work and i just wanted to show that those work but i've also got the on destroy here just so you can see when that's called in case you're curious and then at the bottom on line 46 we have the count of method and this is what's doing all of our magic ui work in the ui we check to see the count of things that match the item type that we've specified for that ui element just by doing a simple link query using the count and matching against the item type so we'll find all of the items that have an item type that matches that or all of the item type instances that match that let's go find where this is referenced and then finish up this code so here we've got our update text and in the update text method of our coin count text this is the thing that's on the text mesh pro label there we're just now setting it to the count of things that match our item type and if we jump back over to unity and go find one of these ui elements like our green coin text here you'll see that we're using the green coin item type so let's say we wanted to make another one for the orange ones we've got to collect these orange coins and we want to make sure that we can keep track of those i could just select my header and my count here duplicate them maybe move them down another 50 on the y-axis rename this over to be orange coins now i'd probably want to have a nice ui designer who's better at this actually create the ui for me but if i want to allow my designers to hook things up and do it really quickly this is pretty quick now i've got my orange coins there i'm going to copy this color onto my clipboard go over to the coin selected counter it will assign the orange coins item type here which i've already created in orange coin one and then i'll go select it and make sure this is actually set to tint to orange let's see i'm gonna go paste in my color oh yeah that looks like it's pretty close we'll save that off we'll hit play and i'm going to run around and now when i collect an orange coin we should see that our orange coins go up as well i guess they're kind of yellowish but you get the idea that's supposed to be my orange i have the i think my base texture there is a little bit yellow already and i can go click collect collect and see that these numbers are just going up up and up and if i wanted to save them off and make this persistent i could use this data and save it when the game ends or something else maybe into player preps or some other place let's stop playing though and take a look at that data outside of full screen mode and just watch the inspector and see what that collection looks like i think that that's an important thing to just do really quickly all right we'll hit play and then i'm going to select this coin collection i've got i had multiple i want to delete the extra ones and make sure that i'm in debug mode here in my inspector and then let's go check out what happens to that collected items i got an orange coin i got a blue coin i got a red coin notice that it's an item type in there not an actual item instance and there i reload and now i've still got all of that data now this example is really showing the first two ways of using scriptable objects one just that storing the state so that we have something that persists across levels and then also being able to create content right now we can create simple coins just by right-clicking in our coin types or anywhere and then choosing new item type and then giving this a name we can make like a purple coin or we could maybe build this into something more extensive where our collectibles now have a value maybe they have a monetary value that we can use to go sell them to a vendor or maybe they have some quest associated or we could start creating other scriptable objects that are quests or maybe recipes that you need 10 blue coins to create some red coin or 10 blue coins to create the new weapon that gets the the new quest unlocked or whatever the thing is we can start creating data relatively arbitrarily without needing to have to write new code for every different thing that we need and that's really i guess the biggest benefit of scriptable objects you're going to need to write a lot less code and it's going to make things a lot faster to iterate on and work on especially if you work with a designer i mean it'll make it faster for you on your own but if you work with a game designer scriptable objects will really save you a lot of time now there is one other method for using scriptable objects that i wanted to talk about and while i'd like to go into depth about this i've already done a really detailed video that kind of shows all of the code and explains it and i think i'm going to refer you over to that but first give you a real quick overview of what that is and that's an event system or a game event system and you can use these for sending messages around your game that might need to vary or change quite a bit and the example that i like to give for this is setting up events or setting up gameplay logic that you really want your game designers to be adjusting it's used for the types of things that you don't really want to be bothered with when they're making changes like you've got a tutorial system and things need to fire off and do some stuff like play an animation when this thing happens or run some code when this thing happens or update some ui element when this thing happens a lot of the time the game event systems or scriptable objects will allow you to simplify that so that the designers can do it without needing to write any code and without making a mess out of stuff you give them a very simple very easy to use system where they can just hook into the things that they care about and then fire off some simple unity code or some unity actions like playing sounds playing animations or playing other things and then you also have a really easy way for yourself to just add in new hooks for them you can add in new hooks without writing any code or with very very minimal code and it makes it a very simple extensible way to build a game that said it's also worth noting that i don't personally prefer to use that as like my core gameplay building system some people do some people like to build their entire game around scriptable objects some people like to build their entire game around uh visual scripting systems too personally i like the the code i like to be able to see the code work in the code and understand exactly what it's doing without having to bounce around and look at how things are hooked up but when it comes to giving power and flexibility to designers scriptable object events are way more than worth it they're worth the little bit of extra confusion that i might run into or obfuscation that i might run into when i'm trying to debug what they're doing but it saves me from having to do all of their work or a lot of the work that they'd like to do and allows them to do really cool things that they probably would just skip because they don't want to go bother programmers so i highly recommend that you watch that video let me know what you think and if you are interested in scriptable objects and you really want to get deeper into this scriptable object architecture again check out my courses down below i've got that black friday bundle going on it's a big sale and i've got all of my courses in there including the newest course the architecture course where we're using scriptable objects to build systems like these and things that are much more advanced full-on inventory equipment crafting and questing systems and a whole lot more alright if you like this video please don't forget to hit the thumbs up button drop a comment and if you have any other uses for scriptable objects i'd love to hear them so please drop them down below and let me and everybody else know what we should be using or what we should be looking at and considering for our projects alright thanks again for watching and i'll see you in the next video
Info
Channel: Jason Weimann
Views: 36,780
Rating: undefined out of 5
Keywords: scriptable object, unity3d, unity, unreal, brackeys, dani, jason weimann, game development, unity scriptableobject, game dev
Id: BFYRUDk6TDs
Channel Id: undefined
Length: 24min 29sec (1469 seconds)
Published: Mon Nov 22 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.