Monster shadow

VIDEO

SAVE and; LOAD IN UNITY - Manage your data

Description:
Allowing players to save their progress and settings is vital to a great user experience. In this video, I'll show you how to save your custom data and also add some additional tamper-proof security. Script: https://bit.ly/3kZjyDU
ADD A COMMENT

Don Johnson
Unauthorized access exception: access to the path error, how do I fix? What have I done wrong?
Don Johnson
The code is ok now but how wud I call the save from another script in which I have the values updating
Don Johnson
I am lost a line 46
Don Johnson
Why did you expose the variables in the savedata class, couldn't you private it an it work the same?
Tarodev
Not if you want to access them externally, which is exactly what you'd want with a save file :P You could expose them via getters if you'd prefer.
CrashBamBoom !
Thank you.
322ss
I thought I wrote a comment here about security issues related to BinaryFormatter, but it seems to be gone now?
322ss
There is a warning box on that page too with a message "The BinaryFormatter type is dangerous and is not recommended for data processing".
322ss
@Tarodev Search for BinaryFormatter security guide, and you can see what Microsoft has written about it.
Tarodev
I saw the notification for that and when I went to find it, it vanished. I saw you said something about Microsoft deprecating it?
srisrid multi purpose channel
Thanks this helped me a lot
Tarodev
Good luck with your game 👍
Not a Game Dev
Very informative video. You need thousands of viewers your content is so good. Hope you will.
SEE ALL COMMENTS


Transcript:

hello and welcome to tarativ today i'll be showing you how to
save your uh game data that could be settings or configs or
for example what uh characters the player's unlocked what character the
player is currently using like volume and all that stuff
so the way i do it is by a save manager so let's create a new chart script and
call it save manager and also we'll create i
i always have a manager's root object but you can you can put it anywhere
so i'll just make another object under that a child and i'll just
chuck that script on there now let's open it up
now first to save something you'll need something to save so what i generally do
is i'll have a class and i'll call it save data or
something like that and as we're serializing it and saving
it we'll need to mark it with the serializable attribute
make sure you have not picked the serialized field attribute as that's a
unity attribute and it's unrelated to what we need
so serializable and in here i'll have stuff
like let's say inch what level is the player out maybe a bull
for the sound if they've got sound on a bull for their music and let's say one
for the if they have vibrate turned on if
it's a mobile device and then let's say we've got a series
of like you've got character type so say you've got like a mage and archer or
what have you so let's say we've got um character type enum
and this will have an archer a warrior say a magician
or a mage and then in here we'll say public character type
current character and then also you might have
uh these might be unlockables for example you might have like 20 of these
in there and they're unlockable so to keep track of what characters the
player has actually unlocked you could have a list
of character types and this could be your unlocked
characters whoops like that and that should be enough for
now so in our save manager let's always keep a
public static save data reference and this will be what you refer to
throughout your game to find out for example what
has he got the volume on or what character is he currently using
etc next we'll be needing the save path where we're going to be actually storing
this data so that will be save path and then we'll
return a string which will be application dot
persistent data path so this will vary depending on the
operating system and unity will handle that for us
and then in that folder we will name it save
and then generally the extension you can put whatever you want
you can also put no extension i like to actually name the extension after
whatever the game is so for now i'll just call it game all right
so let's create the load and save methods public
void save so first what we're going to do is we're
going to convert our save data into a json object so we can use
unity's json utility to do that to json and just put in our save data like that
all right next we're actually wanting to write this to disk
so let's use a using statement and we'll say
we'll create a new file stream equals whoops new
file stream and the first argument will be
where on the disk we're actually wanting this file stream to be targeting
and then uh file mode will be creates okay now to actually
convert this into bytes to put it into our file we will need
a binary formatter and let's just call this formatter it
goes new binary formatter and i made this a global variable as we're going to
be using it also in the load okay so then here we'll say formatter
serialize and then let's take it put in our stream
and then we need the actual data so let's put in our json object
like that and just with that you can save data all right next let's create a
way to load our data which we'll be doing on startup
generally so first we will use a new stream so bar
stream equals new file stream and put in our save path and then this
one will be file mode open okay
let's grab our serialized data so let's say
uh back to the formatter deserialize and then put in our stream
next let's directly assign our save data to
json utility from json and use the generic method so we can pass in
save data and pass in the data here and this is after a string so we can
actually cast this directly to a string like that
and there we go that's as simple as we need it to load
all right so now that we've got the load method what happens if
you go to load your saved data at the very start of the game
but this is the first boot of your game and there's no
save file there well we should seed the initial save so what we can do
is we can check file exists and put in our save path
so basically we're saying if this file does not exist
well in that case let's say let's uh seed the save so
save data equals new save data and let's say they start on level one
let's say music is set to true sound set to true
vibrate true uh let's say uh their starting character let's say
everybody starts as the archer and then the unlocked characters will
make that a new list and it will have obviously archer in it
as he as that is the starting character and then we can say save
like that so now there will always be a saved game ready to go
as soon as we get down here all right so now it would be good if we
could delete the save game so let's make a static uh function and
call this delete save
and then we'll what we'll do is we'll say if the
file exists and then we'll send in our path and as this is a static function we
can't just access a non-static variable so let's turn this into static
and that could also be private as we're only locally accessing it
and so now save path there so if it exists
then let's go file delete save path and to conveniently access
this let's make it a menu item and call it from the developer menu and
then delete save game so lastly let's add a little
bit of tamper protection to our save game
uh you don't want people going in and then editing the same game maybe adding
additional characters or changing their current character or upping their level
or what have you so uh like any good developer i
stole a little function from stack overflow
there's no passphrase or anything you just put in a plain text string
it will give you back a ciphered string put in the ciphered string it will give
you back the plain text string simple as that but probably good enough
to deter 99 of people that are wanting to tamper
with your game okay so encrypt decrypt
so what we can do now when we save our game
instead of directly saving the json we could
encrypt decrypt but in the json like that so that is now saving an
encrypted json string and then when we load we can simply
right here right here we can encrypt decrypts data
all right so just to show you it all put together i am going to create a start
function and on start i'm going to load
so it's going to come down here it's going to check if our save
file exists if it doesn't it's going to save the original one and then it's
going to load it so i'll also put a break point
here just so i can grab the save path go into the game press start
yep let's hit our breakpoint and i'll just copy
this folder here let's go to it nothing there so we'll
step through it's seating our original one
there we go it's turning into json so that there is what you would uh
normally see in the save file but as we are encrypting it
i will show you what it does look like let's open it up in
a text editor and it's saying it it it's not really
supported to view this binary data but would you like
to open it anyway i will and that is what people will see once
they try to dive in and edit their saved save data
so it's light protection but i would say that it works for 99 of
cases so to delete your saved game you can
just click the developer menu that you created and click delete saved game that
has indeed deleted our save game so if that taught you anything give it a
thumbs up and subscribe and i'll see you for the next tutorial
you