I can't understand Serialization, saving, loading, i already have some code written, but only now i looked into Serialization... Do i have to rewrite all of my code just to support saving?

First of all, i am a pretty much a newbie when it comes to Unity’s coding and other stuff like that, and this is the first time me ever coming to the answers section, so sorry in advance if i didnt ask something right or wrong. I’ve been making a little 2D RPG game to mostly learn about unity and everything like that. Let’s say, i already have wrote a lot of “code”, that contains values, strings, changing text, and etc, etc.

But i wrote it all without saving in mind, so of course, later i am trying to take a look at how to save/load content, and then i see, that in every single tutorial for save/load systems, people write the values into that code itself, with the [Serializeable] within it, so that of course, confused me a lot.

And now i am wondering, do i have to rewrite everything i wrote, just so my game can support loading/saving?

Let’s just take this for example… I have this:

public class TA_Player_Health_and_stuff : MonoBehaviour {

    public TA_Player_Scripts playerScripts;

    int playerHealth = 100;
    int playerHealthLimit = 100;
    int playerHunger = 0;
    int playerThirst = 0;

    int playerXp = 0;
    int playerXpNextLvl = 50;

    int playerLevel = 1;
    int playerNextLevel = 2;

    int Strenght = 0;
    int Sight = 0;
    int Speed = 0;
    int Intelligence = 0;
    int Magic = 0;
    int Charisma = 0;
    int Luck = 0;
    int Defense = 0;
}

So i have all these values, that i obviously have to save in a game file, but i still can’t seem to understand, how it’s all done at all, do i have to write it all (Every single value) into a different script with [serializeable] in it? It’ll break a lot of stuff in my scene, and it would be rather upsetting if i’ll have to rewrite everything from the ground up just to make saving and loading working…

Pardon me for bad english, and thanks for your time.

A little edit: Keep in mind, that i have a lot more code with other values as well, i would need to save them somehow too, but i just can’t seem to understand it at all

In the .sln file you can name the assmeblies. if you inspect the .sln file you will find a line like this Project("{E097FAD1-6243-4DAD-9C02-E9B9EFC3FFC1}") = "Assembly-CSharp", "Assembly-CSharp.csproj", "{1AF10902-9781-58C1-C1BA-8EC10E1CFC9A}" Where is a simple map of name - file. Both can be anything else.

5 Answers

5

there is no automated way to save data. You have to gather and save the information your game needs through code and write it yourself. the answer kind of depends on what platform you are building for. if you are making games for pc you can simply write to the user’s hard drive. if you are making a game to be played in a web browser there are limitations by the firewall and so you would send infomation to a server for storage or possibly check into user prefabs.

here is a code example i wrote preiviosly for writing to a hard drive with PC:

http://answers.unity3d.com/questions/1316985/unity-55-save-user-created-levels-to-folder-please.html

To put it in a nutshell - no, you do not need to re-write this entire class.

If I’ve understood the question correctly, along with the comment you left only 20 minutes ago as of me starting to write this, then this should be of some use to you:

https://docs.unity3d.com/Manual/JSONSerialization.html

The concept of serialisation as I’m sure you’re aware is to get in-code object instances to some external source of some description with all the values saved at that point, SQL servers, text files or otherwise.

MonoBehaviour objects cannot be serialised by throwing it into the traditional .NET serialiser classes as MonoBehaviour does not support IEnumerable, so this leaves you with two options:

1 - use the aforementioned link and adjust your loading stuff accordingly.

2 - write your own serialisation.

Funnily enough for a recent project I actually ended up doing the latter, however im not sure how far your "new"ness goes, so I’d recommend the former if you’re not comfortable with it.

I hope this helps :slight_smile:

Ah yea is true! it only gives positive angles from 1 to 180. I had to create a system to know if was to right or to left. By creating the vector between the two original vectors, i can know in wich direction is (top-right, top-left, bot-right or bot-left) OriginalVector1-OriginalVector2 = DiferenceVector. If DiferenceVector.x >0 means is top, if < 0 means is bot If DiferenceVector.y >0 means is right, if <0 means is left. Thats what i did :D

Hello, @Kirillian

If you would like to create your own save/load system, it’s a pretty much yes. But it should not confuse you that much, and it shouldn’t be hard in this case. You just need to take all your variables that you want to save in a base class and create / reference it when you need. In your case :

 public class PlayerData{
     int playerHealth = 100;
     int playerHealthLimit = 100;
     int playerHunger = 0;
     int playerThirst = 0;
 
     int playerXp = 0;
     int playerXpNextLvl = 50;
 
     int playerLevel = 1;
     int playerNextLevel = 2;
 
     int Strenght = 0;
     int Sight = 0;
     int Speed = 0;
     int Intelligence = 0;
     int Magic = 0;
     int Charisma = 0;
     int Luck = 0;
     int Defense = 0;
 }

Then

 public class TA_Player_Health_and_stuff : MonoBehaviour {
 
     public TA_Player_Scripts playerScripts;
     public PlayerData playerData;

void DamagePlayer(int damage)
{
playerData.playerHealth -= damage;
}
 }

Of course this is not right way to damage player but I gave you an example on how to do it. Then you mark PlayerData class as Serializable and serialize/deserialize it. This will bring a lot of problems you will not be able to serialize somethings like Color and need to write a custom Color variable/property. Other than that you can use assets like Easy Save which will save you from bunch of head-hurting stuff. Choice is yours.

Do you happen to know why it goes to "Assembly-CSharp" instead of the name of the project like the picture I posted above? IE Class Library the same name as Project Name. Also are they the same technically? I see that the icons are different.

@AlwaysSunny , my apologies, when I verified my account here, my post didn’t seem to show up, so I simply assumed that it didn’t pass the moderator check, so I decided to dig deeper and have already learned how to serialize some files, well, entities and etc.

Basically, I have created a script, that has different ints and other variables to save, and upon player saving, I simply make it so that main variables get saved into serialized variables, and upon loading from the file, the game loads them back from other serialized variables, I do realize that perhaps its not the best method, but so far it worked without any flaw to me. I apologize for wasting anyone’s time who have replied there, but I assumed my post just didn’t show up here, it kept redirecting me to the main page instead, so. My bad. I appreciate the help though!

Gave a bigger insight into dynamic worlds, and yeah, as for the reason why it is this way, is because my little RPG game that I am using for studying, is indeed in a little open world, so getting to know more is never a bad thing, again, I appreciate the help a lot, but unfortunately I already found a solution, thanks for your time again.

Also apologize for bad formatting, currently posting this off mobile phone

Because names must be Unique, so Unity create different names for each .csproj. If they collide then when building an error will be throw.

I apologize everyone, i have already answered here under @AlwaysSunny 's commentary, it includes the explanation, sorry for wasting everyone’s time, i appreciate the help however!