Godot Save Game Tutorial: Save and load using Resources

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we're going to see how to create a safe system using resources in Godot a safe system that will allow you to do this - open your save in the inspector and to edit any of the properties you have in there this is the one we use in open RPG at the moment and I'm going to run you through the code note that for a safe system there is no one way to make it this is one of the ways to do so there's another one we covered with Jason in the good at course and you won't have to adapt the code to what you need to save in your own game right but this should help you learn to create your own system and a flexible one so this safe system is going to rely on a game saver script that we have in open RPG and you can find the link to the repository in the video description this game saver script and node has two methods one called save and one called load so it's responsible for writing the file to the disk and loading the file from the disk then it's the individual nodes individual objects in the game like for example the characters in the the RPG that are going to be responsible for feeding the data we need to say from them and loading them back into themselves but let us first look at how save and load work here and we'll jump to the elements that get saved as we run through the code so first to save and load the game into a resource we have a special resource called save game I'm going to open the file you can see that it's a very simple one it just has two variables the first one is the game version you use that for file versioning it's very important say you release your game in early access or after release you have a bug with your save system or something like that you need to change the data you will use the game version for backwards compatibility you can check that the save came from an older version of the game and update the save data or load it accordingly so this game version will get it from the project settings if I go to project project settings and to config I've added a property here called version so you can add properties using the bar at the top when you go into a even category make sure to look at the path so for example application and config the path is going to be application slash config if I expand the window here if we can see it you can see the path to any of the properties by hovering it a pop up will show you what this is the category / sub category slash the name of the property in lower case so for application and config its application slash config then you can give your property a name and a type if you string in here and click Add to add it so I've already done so in the project as you can see and so we can access that property we can get it and set it from the code using that path application slash config slash version this is exactly what we do in game saver so we create a new save game that has this game version and a data dictionary and the first thing we do is we get the game version from project settings project settings is a singleton you can access it anywhere in your code right and then we are going to feed data into this data dictionary so it's going to hold everything that we save in the game but the entries in the dictionary it's the nodes that we want to save in our game that are going to feed that I'm going to enter inside of that we have a group in the game called save that virus nodes are assigned to and for every node in this group we're going to call the nodes save function and we pass in our save game resource here so let me show you an example of that if I go to the Part C in the game and I click on one of the party members in this case cadets but it's the same with Roby if I go to the node and groups have added them to the save group so anything you would like to save you just click on it and you add it to the save group and the note tap phone now we only have the party members then these nodes if I go to their script and I go down to the bottom of the script they have a method called save another one called load and this method is going to feed data into the data dictionary so we have savegame which is our save game resource and we access its data field then we create a unique key for each of the nodes that we want to save so that we know how to save and load the node exactly where we saved and loaded it in the resource and we pass the data that we want to save or we get the data that we want to load now about that key you have to generate a unique key that will really be unique for each of the node that you want to save so there are a few ways to do that you could use a get path for example on your node because the node path should be unique the problem is if you change where your node is in the scene tree which is likely to happen as you develop the game you will have to convert the path to the new save file format so this is a bit dangerous instead if I go look at my safe key variable I generated at the start of the game I have a prefix for the type of class that I'm going to save so I use the class name here plus name the idea is that these types of nodes like party member I use them at the same level in the tree so for example each party member is under the party node here Godot will make sure that two nodes at the same level can't have the same name they will have to be unique if I try to rename my good at Robbie Godot would rename it to Robbie - so my name here will be unique for each element in the list and with the prefix I kind of make sure that I'm not going to step on my own toes and that the idea should YOUnique nothing else in the game should overwrite it I'm going to close the scripts and now back to game saver while we continue in our save method so the last part of the save method is all about writing the file to the disk we create directory object which allows us to check if the directory we want to save to exist on that this path to the directory is stored in a safe folder variable in this case it's debug slash save as we are building the project in pre-production if the directory does not exist we call make directory recursive on that directory object and it's going to take care of creating all the sub directories that we need to save the file because if the directory doesn't exist and you try to save a file to that directory it will not work you will get an error then we calculate the path to the found the savegame that we want to save so we use our safe folder path which is a string and strings have a plus file method that allow you to safely concatenate file paths so if you are missing a slash it's going to add the slash for you but if you have a slash at the end of say folder it's not going to add it so it's a bit like Oh s dot path or join in Python and I use the save name template so let me go back to the top of the script same name template is save underscore three digits that we need to replace dot t res which is the extension for resources I replace the three digits with the idea and the ID is an integer that we pass to the save method now we can use resource saver a singleton to save the resource to the disk and this is the same class that Gerda will use to save all the resources you create through the inspector so on the textures the shaders whatever it uses the same tool can call the same method on that which takes the path to the file that you want to save so this is the full path with the folders and the file name and the resource that you want to say so the savegame that we populated with all the data I want to save here if the file saves it's going to return the value zero so this is an error value in Godot and you have constants in Gd script to check the arrow type the constant okay corresponds to you the value zero so if you get zero the file got saved no problem but if the value returned from results IVA that save is not okay it means that you got an error then you can check for different errors if you'd like and print different things or you could use an assert or something like that in our case we're just printing to the console next up is the load method same thing it takes the ID of the save that we want to load and it does things the other way around we first calculate the file path that we want to get so same thing use the save folder plus file and we feed the ID into the same name template then we create a file object not a directory one to check if the file that we want to load exists instead of using a check and printing to the console here you could use an assert and assert that the file exists it's going to return true or false in that case so you could just do that and if while the file does not exist it will give you an error it would crash the game quote-unquote then we load our resource based on the file path so this is going to create a savegame resource like we have here except that as this is a resource Godot can load it by itself it just knows how to load it we store that in a variable then we go back through all the nodes that are in the save group and we call their load method passing in the savegame resource and the nodes are going to load the data they need on their own and that is it now to save and load the game you have to use some bit of interface and we don't have a for save interface at the moment but we have our debug interface here so I can show you how this thing works because that the same principle you'll use creates a savegame UI in your game has a spin box to set the ID of the say game that we want to save or load a Save button a load button the two buttons are connected through the editor to the debug interface node and it's script we have two methods in there on say button pressed and unload button pressed we call save and load on the game saver node passing it the spin box's value which is our save ID the one that won't save all load and the only extra thing that we have here is an initialized method so that a parent node in the game can feed the game saver node or script to that bit of interface had the game saver first and the game scene but we can do better than that we can inline it in the interface because the nodes just provides two methods so we don't have to pass it around I'm going to delete the node and remove the line of code that initializes the debug interface from the game script and then we can go into the debug interface scene which is also going to open it script and remove that part where we try to use the initialize method to grab the game saver instead we can just place the game saver node inside a video interface scene and use an unready variable to get that game saver note and we'll get the same functionality the last thing I want to talk about is a bug that we've seen in guro 3.1 alpha 4 I'm using right now which is that the resource if you save it here into the res folder will not always get saved properly it will work if you save to the user path so I'm going to open game saver again the safe folder is in the project's resource folder so that we can edit it from the editor but goodö doesn't seem to like it too too much so sometimes once you save that resource it will override the values and reset them right it will reset them to the the default resource which can a bit of an issue here so if you'd changed that path to user here which is where you want to save your user data no problem it works as expected now Nikolas mentioned another issue with that approach using resources yes we can edit it in the inspector theoretically but I'm going to change the path back to resource and going to create a save game here by clicking the Save button so I've unhidden the debug interface now we should have the file right there if we try to edit that file in the text editor so I'm going to use Emacs for that I'm gonna go to Godot debug save save zero you can see here that our resource has external dependencies if you were to ever move this combat bat list as character stats resource the save game as well this will not get perfected automatically by Godot so you will have to go edit these lines in the save file to convert it to the new version which does not necessarily happen if you are working with Jason the method we covered in the good at course because it's not just data it's data with functionality that were storing in our save game here we are storing the references that's one of the limitations of this approach using resources thanks to Nikolas for pointing that out that is it for our save game story which was brought to you by our patrons on patreon so big thanks to them for this vote sorry if my voice sounded a little funny I'm sick at the time of recording but hopefully the tutorial did not suffer too much from that that said I want to you thank you kindly for watching be creative have fun be sure to subscribe and that said see you in the next one bye bye
Info
Channel: GDQuest
Views: 46,391
Rating: undefined out of 5
Keywords: godot resources, godot save and load, save game tutorial, gdquest, godot save, godot save game, godot save tutorial, godot save dictionary, godot save data, godot rpg tutorial, godot rpg game, godot engine, open source, game development, game creation tutorial, game development for beginners, gdscript, how to, open rpg, godot open rpg, godot rpg 2d, godot rpg template
Id: ML-hiNytIqE
Channel Id: undefined
Length: 15min 13sec (913 seconds)
Published: Fri Jan 11 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.