3D Endless Runner in Unity: #15 - Save/Load System for Mobile Tutorial / Guide, Unity Indie Devlog

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello and welcome back fellas to another video where we continue to build our mobile endless runner using unity it's been a while i was actually really busy i did a lot of bigger projects at my job lately as you might or might not know i work as a software developer in e-commerce industry and since corona our requests nearly skyrocketed so i had a lot of stuff to do but of course this project is alive maybe more live than ever because we're moving closer and closer towards release but we have a lot of stuff to take care of first of course you already see some footage of our current state of the game playing in the background i think it's pretty decent already in the last episode we added ourselves some ragdoll physics so that the player has a decent death animation when he gets hit by an enemy and also expanded the functionality so that he gets also knocked out by obstacles if he doesn't watch out in today's video we will continue development of course and we will create ourselves a quick and easy save and load system which was working on mobile in our case that will be relatively simple since we only have a few variables we need to store such as our total coins which we'll need since that's our in-game currency and also we want to save our character level unlocks and also some basic settings in other cases this will be much more complex but the core concepts are basically the same i will guide you through the implementation step by step guys so grab a coffee and follow through if you want to see how it's done if you want to support me and want to stay in touch please like this video and also consider subscribing to my channel this helps me out a lot and enables me to do more videos more frequently and also hit the bell icon to get noticed when the next video is out but enough talking let's jump straight into unity and let's start coding alright so first let's navigate to our scripts folder and let's create ourselves a new script and let's call it game data this will basically just be an object which holds our data we want to save so we don't need to extend money behavior so let's get rid of that we can also delete the start and update methods now we need to make our class able to be serialized we just type in in square bracket system dot serializable right above our class name right here and also guys a quick side note before we start implementing our class data serialization will not work with unity-specific data types such as vector3 and so on we need to stick to the c-sharp specific data types in our case that's totally fine and won't matter at all but i just wanted to point it out anyways alright now let's create ourselves some fields for our data first off we need to save our progress um forever create a new hint for our total coins and also create an end for our total distance we traveled all right now let's move on to our unlocks i basically just want to remember which levels the player already has unlocked i will use a simple bool array for this this will do the job so let's create ourselves a new bool array and let's call it level unlocked and i will also do the same thing for characters unlocked now let's move on to our settings to be honest i haven't finally decided which options i will offer the player but there will be at least some sort of a language switch and also a quality switch and i use in for both values this will do the job just fine okay so now i want to define some default values which gets used when there is no save file for that let's use the constructor of our class alright let's see here total coins will be zero total distance is zero as well and for the levels unlocked i will lock everything except for the first one same goes for characters unlocked i'm not quite sure yet how many levels or playable characters we will have but i will go with 5 for each right now [Music] also set the language to 0 which will be english and the quality to one which will be something like medium tier for the language and quality i will create enums later on to make it more readable in code the numbers just represent predefined options right now but for now it's totally fine i guess since our options screen isn't implemented anyways so let's go with it alright that's about it for our game data class next let's build our actual save system so let's go back to unity and let's create ourselves a new class and let's call it save system first we can also get rid of extending our mono behavior as well as the start and the update method our class will be static since there's no need to instantiate it then we will need to access some additional namespaces so let's add some using directives next first we will need system.io this basically contains stuff to handle files and we'll also need to access system dot run dot serialization dot formatters dot binary the binary formatter will help us to write our serialized object as binary data to our file we could also use something like json or xml to store our data but then it basically would be very easy for the user to just open up the file outside of the game read understand it and just enter the amount of coins he wishes to have so we use a binary format to make cheating a little bit harder alright now we will need two methods for our save system to work save and load let's start by creating our save method i declare the method as static so it can be accessed without having an instance of the object and also need game data which i want to save as a parameter first we need to define where we want to save our file so i'll create a new variable called path and we can use application persistent data path this will give us a relative path to where we can basically save stuff this is working on all devices and operating systems and then i add the file name since we use binary data we can basically choose whatever file extension we want so i just called it gamedata.qnd for now okay next let's create an instance of a binary formatter i just called it formatter this object will help us translating our object into binary data for us and we will also need a file stream to write data as parameters we just hand over our path and we also need to specify a file mode in our case we can use create create will create a new file and if the file already exists it will override it now we can access our formatter and use the serialize method and our hand over the file stream and our data object that's it now let's not forget to close our file stream and we are basically done load method is very similar so i create a new static method and call it load but instead of having a parameter we want our game data as a return value now we also need our path since i don't want any redundant code i will create a new method for this really quick i call it getpath and i just copy over and return our code from the save method now every time we need a path to our save file we can just use the get path method okay now let's go back to our load method um we also need a binary formatter and a file stream as well [Music] this time i choose the file mode open instead of create like above we can now access our formatter but this time we choose d serialize to load our data instead of writing it and we will store our data inside of a variable of the type game data since the d series method returns a generic object type of type object we need to perform a typecast we do this with the as operator and then our class name then we of course need to close our file stream when the operation is finished and let's also return our data all right almost there now what we want to do is to take care of what's gonna happen if there's no such thing as a save file because otherwise it's gonna crash so i want to check if the file exists before we open up our file stream um we can do this with file dot exists and then we hand over our path but what i really want to do is to take action if the file doesn't exist so i negate my statement with an exclamation mark so if my file doesn't exist i create a new game data object this will store our basic default data and i call it empty data and also use the save function to save it now we can return our empty game data object and that's basically it for our simple save and load system guys it's not much more to it all we need to do right now is to integrate our new system into our game so let's switch right back to unity and let's take care of that next alright so first let's take a look at our game manager script and let's open it up in visual studio our game manager is basically responsible for handling very basic stuff and from now on we also want him to take care of loading and saving data so let's create a new field for our game data object instead of start we will use the awake method to start loading the data as soon as the script instance was loaded now we just need to set our game data equals and then to save system load and that's it for the loading part guys now of course we also need to save data over here we have our game over method which gets called every time the player dies or the level is over before showing the game over menu we want to save our progress so let's just write game data total coins and then plus equals our player coins to add the coins from our current run to the total amount and we also do the same thing for the distance so game data total distance plus equals and then i just copy over what i use right here to show the distance in the ui um and this just runs the float value of our player position to a better readable int value okay now we just type save system and we call our save method and we hand over our game data all right done now our progress should get saved at the end of every run now of course we need a place to display our total coins i actually want to do this in the main menu because like mentioned the coins will also be our main in-game currency so let's go back to unity and let's open up our main menu scene [Music] as you see we currently don't have the ui to display our total coins so i will quickly go back to our level 1 scene and i will copy over the ui from our actual levels so now back to our main menu paste them and then just drag them into our ui i'll leave the position and style how it is right now i think it does the job and i can optimize it in the future if i want to next i want to create a main menu manager which will have a similar task like the game manager in our levels but just for the main menu so let's first create an empty game object and let's call it main menu manager and let's also create a new script and i call it main menu manager now i just drag my script onto the empty game object and let's open it up in visual studio first we need a field for our game data then again we use the awake method to load our data now i will also need some code to update the ui so let's add a using directive to access ui classes and now let's create ourselves two new fields from type of text one for the coins and one for our distance then let's create a new method and i call it refresh ui and here i just need to sync our ui text with the values from our game data object [Music] okay that should be it now let's go back to unity and let's test out real quick if that works okay doesn't seem to work um and we also have some errors in our console so let's have a look okay you see um we forgot to drag our ui object into the slot in the main manuscript in the inspector so let's take care of that real quick [Music] alright so now let's test it out again so let's start a new game and let's try to collect some coins okay now when i go back to the main menu it should have carried over our coins okay good that works properly now let's see if it adds coins properly when i start a new game remember we have 8 coins at the moment all right so i collected nine coins i guess so the eight coins from the first run plus our nine coins should be 17 coins in total right now all right and there we go seems to work properly now let's try if our data gets carried over when we exit and restart the game and start again as you see it works totally fine alright guys and that's basically it for today's episode we managed to create ourselves a simple system to save and load our game data we will expand this in the future if we need it but for now it's totally fine in the next episode we will take care of spawning and we will refactor our current method with a pooling system this will give us a much better performance i'm pretty much looking forward to this if you enjoyed this video guys please drop a like and also consider subscribing if you want to see more tutorials and devlogs like this our next sessions will be very interesting because we are moving closer and closer towards release also i really appreciate your feedback guys so please make sure to share your opinion in the comments and also feel free to ask me any question that you want with not much further to do guys thanks for watching see you in the next video take care bye bye
Info
Channel: Quick 'n Dirty
Views: 15,113
Rating: undefined out of 5
Keywords: unity, unity 3d, gamedev, game dev, indie games, indie game, indie game dev, indie gamedev, devlog, indie devlog
Id: a4ImOZMPjvQ
Channel Id: undefined
Length: 14min 29sec (869 seconds)
Published: Thu Jun 24 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.