Unity 2021 Use Scriptable Object instead of Enum

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in unity enums are usually used to represent some sort of data now in our project we have two types of weapons a sword and a bow so i have decided that to let the character know what type of weapon is he holding i will define a an enum weapon type and i have two types sword and bow and my weapon script that defines each pickable weapon has this weapon type that it is passing to a weapon manager placed on the character while in a simple project this would work great what happens if i have another weapon for example this axe how can i add it well to add it i would have to go to my script and add another enum and possibly modify the weapon manager as well because it is also dealing with this enum and if we add another weapon i would have to repeat the whole process and i believe that instead of using enums what we can do is use scriptable objects and i want to show you a solution using those hi i'm peter and welcome to sunny valley studio tutorials before we start the assets that i'm using are from quaternious the ultimate modular ruins pack so the link will be in the description make sure that you check it out this is a cc0 licensed asset okay so just to prove my point let me try adding this axe to my inner so first thing that i need to do is duplicate my pickable weapon and i will drag it here somewhere in my scene i will open this prefab and delete the bow object and let's add the ax object okay here is our ax great but now i can select my applicable weapon but i cannot select the axe because the enum doesn't contain it okay no trouble i can simply edit the weapon script right i can add the axe here to our weapon type and all should be well well let's go back to unity and now i can select my pickable weapon a weapon script and i will select the axe now what happens in our weapon script well our weapon script uses on trigger enter to detect when our player has collided with it and it informs weapon manager that is on our player that if the weapon manager exists on the colliding object we are going to want to equip this weapon type so the weapon manager equip weapon weapon type if i go to this definition the weapon manager contains the prefabs of our weapons because since we are passing the enum we need to from somewhere get the prefab of the weapon and it contains a reference to the weapon slot so in the equip weapon we are getting this weapon type weapon type we are playing some sound we are saving the equipped weapon as the weapon type we are creating this game object weapon by selecting weapon type the sword or weapon type bow in a switch statement and we are returning this game object so prefab of our weapon and we are destroying the previously equipped weapon and instantiating the new weapon and assigning it to our player avatar now great but our switch statement now doesn't cover the weapon type dot axe so what can we return here i would like to written an axe a prefab now i do not have it in my game objects references so i will add the axe here and now it should all work let me save those scripts let me go back to unity as you can see it was already a lot of work now i need to also check if my character animated object has the weapon manager script on it and if this script contains the axe reference it doesn't so i'm going to drag it here so now i can press play okay we are in the game i can pick up the bow but i can also pick up now the axe and now i can check my character animated that it equipped weapon is indeed the axe but if i stop the game i haven't defined none or nothing in my equipped weapon so a weapon type so i have always this sword equipped although my player doesn't have anything so those little things about enums make working with them difficult for things like those weapons because every new weapon that we want to add needs to go through the same process of modifying all the scripts in our game so the problem that we have is adding new weapon is problematic and what is going on here well the problem overall is that the enum is a wrong tool for the job now don't get me wrong enums are very useful especially when defining things that does not change for example traffic lights only have red yellow green and cardinal directions usually are defined by north east south west and if we are using encode for each direction in a direction enum so that we select enum direction north direction east direction south direction west it is much more readable compared if we were using for example vector 2 1 0 0 1 to represent those same directions the main difficulty that we have is that every change in our enum requires us to recompile our scripts and modify them so that's why let's consider scriptable objects if you haven't worked with those scriptable objects are data container and they can be created inside unt inspector because we can add to them create asset menu so we can right click and create those scriptable objects they can store ref fields so we can define name for example or weapon type or a prefab for our weapon as well as the methods if we need to have some methods now why should we use those cryptable objects instead of our enum okay this is the same scene that we have seen previously but now instead of enum i have created in the data folder a weapon data so scriptable object by extending descriptable object class and i have added create asset menu to be able to create it through the create a new asset inside the unity inspector now what we can store here is for example public in the damage string weapon name as well as the game object weapon prefab now if i go to unity we can select the data folder and i have defined bow weapon data and as you can see i have assigned this a bow weapon prefab i can also right click create a data and weapon data and i can create for example x weapon data and i can give it some damage value weapon name axe and i can select my prefab axe weapon prefab now their weapons so depictable objects in our game contains the same weapon script but now instead of having a enum it has a private field of type weapon data so so we can assign this weapon data so object to our weapon and ultra grantor now sends to our equip weapon method the weapon data so object and if i go to our equip weapon definition now it gets this weapon data so and from this it gets its prefab for our weapon as well as in our weapon manager now we can save the reference to our weapon data so so we have access to all the data about the weapon because as i have shown you we can add additional fields here like public into damage or anything else anyways the main problem in our project was that we couldn't easily add a new weapon to our game without having to go into the code and modify the existing logic right now we can duplicate the pickable weapon object i can delete the bow and i can now select my weapon axe just like we did before but now if i select the weapon script on it all i need to do is select the weapon data and swap it for my axe weapon data that i have created just a while ago and now if i press play i will be able to pick all of those weapon up without any trouble right now our character animated object our weapon manager script doesn't contain any weapon data so so we have no problem with this where we had this enough set to be stored by default now if i go to my bow i will have this bow weapon data in my weapon manager as well as my character now carries a bow and if i go to my axe now as you can see we have the axe as well as in the weapon manager we have equipped weapon axe weapon data so again we have access to all its data that is defined in the scriptable object so this way we have used scriptable object and a bit of object oriented design so instead of using this enum we can save the weapon data so reference which is serializable to my save file if i want to save or reload the state of my game and to add more weapons i can simply create a new weapon data object and define its properties and it will all work with my current system because every weapon data so has the same parameters so i will need to define the same parameters that are currently used by my logic and of course i can go to weapon data and add public int health or whatever we want to have here to define our weapon data and we can simply access this through our weapon manager okay so to summarize in case you are starting to see that your enum is growing and you need to add more to it this might be an indication that the best possible solution for it is to refactor your code to use scriptable objects instead and this way you can add more to your game without sacrificing the time to modify any of your scripts so if you have an encoder in your team too that is working on those weapons they can easily add those into your game without worrying about the code side of your game great if you want to learn more about c-sharp language and how to use it in unity and about object-oriented programming paradigm check out my video courses the link will be in the description thanks a lot for watching take care
Info
Channel: Sunny Valley Studio
Views: 34,877
Rating: undefined out of 5
Keywords: scriptable objects enum, using scriptable objects in unity, unity, unity3d, enums, c# enum tutorial, unity enum, enums in unity, how to use enums with unity, working with enums in unity, what are enums?, enums in c#, unity enum tutorial, enumeration, c#, custom editors, enum selection unity, how to use enums in unity?, data management in unity, data management tutorial, unity scriptable objects, how to scriptable objects, unity game development
Id: ALSTea0HXZI
Channel Id: undefined
Length: 10min 56sec (656 seconds)
Published: Tue Nov 16 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.