Save And Load Data in Godot 3.2
Вставка
- Опубліковано 24 кві 2020
- Saving and loading data can be a daunting task if you've never done it before, but it's a lot easier than you might think. In this video I'll be showing you how you can save binary data onto your player's machine so that you can retrieve it later. This is useful for allowing your player to come back to their game without losing their progress. You can store many things such as player stats, levels completed, and even collectables they've gathered.
Error Codes: docs.godotengine.org/en/3.2/c...
Source: github.com/GameEndeavor/save-...
Become a patron to get exclusive content and support the channel:
/ gameendeavor
Join Our Community! We look forward to welcoming you.
Discord: / discord
Twitter: / gameendeavor
Into: Splashing Around - The Green Orbs
Howdy everyone. A little disclaimer since I've gotten a few messages about this. The password at the end is NOT a legitimate password. The one frame reveal is on purpose and intended as a joke, so enjoy. :D I'm touched though, I didn't expect so many people to be concerned, but thank you kindly. Enjoy the video!
thank you, "pb" for thinking about us explorer types and for telling us about the Easter egg in the video. looks like a really legitimate password for being a fake, I think that was why their was real concern.
Howdy, its me flowey
@@Wolfyxon Howdy, Flowey. :)
@@GameEndeavor
I think i too much played Undertale
I turning to Flowey
@@Wolfyxon Is that the flower? I still need to beat that game.
Creating a save system has been something I've put off so long on my current project (which is my first real game, and my first project with a save system!) This tutorial was so straightforward, and I really appreciate that here, and in all of your videos, you go over not just the steps, but WHY we use them, and what they mean.
I also love your sense of humor, charming and engaging without being overly zany and distracting.
Anyway, all this to say, thank you for making videos! I'm a little less daunted by the prospect of writing a save system, now :)
This is genuinely the nicest comment I've ever received, thank you very much. It means a lot to me. :) You pretty much captured the concept of this channel in one comment. All of that is intentional, because it's what I would like to see more of. Especially the why and what it means. There's no point in me trying to teach anything if the viewer leaves the video not understanding the concepts in the videos so they can implement it on their own.
Good luck on your save system, bud. :) My first was daunting as well, but it's not so bad if you go about it the right way.
I thought I knew how to save and load properly, turns out I was missing on some nice stuff. Amazing as always.
There's always something new we can learn and improve. I find stuff I've been missing out on practically every day. :) Thank you for watching.
I CAN’T believe it’s this easy. Thank you, this topic was kind of an elephant in the room, I’m this helped me a lot, finally I dealt with it. Keep up the good work!
Right? I've always found the lack of binary file saving to be pretty odd, given how simple it is. I'm glad it helped, thank you for watching!
you forgot to say how you access those variables to reassign them (when starting the game)
The way you access variables in the list / dictionary is:
player_data["health"]
or really any variable in the library with [brackets] and ""
so you have a health variable, and a "health" variable in the dictionary list.
Then you just add:
health = player_data["health"]
to the load function.
hope this helps!
Thank you, this helped me. But now, everytime when i restart game or press load before saving, player data gets corrupted. Labels with variables just say "NULL". Do you know any solution for this?
Great straightforward tutorial! This is exactly what I was looking for!
Your channel is a literal BLESSING. I don't have a lot of time to work on game dev and I love how your videos just get to the point!
Save/Load data is something I have a hard time with and I'm learning a ton from this video
Thank you! That's been the goal of this channel from the start. Information but dense without the usual tutorial fluff / handholding. Modeled after the style of tutorials that I desire, because I also don't have the time to sit through half an hour of 5 minutes worth of information.
I was starting to miss your videos, and you just made the kind of tutorial I needed. Thanks so much!
Ha, thanks for watching. :) Sorry for the disappearance. Got a little busy for a bit, but I should be back now, so expect plenty more videos! Thank you for watching.
@@GameEndeavor Oh yeah, I know! 2 jams in such a period of time must have been a bit time consuming
@@joelchrono Wasn't just the jams, I had a bit going on before that, but yeah jams require so much attention, especially the week long Kindred Jam, lmao. :)
Hey, i'm really glad i found your channel, this is golden and i appreciate the effort for putting subtitles in your videos, keep up the good work buddy!!
Thank you! It's surprisingly no effort. I just upload my script and UA-cam transcribes it for me. :) A feature I wish more people took advantage of.
This is the best tutorial on saving by far. So much info in 10 minutes. Thank you.
This was so simple that I thought I missed it. Thank you!
VERY helpful. because you go full on the detail and not miss important bits. thank you
Epic!
Just Epic,
all the save data videos and web pages Ive looked at never went so deep into how it works, or even where the file was saved,
and worse, none ever said the order of read and write matters, I kinda thought it did, but Ive not tried anything yet,
I really like the encryption with pass, excellent stuff!
Cool video! I'm just starting off with Godot and I had no idea about how to save anything but a scene. I'll make sure to play around with all this until I get the gist of it. Thanks!
I'm glad it was helpful. :) Good luck with Godot!
Amazing as always! Glad your back to making more tutorials :D
Thank you! I'm looking forward to making more. :)
Thanks for pointing out the native encryption! Very helpful!
Congratulations for the video, it's wonderful.
Lol. Sometimes I am really not expecting your humor and it just gets me haha.
Glad to hear it. I do like to be sneaky with it sometimes. :)
@@GameEndeavor Hey question. does using this make a new JSON file every time you run the save function because of var file = File.new()? Total noob here tryna figure this out.
@@youngwood5704 It doesn't. It makes a new File resource which opens the file and reads the data from it.
Thanks sooooooo much. This couldn't have come at a better time.
Thanks for watching! Glad it was useful to you. :)
thanks for this. this is exactly what was causing me to do some major head scratching
Glad I could help, thank you for watching. :)
I have finally understand the save and load logic thanks to you!
Eyyy, glad I could help. :)
I was about to give up on making my game but this video saved me sooo much. Thank you!
Glad I could be of use. :) Thank you for watching!
Are you going to do a second part to this?
@@cal_azure5676 I assume you mean to cover how to load levels as they were saved? ie with an rpg you might save the player's location, treasure chests looted, enemies defeated, etc? And use that data to instantiate the world. It's the only thing I could imagine I'd need to cover in a second part.
Yeah, it's on the list. It's not something I intend to cover right now. I need more experience with the style of games that require this type of system. Mario style platformers don't really need such a robust saving system, so I don't have as much experience in this area. But I plan to start an ARPG series at some point which is where I would cover such a thing.
Yeah that’s what I mean
Awesome tutorial! Would you please make a video on how to lock and unlock levels/ stages? I'm badly in need of one and can't seem to find any. Thanks!
Well made as usual! Well done GE!
Thank you kindly!
Thnx a lot. Very Useful!
Thanks mate, this was really helpful!
Hey is there a way to save scene name so that I can load and immediately use change_scene on the save file
Btw sorry if there is an obvious answer to my question cus I'm in the 1st week of learning godot
Wow, this was an incredible tutorial with so many tips
Thank you. :D
Great video direct and to the point love it :)
howdy such a great video I'ma try this in my game
omega 3 and all that keyboards need love too
Honestly the best comment I've ever received.
Awesome, I love your tuts
You're the BEST, thank you and keep it up ! :)
That's a great tutorial, thank you :) I was thinking it was way harder than that. Do you have any idea of how to create an autoload saving system? I was thinking maybe by implementing a timer with a signal that each 30 seconds the game will save automatically, but I ran into some weird godot errors 🤔
Amazing tutorials as always
How do you save and load multiple variables? Like you would have other variable to save than "data" here.
so my saving function is on the other scene, and the loading function is on the title screen scene, does it still works tho?
Thx!
I thought it's very difficult but it is very easy!
How can I automatically assign the value to the dictionary so that it is updated as the player is updated?
Hi- what would happen with this process if you released a new (update) of your game. Would it create a whole new file, and the player would have to start again, or would it be smart and read off of the same fil as in the previous version? If this happens, how would you fix it? Thanks!
I cant export project! Help me!
My exported game works fine in the editor, and the debug export but not if it isn't a debug export
So in the func load_game I added unlockedLevels = player_data. So when it's loaded the data from before will be what determines # of levels unlocked. But the player_data is written as unlockedLevels:2, (2 is from the save). So help me how do I get the 2 and nothing else? (Because unlockedLevels needs to be a int/float not a string)
i have a littler problem, when i write a file multiple time it append in the current file. but i want the previous content to be removed. how to do so?
basically, how to overwrite the content ?
What if I want to implement and save a new thing into my game? I cannot save it without deleting my save file, right?
tbh i still dont understand it but i have watched about 10 tutorials on how to save n load and this is by far the best one
It can be tricky to get started. I hated trying to learn it when I started. If you dabble with it long enough then hopefully you'll get the hang of it. :)
@@GameEndeavor thanks man i appreciate your work so much do you have instagram??
@@batatachibbbez Thank you! Glad you find it useful. I don't. Discord and Twitter only atm. :)
@@GameEndeavor I would love to join your discord later but I don't have discord at the moment so I will join tmr or smth KEEP UP THE GOOD WORK ITS ESSENTIAL😂😂👍👍🤝🤙
is there any way that we can save the current state of the game automatically I don't want people to click buttons to save and load the game, most of the mobile games doesn't have those buttons and when we exit and enter all the things we do saved automatically, I really don't know how we can do that
Fantastic as usual.
Thank you! :)
how i can load the game after saving? (i mean restarting the game by the point i saved)
How to use json with gdscript to make a system, where if I completed a level, next level will be unlocked... Like candy crush...
Honestly, I've been trying to do this for months but I couldn't get my head around other tutorials. This helped me a lot and you did a good job at explaining and walking us through the process. The only thing I don't get is how do I actually get the data from the player and link the data in "var data" together.
yeah. that's everyone's problem probably, saving is easy as there are godot documentations specifically for that, this tutorial would be actually useful if he could have tought us how to access that data and load it.
The source code only contains empty folders, when will the source code be uploaded?
Bro please help me I learn c but game make code are Different why
Hi!
I made my save system with this tutorial long ago, but I haven't done the encryption part. Now when I'm trying to do it it returns an error:
open_and_parse: Condition "magic != 0x43454447" is true. Returned: ERR_FILE_UNRECOGNIZED
Can you help me?
Sorry for my (maybe)bad English
Thank you!
Woah! Thanks a lot!
If i encrypt the data the whole things stops working for me and i get error 15 which seems to be the unrecognised file error. Any idea whats going on here?
EDIT: I figured it out. When adding the encrypted part you have to delete the unencrypted data file, then when you play the game again it re-created it and it works.
Hello thanks for the tutorial.
How do you work if update new variables after save and load data?
Let say
Var hp = 100
Then save and load
Then i make new variables
Var defense = 50
If i load my data
The godot engine said that
Defense = null
Yeah it happens because defense is not in data yet. I need to save the game but it will reset all data, please could you explain to me how?
Hoping i could see new tutorial about saving data on Google play service.
I dont think this could help player to reopen their game on another phone without moving the data files
At 5:34, is it OK that file.close() is under the if conditional? What happens if error != OK and file.close() is never called?
Thanks for the great tutorial!
If it errors then the file never opens, to my knowledge. :) I've never had an issue doing it this way.
This leads me to the rabbit hole why I never make games. I want to be able to everything so I do nothing when I can't do one thing from seven things :D
Example what I want to be able to do: First how to add ability to people to save custom keys -> Then have ability to safe file saving these keys also so it remembers your options and stuff on top of competed levels and unlocked levels xD and power ups, unlocks....
So not only do I need to learn who to make safe files I need to know how to make rebindable keys :( that the game remembers... so I often just give up. Maybe this time I have patient to learn one of these systems
I finally got it to work big brain
i am saving my player position,it saves and loads it but then how do i apply the loaded position to the player?
You need to give that information to the player and have them use it. Saving a game is a whole other much larger topic unfortunately.
how did you make the ui in the beginning of the video? it look nice.
It has been awhile, I don't remember. I do know I spent way too much time on it for a 5 second bit, lol.
To save "position" of my object and player , should we use store _float ????
You can if you want, or you can store it in a dictionary like demonstrated. If you use store_float() then you'll need to read it in the exact same order that your read it.
Thank you so much dude!
Any time. :)
Thank You!❤️
Quick question: can you call the save file format however you want, like make it a .sav instead of a .dat?
Great tutorial, very clear and helpful, gonna keep it handy!
Yes, the filename doesn't matter one bit as far as I know. You could even call it .png if you wanted, lol.
What's your view on using config files to save data? is it less efficient?
I would imagine it is, but I don't know the specifics of it so I wouldn't quote me. But I would imagine that File is more efficient at reading and saving data, since it's done sequentially. Whereas ConfigFile uses the whole tag system and likely needs to iterate through the file each time you get a variable. If so then this could be quite the slow down for really large files. Also I would imagine it's a lot more space efficient since ConfigFile stores everything as a string, depending on what you're saving.
i chacked a lot of times and for some reason the file never saves
Does this work well involving procedurally generated levels saved as a chunk?
It would require more work, but yeah you can save anything in a binary format.
Thanks, yu help me a lot
How would i save the data from a different scene?
With the use of a Singleton I would imagine. Depends on the layout of your game.
Thanks for your great tutorial !
I have a question about it : when I use save and load for a big array (I work on a "game of life" version) with overwritting, the new file is getting bigger and bigger every time. Do you have a clue where this comes from ? I checked the "close file" in my code.
The only thing I can think of is you're appending more data into the array or the file resource is persistent and accumulating data. I would inspect the contents of the array and the file being saved.
@@GameEndeavor
Thank you, I thought about unexpected appending data too. I think I will check the code with a smaller array first.
there is nothing in the git hub zip file
very nice tutorial!
Glad you liked it!
i have a question does godot support a smooth animation i mean if you have 2 pictures and it make automatik a animation from frame 1 to frame 2 does this exist?
As in, will it generate sprites in between those two frames? No. That kind of thing would require machine learning and not the purpose of Godot.
@@GameEndeavor no i mean like i have a red color and i want to smoothly fade in to a green color would that work?
@@Gost-nf3cr Yeah, you can use an AnimationPlayer, and keyframe the module or self_modulate from red to green. Or you can use a tween.
@@GameEndeavor ahhh yes i see tnx soo much
@@Gost-nf3cr You're welcome. :)
omg tnx SOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO much
My game does not save or load help me
Does this work in Godot 4?
I want my game to load a specific scene when I press play, and I want the game to change what scene will be loaded when a certain scene if loaded for the first time. Basically, the first time you play, you will go to scene one. If you get to scene two, the next time you play, you will go to scene two I use Godot 3.1.
Please, how do I go about this?
I assume you can use a load scene which will check for the save and redirect you wherever you want. I'm sure there is a better way to do this thought.
just sve a string "" of the scene ant then when you load it yust load it into a change_scene()
Thank you, very helpful. One thing, should we put file.close inside the if branch? I am guessing if there is an error, the file will close automatically?
I assume that if there is an error then the file doesn't even open, but I'm not 100% certain about that.
@@GameEndeavor Right, that makes more sense. Thanks!
You're welcome. :)
Absolutely fantastic tutorial! Not only did you show us what to do but you told us why and made it entertaining, too.
I've been working on actually using the data that is loaded but one thing that keeps tripping me up is switching to the scene once the dictionary has loaded the data. I have it save to a load_scene variable and then have it call get_tree().change_scene(load_scene) but it can't parse it correctly.
So i'm wondering, how do you actually switch to a scene that was saved and loaded?
This just saves data. It doesn't save the scene itself. That's a whole other topic that is quite challenging. The typical way to handle this is to save the data you would need to recreate the scene yourself.
@@GameEndeavor I'm kinda late, but I'd greatly appricate you doing a tutorial on doing exactly this. Being able to do it between scenes is needed for a lot of games and there aren't many great tutorials on it.
Or if there are they use a system or method that differs from what you do in this video by a bit or have other linked videos you need to watch before you get to swapping the scenes.
Perfect tutorial
You're too kind, thank you. :)
5:28 Shouldn't you want to close the file regardless if the writing throws an error or not? Same with reading the file at 6:25.
I assume that if the file errors then it doesn't open in the first place. Opening errors are things like bad paths, no permissions, corrupted files, etc.
Hi excellent video! but how I can get the value when loading? eg: if I save "points" = 100 in the dictionary, when loading, which var is saving the value to manipulate it ? Sorry I don´t speak english and I can´t get all that you say
Howdy. :) So you put the value into the dictionary, and then write it to the file with `store_var()`. When you load the game, you'll load the dictionary with `get_var()`. This will return the dictionary that you need to store into a variable. You then use this dictionary as you would any other dictionary.
So `var data = {"score" : 1900}` save it with `store_var(data)`, retrieve it with `var data = get_var()`, and then you can get the score with `data.score`. Hope that helps, let me know if not. :)
Game Endeavor got it!! I was trying to manipulate data that is a dictionary, missing data.score that is the variable thanks a lot!!! I wish you the best
@@martinpellicer8489 Glad you sorted it out, bud. :)
Will this work with a mobile app? If not is there a way to make a save system for a mobile app?
I've never developed for mobile, so I'm not sure. I guarantee there is though, and I wouldn't be surprised if this method worked.
does it work in android? if yes are there any changes i must do?
I haven't developed for android so I wouldn't know.
Thanks man, how do i tell godot what level to load from the file, i have a continue button and nothing that i do works
You could save the file path to the level and use that in your level loading logic.
@@GameEndeavor really not helpful, but thanks for trying :D
@@GameEndeavor And sorry for bothering you, how do i access the json file in the start menu to tell godot that i want the "current_lvl" from the json, to load?
Can I also put my save data in the global script
You can put it wherever you like. :)
Oh, yeah. Finally some good fucking tutorials
thanks
the github repo which you linked is empty. may be you didnt push your commit ?
I did, not sure why the folders were empty since I did it as one push. It pushed the folders just not their contents. I deleted the repo and reposted though. Seems to be fine now, let me know if not.
and if its possible an automatic save system
damn that secret password is not blured its: "P@paB3ar6969"
Nice.
How to save "scene where we were at last" ?
You can save the file path of the scene and load it.
i also want to know this, but i can't figure out where i have to start.. :\
@@attilanemeth6204 try looking up what you can do with autoloads / singletons, that might be a good starting point
@@Eviled Thank you, after i wrote it, i solved my problem, so i know how it works and what can i do with the file or configfile :)
@@attilanemeth6204 alright
nice!
Hi. Maybe someone already noted this to you but... When you blur text on screen, for example something similar to: Pap@B3arKINKY, better make sure to blur all of it.
Yup, so many people that I pinned a comment about it. It was a joke, bud.
How you so good at this...
Lots of practice. :) Game development is so much fun to me, I absorb it like a sponge.
I have one question: when you create the dictionary, can you assign global variables that already exist to the dictionary variables?
just to be clear:
In my little game I want just to save 3 parameters (collected coins, enemies destroyed and hostages freed). These values are stored in global variables.
Every time you beat a level, I want these collectables to be stored and saved automatically, so at the next run you will have these values usable to continue your adventure (like... you can continue if you have collected a certain amount coins, destroyed a certain amount of enemies or freed a certain amount of hostages... like credits...)
I hope it is clear.... sorry but I am really new to the godot engine and in game programming in general, so I apologize if my question seems silly.
Thank you
Regards
I've been waiting a long time for a tutorial like this! Thank you GE! But how do I go about saving my player location in a .dat file? + I need to be able to save multiple variables instead of arrays?
This topic has been requested quite a few times for a while now, so you're not alone. :) Thank you for watching! You just save the player's position as you would any other variable, then when you load the scene you apply the position to the player after loading the level. Level loading is a whole other video on it's on that I'll get into at some point (I need more experience with it myself).
To store multiple variables, you just call one of the `store` methods again. You can call as many as you like. You just need to make sure that you load the data in the same order that you saved it, because it saves and reads sequentally. I should've demonstrated this, so that's on me.
@@GameEndeavor Ohh thats how to store multiple variables, It's working, thanks again!
@@GameEndeavor
To save the players location in the same scene;
in the SAVE function;
var x_pos = 0
var y_pos = 0
x_pos = $player.position.x
y_pos = $player.position.y
file.store_var(x_pos)
file.store_var(y_pos)
then in the LOAD function;
var x_pos = file.get_var()
var y_pos = file.get_var()
console_write(x_pos)
console_write(y_pos)
$player.set_position(Vector2(x_pos,y_pos))
@@Fizmox you can just `store_var($player.position)`, then just `$player.position = file.get_var()`. store_var will store the Vector2, so no need for the extra steps.
@@GameEndeavorAh okay! I thought you can only store the variables
Hi bro, your video helped me a lot, however I'm still in trouble. My idea is: save the level, when the player passes through a portal at the end of the level, and when the player closes the game, and presses the load button, he will return to the same level he saved. But I don't know how to put that in the dictionary.
will only be saved when the player passes through the portal at the end of the level and moves to the next level. How can I put this in the script?