Easy Saving and Loading in Godot (Tutorial)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi the other day i was helping out a friend with some saving and loading in godot and i figured i might as well make a video about it so what i prepared here is a little scene let's see i have a checkbox in the corner that well yeah can be checked i can change the background color and i can move these three things around and i would like all of these things here to be saved and loaded again when i boot up the application now currently it doesn't do that currently if i start it everything is back to default just as it looks in the editor now how can we go about actually saving and loading stuff here what i like to do is i like to create an autoload script let's just call it data manager manager and the product settings data ah come on just select it easy add okay so this data manager script can take care of everything for us so let's see we give it we we need a file name if we want to save something so i'll just say save dot data the file ending doesn't really matter because this just saved us binary data for godot anyway but you can be creative if you want something specific and i'm going to be putting all the actual data into a dictionary i'll just call it data now we'll need two functions here load data path and save data path i guess we can start with saving data because otherwise we have nothing to load so we can open up a new file object and since we are writing we don't actually care the file exists yet so we can just say open the file in write mode file name file dot right let's make sure it's opened in right mode so we don't care what exists we don't try to read it at all now file.store and put data in there now that's our data variable from up here so anything we put in here we can just store in there which makes dictionaries quite nice because we can put a whole lot of data in there and without having a whole bunch of store calls of course if you have separate levels and you want to sort or data for each one it can be helpful to have multiple save files or something of the sort that way you don't have to load up all levels at once but can only load one at a time but in that case just do the same process multiple times it's not really that different so make sure to close the file again because we don't want to leave that open when it's not being used and that's basically saving already done now loading we open it in the same way but we first want to check if the file exists if the file let's see if file.file exists user class filename then we can actually open it if it doesn't exist we have to go with some default data because there's nothing for us to do so if this exists we can say file dot open [Music] file name we should have probably made a file path variable but whatever uh we want to open this in read mode file.read and now data equals file.getfile close the file again and that's it this is all the loading the entire variable we saved before gets loaded back into data and we can actually call this in our ready function to make sure it's loaded as soon as the game launches so go to ready load data once and we're good now what are we doing if the file doesn't exist well it's probably going to exist by the end of a play session but at the start we can just set some default values here so what i can just do here is data equals let's say objects for the movable objects i'm using that can also be a dictionary again and settings for the other stuff like the background color and the little checkbox we can do that in there yeah that seems fine now what we can do let's see this here would technically save and load fine already but of course we need to actually work on data we need to actually change the data so if we just go into the script for these ones these three currently are sharing the same script because all the script is really doing is allowing them to be moved around so what we can do we go in here and here this is the case where we are dropping something so whenever one of the objects is dropped we can say date data manager yes data manager dot data objects and our name so yeah you these do have individual names so each of these objects i can just use the name as a key in the objects dictionary and say this is now equal to my global position i'm using global position because it's safer i don't want to mess anything up with local positions but technically you can use that as well of course now in the ready function i want to make sure to load my old position so i can say if data manager dot data objects has my name so if my name is somewhere in there then my global position is equal to datamanager.data objects name and that should pretty much work by default one thing to be careful with we're changing it but we're currently not actually saving the change so we can just say datamanager.savedata the same thing could be achieved with a setget call inside of the data manager but for now i'm keeping it simple and calling safe data manually so let's give it a shot we have everything here let's just move this guy here this guy here and this guy here does that work it does everything loads in in the correct spot just where i left it nice now we want to do the same thing essentially for the color and other settings so we have a little thing here that just changes the color so in that case let's see we can add a thunk ready if data manager dot data object no no no no we do we're doing settings here settings dot house um let's give this a name actually background dot has name and we can use that again since it has its own name now then not sure let's see color no no we need we actually have to use the set frame color command so set frame color to whatever the value in there is data manager.data settings name instead we could turn this into an actual setting name by just saying this is called color now it doesn't really matter but since i gave it a background name that also works loading properly i think so now we need to save data manager.data settings name is equal to [Music] i guess we can just use the same one color picker.color so that's the one we are using up here anyway oh actually the color is passed here i made a mistake there earlier that's not even needed we can just do this easier and then we save that dot save data we don't technically have to save every single time we change something in a very big application that might become a little much in that case you may want to save in specific intervals on a timer or something or you could save in specific locations you could save based on any number of conditions i just don't have that many things here so i'll just save whenever anything is changed immediately let's take a look make it red load it and it's red nice it's remembering all kinds of stuff now lastly the checkbox this doesn't actually have a script right now it needs one let's see attach script yeah sure we'll call it checkbox i don't care now we already have ready here so might as well if data manager.data dot has and i'll just say no no updater settings dot has box checked in this case i'm not giving this a name just to show the other way of doing it so now we're just setting a custom parameter here then let's see what's this pressed i think it was pressed as the thing let's check it quick um yes it is okay pressed is equal to data manager.data and all of that now we actually want to react to when this thing is being pressed um no no no where is it there's a check box here this and in this case we can just say data manager dot data settings box checked uh no is equal to pressed save it and see what happens now we have this this is quite small here so yeah let's make it bigger you can move this stuff around still close it open it and it's still checked everything works everything is being saved correctly now simple enough now you can adapt this to store any kind of data you can really imagine just plug it into your dictionary and goodies pretty simple i think this will be all for today bye you
Info
Channel: iaknihs
Views: 1,650
Rating: undefined out of 5
Keywords:
Id: 3ZSmGpB7TIc
Channel Id: undefined
Length: 13min 25sec (805 seconds)
Published: Tue Feb 16 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.