Forum rules - please read before posting.

Change variables in saves

edited July 2018 in Technical Q&A
Hi there, 
Is there any simple way to create a patch that will change a certain variable from all the saves?

What I am thinking about is to have a script that will run in the MainMenu and if we have the path to set a certain variable to true for all the saves, however if the patch is not there to set every variable to false.  

Thank you

Comments

  • What's the actual purpose behind the need?  It might be there's a better way to achieve it - if a Global Variable is linked to Options Data, it's value will be independent of save files and become a per-profile value instead.

    But to answer the original question: not easily, but possible through a fairly comprehensive custom script.  You'd have to first iterate through the found save files, load up each in turn and extract the de-serialized variable data:

    KickStarter.saveSystem.GatherSaveFiles ();
    foreach (SaveFile saveFile in KickStarter.saveSystem.foundSaveFiles)
    {
        if (File.Exists (saveFile.fileName))
        {
            StreamReader r = File.OpenText (saveFile.fileName);
           
            string _info = r.ReadToEnd ();
            r.Close ();
            _data = _info;

            int divider = saveFileContents.IndexOf ("||");
            string mainData = saveFileContents.Substring (0, divider);
            string roomData = saveFileContents.Substring (divider + 2);
            SaveData saveData = (SaveData) Serializer.DeserializeObject <SaveData> (mainData);

            Debug.Log ("Saved variables data: " + saveData.mainData.variablesData);
        }
    }

    You can then make the necessary change to variablesData (see the SaveSystem script's AssignVariables method on how this string is extracted normally), and then update the original save file:

    string mainData = Serializer.SerializeObject <SaveData> (saveData, true);
    string allData = mainData + "||" + roomData;

    SaveSystem.SaveFileHandler.Save (saveFile, allData);

    Untested, but that'd be the general method.
  • edited July 2018
    Well to put it simple. I'm giving the game to different types of supporters. Some of them pay more than the others. What I need is a file for each type of supporter. If someone pays 1$ he'll have the base game, if someone else will pay 5$ then he'll get one more quest for example. And in my scene I would like to test my variable and if (support == 5) then he'll get one more thing to do there.
    So what I need is a simple file that placed inside the game folder will make the difference between someone who paid 5$ and someone who paid 1$
    That's the main idea.
  • OK - but is this to apply to the game itself or pre-existing save files?  As in, they already have the game, made saves, and a patch should unlock new things?

    It may be that linking the variable to Options Data is the way to go.  That stores the variable's value in PlayerPrefs and outside of any single save file.
  • Yes that's the case. Also I got one more question to ask you.

    I'm not sure if it's a thing I did or it's the way AC is created. I created a first episode for my game, made the release and then I came with a second one(added new variables, made new quests etc), however some of the players told me that they had problems if they continue with a previous save. The problems that they were describing sounded like some new variables where not set. So my question is. "Do you think it's the AC, maybe the system does not allow me to create new things and then continue, or I did something wrong with my variables? Maybe I didn't set them correctly. Should I do a script like the one you showed me above to set all the new variables on default values for each previous save?w
  • If you create a new variable after a save is recorded, then that variable's value won't be altered upon loading that save - because there's no recorded data for it.

    What you can do, however, is hook into the OnBeforeLoading custom event to set the values of these variables to whatever default value you want.  If you accidentally set the value of a variable included in a save, you should be OK because it'd then be overwritten by the save data itself.
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.