Forum rules - please read before posting.

Decompressing PlayerPrefs String

How do we 'decompress' the stored PlayerPrefs back into the game memory?

For example, my PlayerPrefs string is:

XML<?xml version="1.0" encoding="utf-8"?>01-10:1|1:0|2:0|3:1|4:0|5:1|6:0|7:0|8:1|9:1|10:0|11:1|12:0|13:0|14:1|15:0|16:1|17:1|18:0|19:0|20:0|21:0|22:1|23:0|24:0|25:0|26:0|27:0|28:0|29:0|30:0|31:0|32:0|33:0|34:0|35:0|36:0|37:0|38:0|39:0|41:0|40:0|42:0|43:0|44:0|45:0|46:1|47:0|48:0|49:0|50:1|51:1|52:0|53:0|54:0|55:0|56:0|57:0|58:0|59:0|60:0|61:0|62:0|63:0|64:0|65:0|66:0|67:0|68:0|69:0|70:0|71:0|72:0|73:0|74:0|75:0|76:0|77:0|78:0|79:0|80:0|81:0|82:0|83:1|84:0|85:0|86:1|87:0|88:0|89:0|90:0|91:0|92:0|93:1|94:0|95:0|96:0|97:0|98:0|99:0|100:0|101:0|102:0|103:0|104:1|105:0|106:0|109:027:False|26:False|2:False|17:False|9:False|3:False|21:False|22:True|24:False|28:False|29:False|30:False|31:False|32:False|33:False|34:False|35:False|36:False|37:False|40:False|41:False|42:False|43:False|44:False|45:False|46:False|47:False|48:False|52:False|49:False|38:False|39:False|50:False|51:False|11:False|53:False|54:False|55:False|56:False|57:False|58:False|59:False|60:False|4:False|0:False|1:False|5:False|8:False|7:False|6:False|10:False|12:False|13:False|14:False|15:False|16:False|18:False|19:False

(I've cut a load of the string out but you get the idea.)

Once I've loaded this back up, how to I re-assign all the values back into the game, if that makes sense?

Comments

  • You can call the SaveSystem class's ExtractMainData method, which converts the string into a SaveData class.

    The same script's ReturnMainData function demonstrates how a SaveData instance can be used to apply data back to your game, but AC's provided save system handles this automatically. If desired, a custom file handler can be assigned through script.

  • So my string containing all of the dataToSave data is called ACdata. So am I correct in assuming I can call, from my third-party script:

    string ACdata = Encoding.ASCII.GetString(ACDataByte); //converts the loaded byte back to a string
    SaveSystem.AssignVariables(ACdata, false);
    SaveSystem.ExtractMainData(ACdata);
    PlayerPrefs.SetString("DarkSide_0_0", ACdata);
    AC.SaveSystem.SaveGame(0, false, "");
    

    Am I on the correct path?

  • Afraid not.

    Be sure to check the Scripting Gude's entry for each function:

    AssignVariables needs to have passed the SaveData class's mainData.runtimeVariablesData string.

    ExtractMainData only has any effect if you assign its return value to a SaveData class.

    SaveGame saves the game, but this topic is about loading.

    If ACdata was correct, you should be able to assign variables with:

    SaveData saveData = SaveSystem.ExtractMainData (ACdata);
    SaveSystem.AssignVariables (saveData.mainData.runtimeVariablesData, false);
    
  • I'm getting the dreaded 'Object reference not set to instance of an object' when calling those lines.

  • Upon checking, I get the 'Object reference not set to instance of an object' when using the second line:

    SaveSystem.AssignVariables (saveData.mainData.runtimeVariablesData, false);

    Any ideas how this could be fixed?

  • It depends on the result of the ExtractMainData function. If "ACdata" is not the expected value, it won't be able to generate a SaveData class.

    If ACdata is the string in your first post, then it may be a case of the SaveSystem's assigned FileFormatHandler not matching that of the data, i.e XML.

    Be sure to have AC rely on this beforehand, which can be done with:

    SaveSystem.FileFormatHandler = new FileFormatHandler_Xml ();
    
  • It's already set to XML. Hmmm, I'll do some more testing and see what happens. Thanks Chris, I'm getting closer now :)
  • I think I've worked out what's happening.

    AC converts the string into an XML. The Xbox code then converts that XML to a byte array for the 'data'. Then, when it's reloaded, it's decompressing the ENTIRE byte/XML (the original ACdata string) so there's a Container Name, a Blob Name AND a complete XML file complete with the XML header etc contained within that data. There's essentially 2 XML files, the second one within the 'blob' data.

    Is there anyway to remove AC's XML so the string in PlayerPrefs is raw data (e.g 109:027:False|26:False|2:False|17:False etc) without the opening XML<?xml version="1.0" encoding="utf-8"?> etc?

    Unless there's any other way of doing this.

    I'm probably on the total wrong path anyhow! Ha!

  • Data serialized into XML will add XML tags and headers automatically. You could SubString it manually, but I don't recommend it.

    If XBox is serializing it into an XML of its own, you could try having AC rely on something else e.g. Json or Binary, so that only one XML serialization is involved.

    Unless there's any other way of doing this.

    The use of AC's own save system, with a custom save file handler, is the recommended approach.

  • I've tried binary and JSON but still get the 'Object reference not set to instance of an object' when using

    SaveSystem.AssignVariables (saveData.mainData.runtimeVariablesData, false);

    after the load :neutral:

  • What is the value of ACdata string? That will need to be correct for SaveFile to be non-null?

    Still best to rely on AC's save system, mind.

  • Here's the complete string that's being uploaded and loaded from Xbox Cloud:

    https://jpst.it/33chL

    I'm not sure if it's all correct as it seems to be looking slightly corrupted towards the end?

  • What is the exact error you get?

  • edited December 2022
    I've literally just tried setting the FileFormatHandler to JSON again and now it's suddenly started working?! I'm not complaining! I'll put it down to a cache issue on Microsoft's side. But yep, it's now downloading the save game data from the cloud and applying its settings back into the game. It's only took me 4 months (lol) but with your help Chris I wouldn't have done it, I don't think.

    When the game's released I'll send you a free download key as a thank you :)
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.