Anyone has an example of a correct serializing / deserializing so the server update things correctly without deleting them on restart? thank you!
 
There are several articles written about it. Basically, it's a first in, first out thing.

When you Serialize, you write the variables to save them in a particular order:

C#:
writer.Write (2);  // This is the version number. Perhaps we just changed this from 1 to 2. You will see why shortly

// Let's say these varialbes were added later.
writer.Write( someVariableThree ); // pretend variable three is a bool
writer.Write( someVariableFour ); // pretend variable four is a double

// Let's also say these were the first variables we created when the script was first written
writer.Write( someVariableOne ); // variable one is an int
writer.Write( someVariableTwo ); // and variable two is another bool


Now let's Deserialize:

C#:
int version = reader.ReadInt(); // we wrote an int to memory first, so we have to read one first. In this case, the version number

switch( version )
{
    case 2: // This is the case that will run when the server loads after saving the (2) from above
    {
        someVariableThree = reader.ReadBool(); // the compiler is expecting a bool, so we give it the same one we saved first before
        someVariableFour = reader.ReadDouble(); // you see the pattern here?
        goto case 1;
    }
    // If there is a previous version of the server saved where a (1) was written, it would not contain the variables three
    // and four so it would start here. In this case it would only read these.
    case 1:
    {
        someVariableOne = reader.ReadInt();
        someVariableTwo = reader.ReadBool();
        break;
    }
    case 0: // This may have been the first iteration of the server, a long time ago
    {
        break;
    }
}
 
Back