Search Unity

Save system: XMLSerialization vs (Non serialized?)XML

Discussion in 'General Discussion' started by Itsproinc, Mar 19, 2018.

  1. Itsproinc

    Itsproinc

    Joined:
    Jan 1, 2013
    Posts:
    1
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Hard to tell what the performance difference would be without testing them, so I'd suggest just doing that and comparing.
     
  3. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,697
    If you're concerned about performance, don't use XML.

    Text-based formats like XML and JSON are relatively inefficient, but they're also very useful for development and debugging.

    Make it modular so you can use a class that serializes to XML during development, and then swap it with a class that serializes to a faster, smaller binary format for release.
     
    Ryiah likes this.
  4. Balthamet

    Balthamet

    Joined:
    Jan 25, 2014
    Posts:
    56
    I wouldn't use XML. If I was going to output to readable text for saving then it is JSON all the way.
     
  5. zoran404

    zoran404

    Joined:
    Jan 11, 2015
    Posts:
    520
    Hey, so long as you're not saving stuff every frame then performance really isn't something you should bother with.
     
  6. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,697
    ^ That's a good point if you don't have a lot of data. Even if you do have a lot of data, the first step is to get saving working at all in the first place. In the end, if you do have a lot of data to save, you'll want it to run fast and/or asynchronously. It looks really bad to freeze the game for a long duration while you're saving or loading.
     
    one_one likes this.
  7. one_one

    one_one

    Joined:
    May 20, 2013
    Posts:
    621
    Have you taken a look at protobuf? In addition to serializing very quickly and producing small (non-readable, whether that's a pro or con really depends on your requirements) files it has a rather comfortable workflow, too. You define data types in a C-like language (in .proto files), a compiler then generates all the C# (and many other languages) code for you. IMO it's especially useful for save games because fields in the data types you define also have integer IDs, which is a big advantage when you intend to add or rename fields in your save game data format (which you usually do over the course of development.)
     
  8. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,037
    I second the JSON and Protobuf suggestions. XML isn't the best at anything but eating space and bandwidth ;)

    JSON has the advantage that you have to try hard to find something which doesn't support it natively, and it's still relatively compact before compression.

    Protobuf has the advantage that it goes hand in hand with gRPC, which lets you generate server stubs and client SDKs from the same files to connect via direct (potentially secure, client certificate-based) TCP and/or a REST API. If you need to handle data from the game in anything external it's just a matter of integrating the generated SDK. And as mentioned above, fields have numeric IDs so you can omit unused data in the structures, which is nice for reducing save sizes early in a game. Most of the boring code is done for you, including streaming chunks of data.
     
    one_one likes this.
  9. N1warhead

    N1warhead

    Joined:
    Mar 12, 2014
    Posts:
    3,884
    I know this isn't entirely related to XML, but you could also check out my MIT licensed Saving and Loading system I created awhile back. Granted, at this moment it's only useful for Instantiated objects (For Dynamic worlds). But shouldn't be too too much editing to make it for a static world.

    https://forum.unity.com/threads/public-open-source-save-loading-parser.474015/

    This way you can skip serialization all together. As this system doesn't require serialization at all.
    I was saving like 5,000+ objects in roughly a second. Saving positions, rotations, object references (Transform and GameObject), variables, etc. Even seemed faster on my phone than my PC, it even offers basic Encryption as well.

    I'm just trying to help give you options, being its MIT you are free to do with it as you please.