Data Persistence - Save & load your game state while avoiding common mistakes | Unity Tutorial

preview_player
Показать описание
Learn how to save and load data in Unity - avoiding common mistakes and dangerous serialization techniques many other tutorials will tell you to use!

In this video we'll look at using text-based serialization using JSON and optionally encrypt that data. I also discuss some of the pros/cons for using text-based serialization, and what you should use instead if you really want to/need to use a binary serialization technique.

Common suggestions that you should absolutely not use to persist game state data are:
1. Player Prefs - these are not designed for storing game state. Only...Player Preferences such as graphic & audio settings.

📚 Resources 📚

❤ Believe in LlamAcademy's mission and have received value from the videos? Become a Patreon Supporter or YouTube Member:

🧡 Believe in LlamAcademy's mission and have received value from the videos? Here's how you can show your support:
📚 Take my Unity ShaderGraph Course:

Some links may be affiliate links, which at no additional cost to you, gives me a small portion of the purchase.

Chapters:
00:00 What you shouldn't do
01:45 What you should do
03:20 JSON Serialization
04:43 Sample Scene Overview
11:03 Demo Saving Data
14:23 Demo Loading Data
22:02 Encryption/Decryption Demo
23:05 Demo Encryption/Decryption Error Handling
23:47 Final Thoughts
Рекомендации по теме
Комментарии
Автор

Absolutely fantastic video! YouTube is full of tutorials for basic implementations, and you presenting the more complex and advanced topics, and doing a deep dive into them, is super useful and refreshing (and also important - this seems to be must-have knowledge to get a job in the industry as a developer).
The presentation was fantastic, good explanations, and the pace is great.

zivv
Автор

Thank you! It's SO hard to find tutorials that don't just say "use playerprefs lol" or indeed just refer to the binaryformatter.

bassfight
Автор

Hi. I wanted to thank you for this tutorial it really helped me out. I was retying your solution to test it out and noticed you are rethrowing the exception using "throw e" at 13:10. For anyone reading this comment please use just the "throw" keyword. If you use "throw e" you will remove part of the call stack. Sorry for pointing stuff like that out. This video is one of the best I've seen showing how to do things properly and I firugred my comment might save someone some pain in the future.

mattomwit
Автор

Pls make a video about a real game demo which has different classes and entities and improve that save - load system for them. If you do that, you will be have the best Save Load Tutorial in the world!

mutlugameofhalit
Автор

Excellent topic. I've been playing with saving and loading in my project and it's a mess. I read not to use binary formatter but there weren't any replacements suggested. it's nice to see how it can be done safe and clean.

poobagification
Автор

Thank you so much! I built a massive rpg world and have the story 90% done and this was up on my list, so thank you!

lucasdahl
Автор

Finally someone made video on this thnx.
hi,
Kindly make more videos of unity challenges n other projects/tutorials for beginners
Thanks

harshjadon
Автор

Thank you, I always come back to this video to remember how to save data is done!

Francisco-Gutierrez
Автор

Brilliant video that helped me set up a save system that has felt so daunting a task I've put it off for a long time (as a hobby coder).. Thank you!

benrobinson
Автор

Thank you !! Working perfectly with my game, so much easier than my playerPref method, no conversion needed can remove so much code from older method :)

sqwert
Автор

Good tutorial, thanks! However, I would make a backup of the previous save file and delete it only at the end when the new save file has been successfully created. That way, you can be sure that you won't lose the entire save file if the game or the PC crashes / an error occurs during the save.

martin.m.kloeckener
Автор

so how do you save complex data?
I have a class(inventory item) with image, transform, an enum, a scriptable object and another gameobject as variable.
i want to save it and unity is having a hard time converting that into data.
do I have to manually break down the data into primitive variables and save them?

superdahoho
Автор

I'm getting a NullRefrenceException whenever I try to call the Load method :/

Coolae
Автор

thanks for making this. Although I'm going to use BinaryWriter

soolydooly
Автор

Super helpfully and beautifully explained!

umairjamshaid
Автор

I've run into cases where my data structure goes > 10 layers deep, so Serialization fails. In those cases, I've found that I can store some data classes, like "Inventory" or what not, as a JSON string, and then just save the various strings. On Load, I can load all the data in two passes -- loading the saved data, then loading the saved JSON strings.

I'll have to come back after the day job to learn why using JSON Utility is not good! :D

MagicPigGames
Автор

Excellent lecture! Your coding style is clean and professional and teaches good programming habits. I have a hard time tackling Game Serialization and this video gave me some good ideas.

One question if I may.

I'm working on a Baldur's Gate-like game, with lot's of area transitions and after many days of brainstorming I still can't decide what approch to take for bringing the player party to another area with inventory and equipped armor/weapons. I'm going back and forth between DontDestroyOnLoad and using a persistent scene with additive scenes for each area, but both ways feel wrong and hacky. I seriously would prefer to save everything in a scene, destroy it and reload everything in the next.

May I ask you and others, what approach you think would be best or which approach would be horrible to take?

erfrid
Автор

Hey there! Let me start out by saying AMAZING TUTORIAL! I love the way this system works and how clean and reusable the code is. I do have an issue though. While the file is being written to correctly, the data is not being retrieved correctly.

For example. Let's say I have an array of weapon classes that I'm saving. The file gets written to as show below:

{
"Setup": {
"ID": "WeaponClassSlot.1",
"DisplayName": "a;sdkfja;sdlkfja;sdjf",
"PrimaryWeapon": {
"ID": "TestID",
"DisplayName": "TestDisplayName",
"WeaponRef": null,
"WeaponSkins": [],
"WeaponAttachments": []
},
"SecondaryWeapon": {
"ID": "",
"DisplayName": "",
"WeaponRef": null,
"WeaponSkins": [],
"WeaponAttachments": []
},
"Knife": {},
"Utility1": null,
"Utility2": null,
"Utility3": null
},
"name": "WeaponClass_Slot1",
"hideFlags": 0
},
{
"Setup": {
"ID": "WeaponClassSlot.2",
"DisplayName": "Weapon Class 2",
"PrimaryWeapon": {
"ID": "",
"DisplayName": "",
"WeaponRef": null,
"WeaponSkins": [],
"WeaponAttachments": []
},
"SecondaryWeapon": {
"ID": "",
"DisplayName": "",
"WeaponRef": null,
"WeaponSkins": [],
"WeaponAttachments": []
},
"Knife": {},
"Utility1": null,
"Utility2": null,
"Utility3": null
},
etc...

However, when I use the load function and attempt to print the file to the console (similar to how you do with the text field) I get empty values like this:
Any Idea on how I can fix this? Perhaps saving an array of a custom data set is not supported? But then that wouldn't make sense because the file gets correctly written to. It's only when trying to load the data back into an array of this data type that it returns all values as null. Thank you in advanced, and thank you for the video :)

Loaded from file:
[
{
"Setup": {
"ID": null,
"DisplayName": null,
"PrimaryWeapon": null,
"SecondaryWeapon": null,
"Knife": null,
"Utility1": null,
"Utility2": null,
"Utility3": null
},
"name": "WeaponClass_Slot1",
"hideFlags": 0
},

{
"Setup": {
"ID": null,
"DisplayName": null,
"PrimaryWeapon": null,
"SecondaryWeapon": null,
"Knife": null,
"Utility1": null,
"Utility2": null,
"Utility3": null
},
"name": "WeaponClass_Slot2",
"hideFlags": 0
},
etc...

NaviYT
Автор

Would it be safer to write a second save file if you find a save file already exists? Then when the file is written successfully, you delete the old save? That way if anything goes wrong during the save it can gracefully revert to the old save?

evanlane
Автор

Great Videos! I am learning more every day. You mentioned that you shouldn't use the binary formater. Can you explain why?

bravo