Jump to content

ScenarioModule help needed (solved)


Recommended Posts

My (flawed) understanding of the ScenarioModule was that OnLoad and OnSave was where the data to be saved/loaded could be set.  But, in my recent testing, it doesn't work.

OnSave gets called, but no data gets saved unless it's a KSPField(isPersistant = true)

As a test, I manually added the missing fields to the scenario module in the persistent file after exiting a game and THAT worked.  So, it appears that the OnLoad works, but the OnSave, while it is being called, is not saving the data, is it possible that it's being called too late?

 

See the following module for an example of what doesn't work.

    [KSPScenario(ScenarioCreationOptions.AddToNewCareerGames | ScenarioCreationOptions.AddToExistingCareerGames, GameScenes.SPACECENTER)]
    public class KSPCasherData : ScenarioModule
    {
        public override void OnSave(ConfigNode node)
        {
            Debug.Log("KSPCasherData.OnSave");

            ConfigNode n = new ConfigNode("KSPCasher");
            n.SetValue("LastBudget", KSPCasher.instance.LastBudget);
            n.SetValue("Multiplier", KSPCasher.BudgetMultiplier.ToString());
            n.SetValue("SciBuy", KSPCasher.ScienceBuyMultiplier.ToString());
            n.SetValue("SciSell", KSPCasher.ScienceSellMultiplier.ToString());
            node.AddNode(n);
            base.OnSave(node);
        }

//
// The following did work after manually putting data into the persistent.sfs file
//
        public override void OnLoad(ConfigNode node)
        {
            Debug.Log("KSPCasherData.OnLoad");
            base.OnLoad(node);

            var n = node.GetNode("KSPCasher");

            if (n != null)
            {
                string param = n.GetValue("LastBudget");
                if (param != null)
                    KSPCasher.instance.LastBudget = param;

                param = n.GetValue("Multiplier");
                if (param != null)
                    KSPCasher.BudgetMultiplier = double.Parse(param);

                param = n.GetValue("SciBuy");
                if (param != null)
                    KSPCasher.ScienceBuyMultiplier = double.Parse(param);

                param = n.GetValue("SciSell");
                if (param != null)
                    KSPCasher.ScienceSellMultiplier = double.Parse(param);
            }
        }
    }

 

Edit:  The problem was that SetValue was being used instead of AddValue

Edited by linuxgurugamer
Link to comment
Share on other sites

17 minutes ago, linuxgurugamer said:

My (flawed) understanding of the ScenarioModule was that OnLoad and OnSave was where the data to be saved/loaded could be set.  But, in my recent testing, it doesn't work.

OnSave gets called, but no data gets saved unless it's a KSPField(isPersistant = true)

Try and take a look at 

-MM- uses scenario module for persisting global data. I think that will get you going.

https://github.com/mmoench/KRnD/blob/master/Source/KRnD.cs#L1159-L1235

Link to comment
Share on other sites

40 minutes ago, Warezcrawler said:

hmmm... setvalue only work when the value is already present, right? So addvalue would probably be the way to go. :)

Yes.  I'm guessing that it used to work both ways, when the mod was originally written, it worked well.  But with updates, it was broken.

Link to comment
Share on other sites

2 hours ago, linuxgurugamer said:

Yes.  I'm guessing that it used to work both ways, when the mod was originally written, it worked well.  But with updates, it was broken.

Well, based on a peek at the definition of SetValue, I guess you might have had a true as the last argument (createIfNotFound), or it was defaulted to true previously.

public bool SetValue(string name, string newValue, bool createIfNotFound = false);

 

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...