Be CAREFUL with Scriptable Objects!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome I'm your code monkey now scriptable objects are an excellent Unity feature you can create discrete objects that exist as an asset and you can use them to store whatever that you want I cover them in detail in another video if you want to learn more so they are great they are super useful for things like creating items maybe crafting recipes maybe store some skill stats and so on however one thing they are not good for or whether they're not supposed to be used is for changing data you should only be using scriptable objects for static data so unlike you define an item with a name an icon perhaps a gold cost and then you don't touch that data after running the game the reason why I'm making this video is because I've seen some people try to make a safe system with script among objects and everything won't work well if you're in the editor but it won't completely break once you finally make the final build so let's see the issue that I'm talking about over here I've got my simple testing demo now for the visuals over here I'm using the ancient empire cinti pack it's a really gorgeous pack with a really nice Roman Greek theme so if you're interested check out the link in the description to get it for yourself and actually right now it's discounted as part of the Cynthia Black Friday sale all of their packs that are at half price so if you're a fan of their Style just like me then definitely check out their store another great Black Friday sale running right now is on the NT accessor over 500 excellent assets are discounted everything from visuals particles animations and tools there's even Flash Deals at 70 off changing every single day and finally there's a Humble Bundle running right now with some low poly assets they are low poly but different from the Cindy style they look quite a bit more cute so if you need any assets then check out all those sales Link in the description those are film links we've used them it's the same price to you and I get a nice commission and here I have this nice character so I can walk around the keys I can go anywhere I want all right then in the project files I've got this script so a simple save file ISO which is a typescript normal object and over here I'm just holding a public Vector 3 for the player position and here in the editor I've got a scriptable object of that type so it's got some player position being stored and finally also to have a simple script over here has a reference to the save file script along object to deployer transform then with some testing inputs just running these two functions so on the save function I simply grab the player transform I grab the position and I save it on the save files for the mobile object so I did that that saves the position on the scriptable object and then for the load simply does the opposite just goes into the save files for the long object and loads the position onto the player transform so very simple just a super basic save system and over here and then what does work so I can move the character away I can press unload and there go it loads into the default now I can move around let's say I want to go all the way up here and I press on Save and there you go I've saved a new position so now I go into a completely different position and I hit on load and there you go I unload it back into that position so it looks like this works perfectly let's also make sure it works between runs so let's stop playing and over here if I look into the save file scriptable object yep it does have the new position that I saved while playing and now if I hit play again so here I am again again with my character and if I hit on load yep there you go it does load into the new position that I saved so the default was in here then I saved in here and right now it loaded into that one alright great so everything seems to be working perfectly correct so I can save some data on the scriptable object and then easily unload it and that data is indeed persisting between different runs so right now you can see that it seemingly does work however it doesn't actually work now this is the really sneaky thing with how scriptable objects operate so for example let's say you're happy with the system the game is complete and you ship it to your players in order to do that as usual you just make a build so you just go into file build settings let's say windows build and just go ahead and head on build okay so here's my build executable let's run all right here's my game once again and now over here if I just hit on load and there you go it doesn't load that position okay so it seems like it works now let's try saving another position so let's move the object somewhere let's say the player is over there let's hit on Save and down there on the log we can see that save so now let's move somewhere away let's load and there you go it did work so it did save that one okay so again it seems like it's working however again the home point of a save system is to keep the saves between various runs so let's press alt f4 to quit the game now let's go ahead and play the game again so here I am back in the game and if I hit unload let's see if it shows up on top of the stairs or over on the right side so I press unload and there you go that's the issue so in the last time that we saved the game I over here and I save this position but then upon opening the game again it resets into the default position this is a really sneaky issue so basically what we have here is different behaviors in the editor and in the final build so for example if you were building your game for months and you just expect the final build to behave just like the editor then this would be a very tough thing to find out just at the end of that project for example if you publish this bill to your players you would get complaints that the save system isn't working and then perhaps you would go insane by testing it in the editor and in there it would work normally now I should point out that this quote unquote issue this one isn't really an issue it's not a bug this is the exact intended behavior of these scriptable objects they are meant to be used as read-only data containers so you should use them for defining your items on their Sprite their gold costs and so on but you should not try to save that onto them during runtime so don't ever build a safe system or try to save the player inventory or anything like that don't ever try to do that with a script long object the issue is that you can see the save does not persist so if I'm playing the game and I save and I save this position here I go away and unload now the scriptable object which is currently stored in memory that one does have this new safe position but if I exit the game and I play it again then it's going to start with the value that was assigned right there at build time so whatever changes you make while the game is running This only affects the memory and doesn't actually affect the actual data stored on the file so when you run the game again it won't go back into the defaults that were saved now at this point I should point out that what I'm talking about here this quote unquote issue is specifically for the difference between the editor and the build the issue is that you cannot save data once in a build but you absolutely can save data while working the editor so that means you can definitely use scriptdoll objects and even write to them whilst working on your editor tools so over here if this instead of being a save system for play mode if this was some kind of level editing system that you only wanted to use once in the editor if so then you could indeed do this so you could make a tool that is based on script along objects for storing all of your own level data and when you do that you just also need to make sure to use the editor utility which is inside up here using Unity editor so in this one you have the editor utility and in there you can call set dirty then you pass in the Target so for example this save file Square Mall object this basically serves to tell the editor that this file has been modified so it should be saved now if you absolutely need or you want to use scriptable object to save data if you do then one thing you can do is just serialize them so you can use the built-in Json utility tool and you can convert to Json and let's say this save files original object with this you can convert the script on object into a string Json then with that you can save that as text in a regular file then read that text and load it back into the description object so that is one approach you can take although at that point for that approach you really don't need the description object at all I actually cover the save system just like that in another video it is a simple c-sharp class for the save object to hold all of the save data and then uses the Json utility to convert the save object into Json and save it onto a file now this video isn't meant to scare you away from using stripped on objects at all scriptable objects are an excellent Unity feature and you should absolutely be using them however use them the way they are meant to be used meaning mainly use them only as a read-only data container so do you use them to store and level data store item data enemy data and so on just don't use them for saving data since that won't work when you make the final build one excellent example let's hear the more objects is how I use them in my crafting system I use them to define the recipes and even made a custom Editor to make them super easy to use so check out that video if you want to see a really good nice use case for script and all objects alright hope that's useful check out these videos to learn some more thanks to these awesome patreon supporters for making these videos possible thank you for watching and I'll see you next time [Music]
Info
Channel: Code Monkey
Views: 71,201
Rating: undefined out of 5
Keywords: code monkey, unity tutorial, unity game tutorial, unity tutorial for beginners, unity 2d tutorial, 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, how to make a game, scriptable object, unity scriptable object, unity scriptable objects architecture, unity create assets, unity game designer
Id: 5a-ztc5gcFw
Channel Id: undefined
Length: 8min 26sec (506 seconds)
Published: Thu Nov 17 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.