Advertisement

how to serialize so many variables?

Started by July 04, 2015 05:09 AM
1 comment, last by Forenkazan 9 years, 6 months ago

Hello

currently i am learning C# serialization to save and load Player Data.

following this tutorial:

http://gamedevelopment.tutsplus.com/tutorials/how-to-save-and-load-your-players-progress-in-unity--cms-20934

i know how to save and load variables.

but lets say i have such class:


[System.Serializable]
public class PlayerData {

	public static  int PlayerLevel;
	public static  string PlayerName;
	public static  string PlayerClass;
	public static  int PlayerHealth;
	public static  int PlayerExp;
	public static  int PlayerAttack;
        public static  int PlayerStrength;
	public static  int PlayerAgility;
	public static  int PlayerAttack;
        public static  List<Item> Inventory = new List<Item>;
        public static  int PlayerMana;

        // A lot of variables ....

}

so my questions:

1- how can i save all of them together instead of one by one

2- is saving all of them when only one of them is changed slows the game?

Hi there,

First and foremost, based on the tutorial, you are using the (System.Runtime.Serialization.Formatters.Binary.)BinaryFormatter, and using binary serialization is actually pretty fast and really compact!
Second, if you do not have a 100 PlayerData instances or 10000 variables in the class (which would be nuts blink.png!), I don't think that serialization will bug your framerate down...

To be a bit more precise, and to answer your questions precisely:

1- how can i save all of them together instead of one by one

I don't really understand your question here. When you serialize an object you serialize all of it's fields. I guess maybe you are using serialization the wrong way unsure.png? As I see all your fields (at least the ones you copied into the post) are marked static. If you would like to serialize all the fields of an instance of a class simply serialize that instance, instead of the fields one-by-one. If you need one static instance of the PlayerData class consider this modification:


[System.Serializable]
public class PlayerData {
    public static PlayerData Instance = new PlayerData(); // <- this is your static instance, you can serialize this instance!
    
    public int PlayerLevel;
    public string PlayerName;
    public string PlayerClass;
    public int PlayerHealth;
    public int PlayerExp;
    public int PlayerAttack;
    public int PlayerStrength;
    public int PlayerAgility;
    public int PlayerAttack;
    public List<Item> Inventory = new List<Item>;
    public int PlayerMana;

    // A lot of variables ....
}

// use the static instance for keeping track of your player data, e.g.:
public void LevelUp() {
    PlayerData.Instance.PlayerLevel++;
    // give her some extra health and attributes, etc...
}

// And when serializing use this instance:
public void Save() {
    BinaryFormatter bf = new BinaryFormatter();
    FileStream file = File.Create (Application.persistentDataPath + "/savedGames.gd");
    bf.Serialize(file, PlayerData.Instance); // here the whole PlayerData object is serialized!!!
    file.Close();
}

2- is saving all of them when only one of them is changed slows the game?

As I mentioned before: no; one file and a couple of dozen fields will be practically nothing. It is an important question tough, that how frequently you save the game ?
Still, if you save every frame I think that will not make your game crawl.


If you still run into some performance problems and you can verify that saving your game does slow it down (quick check: disable saving and check if performance issues are gone), you can try some tricks to optimize it, but only if saving the game is actually a performance bottleneck!

Some ideas you should consider:
- Maybe only save once a couple of seconds using a timer:
I don't know your games requirement but some data changes a lot (e.g.: health), saving each change is a lot, although as mentioned saving once a frame may be perfectly acceptable, but an auto-save every 1-2-5-10 seconds could also fit a game well and would lower the number of times the game is saved...
Also if your game can get by with a save mechanism requiring user action (pressing a save-game button) than all your save related performance problems are solved for life ;)
- Filter out unnecessary save games, by using a flag (bool) indicating whether save is required, set this flag when you modify player data, and only save if this flag is set.
- Split your PlayerData if it becomes humongous smile.png. As a simple example split the class and put inventory into another class: PlayerInventory. Inventory changes much less frequently.

Also huge reference for C# + Serialization (keep in mind this is .net, but the info translates well to mono/unity):
https://msdn.microsoft.com/en-us/library/7ay27kt9%28v=vs.110%29.aspx

wink.png

Br.


Blog | Overburdened | KREEP | Memorynth | @blindmessiah777 Magic Item Tech+30% Enhanced GameDev

Advertisement

@Spidi

Thanks man, very nice job :)

Good luck :)

This topic is closed to new replies.

Advertisement