Godot Save Dictionaries to a File Easily Using GDScript

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
here's how to take any data type and save it into a file and then open it back up again and have it back into that data type specifically I'm going to be showing an example using a dictionary okay so we have our new game project and for an example I'm just going to be creating a node because I'm not going to be putting anything on the screen necessarily I'll be using just the debugger output to prove the example so I've created a script again this is just an example so I don't really need to do everything necessarily by the book this is just to prove that it works so to save files you're going to use the file class which is used for reading and writing files so first thing we need to do is create a dictionary and I want to put some things in it so so we're going to do vector vector and we'll do an array and this is all just to prove that this does work 0 1 2 3 so you can even nest a dictionary in a dictionary you can also the values are the you can put like objects in two keys which is what I was doing I was using a vector 2 for coordinates and then I would put a boolean value of whether or not that was filled in for something I was working on so here we have our dictionary and then what we're gonna do is we're gonna create our file well first let me just print out our dictionary just to show you what we got so I'm going to run the program I haven't defined it main scene we'll just choose node there we go so see down here that's our print for the dictionary and you can see it's got the array the vector 2 is in the key over here and the boolean value so in this case here we're going to open a file and I'm so the other thing I was doing as well was I was creating a level editor and I was saving the level names back into Godot as a resource file that way I could create a level editor inside Godot for the game that I was making and not have to have a bunch of scenes or anything like that I could just load from from this this file the different levels and then populate the scene itself and then I could dine you know I could do all kinds of changes and stuff to my code and whatnot in the logic of the game and I wouldn't have to worry about any work being done on levels breaking because of the type of game I was using or I was creating it made a lot of sense for me enough talking let me put some some of that code in here so we're gonna do file and so right here I'm calling file again right and that file right this file right right here is a constant value has nothing to do with this right here you can always pull your constant just like somewhere else so let me let me show you what I mean we'll just print it out so file right is actually number two and so this is just pulling the constant ink and you're like well why is it nested in there it doesn't make any sense so that's just pulling a constant from the file class and I guess technically you could think of this this is a separate class from this this file variable that you've created because this is an instantiation of file but this you're just saying hey what's your constant so we're gonna store string and here's the trick to it all so the store string method takes and whatever you put in there it's gonna save it as a string to the file but if we just put dictionary in there it's not gonna do exactly what we want we got to use a GD script function called var to string and this will take our dictionary and turn it into a string basically what we saw in that echo earlier now we're just going to close the file and that's that so what will happen here when we run it again is we're gonna have a new file created in this path inside because in this instance I'm using this re S which is a resource so it's gonna save it locally into Godot if we were saving to a user file we would use user : I need to create the folder first because it doesn't exist so we'll create a folder called level and let's refresh our program and now don't make me a liar there it is what does it look like so I will open it up in a file manager here on my computer here it is right here I'm gonna open it with well this is your going to see everything I have on my computer's is a little I feel a little exposed right now here visual studio let's bust that out so that's gonna load up here on the other screen I have going on so there we go you'll notice to a dictionary because it's a key value sets the order isn't concern and so it's stored it in a different order but that doesn't matter because we call by Keane so that's what the file looks like so how do we load it this code here saves the file so I'm gonna MA I'm gonna cancel all that out this code here is where you're ever you're going to be loading from so I commented that out to just show that okay that works it saves it right but now we need to now we need to load it we're going to do file open just like before we're gonna pull the same string the only difference is we're going to use the constant of read so read goog oli my typing and talking I'm just not very used to it okay so we're going to read now let's throw that back into this is going to be a new variable called read read dictionary and this is just to prove to you that I am pulling from a new variable I'm not doing any like tricks okay so here's what we're doing we're pulling the file and we're getting it as text we needed as a string so get as text is a method that retrieves the contents of the file as text and now we're doing the another function 4gd script called string tavar so we're putting that text we're taking the string and turning it back into a variable and now we just close out and I can echo but I'll do that first so I'll just print this out in the debugger to show you that yeah we've got a dictionary but the really proofs in the pudding so let me pull the actual values all right so we've got read dictionary now let's pull the effect to key right so we're gonna do vector to zero one so this right here is you can just like you can call up an array you can you can call the key to get the value inside of a dictionary here so we should get the the in here we should see true and there it is and we can do the same thing here I've named an array array as a key inside of a dictionary and we'll run it again and there's our array so that's the proof right there so we're loading the contents of this file we don't have to do anything extra we don't have to do any to JSON and do converting the values of an object and then put it into something that that's friendly there so the here's some caveats that I would suggest avoiding so the kinds of things that I would only recommend for saving to a file using this method are the things that Godot supports this variant types so if you're trying to save your own custom objects I would I would strongly discourage that and I hope you find this useful I'm gonna be putting up more videos like this for a good oh and maybe some other game dev stuff in the future so go ahead and subscribe and like if you like this and leave a comment if there's something else you'd like to see for me thanks and have a good one
Info
Channel: Learn Video Game Development
Views: 2,693
Rating: undefined out of 5
Keywords: Saving Dictionaries Using Godot, Godot Save Game, Godot Save Files Without Using to_json, Saving With GDScript, Save Games in Godot Easily, Godot Tutorial, Godot, Godot programming, Godot GDScript, Godot Dictionary
Id: 86fvMk1tkeM
Channel Id: undefined
Length: 9min 28sec (568 seconds)
Published: Mon Dec 16 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.