Jump to content

KSPField isPersistant doesn't appear to be working


Recommended Posts

I am trying to save 'lowPowerMode' so that if I load the craft it will be whatever it was when I was last flying that vessel but it always seems to reset to false when I re-load it.  

Here is the code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProbeLowPower
{
    public class ProbeLowPower : PartModule
    {
        [KSPField(isPersistant = true)]
        public bool lowPowerMode;
        [KSPField(isPersistant = false)]
        public float lowPowerRatio;
        public double normalRate;
        public double lowPowerRate;
        public ModuleResource moduleResource;

        public override void OnAwake()
        {
            if (HighLogic.LoadedSceneIsFlight)
            {
                foreach (ModuleResource modres in this.part.GetComponent<ModuleCommand>().inputResources)
                {
                    if (modres.name == "ElectricCharge")
                    {
                        moduleResource = modres;
                        normalRate = modres.rate;
                        lowPowerRate = normalRate * lowPowerRatio;
                    }
                }

                print("AWAKE, Low Power Mode: " + lowPowerMode);
                if (lowPowerMode)
                    moduleResource.rate = lowPowerRate;
                else
                    moduleResource.rate = normalRate;

            }         
        }

        [KSPEvent(active = true, guiActive = true, guiActiveEditor = false, guiActiveUnfocused = false, guiName = "Low Power Mode: Disabled")]
        public void activateLPM()
        {
            lowPowerMode = true;
            moduleResource.rate = lowPowerRate;
            Events["activateLPM"].active = false;
            Events["deactivateLPM"].active = true;
        }

        [KSPEvent(active = false, guiActive = true, guiActiveEditor = false, guiActiveUnfocused = false, guiName = "Low Power Mode: Enabled")]
        public void deactivateLPM()
        {
            lowPowerMode = false;
            moduleResource.rate = normalRate;
            Events["activateLPM"].active = true;
            Events["deactivateLPM"].active = false;
        }
    }
}

 

Link to comment
Share on other sites

Can you add a Debug.Log(lowPowerMode); line to the Update method?

I think your issue is that OnAwake only runs once when the module is created and so always runs before OnLoad does (which is when KSPFields load data from the save file). If this is the case, things are working correctly, just your Debug.Log line will always say FALSE as it runs before the KSPField can load its state from the save file.

D.

Link to comment
Share on other sites

14 minutes ago, Diazo said:

Can you add a Debug.Log(lowPowerMode); line to the Update method?

I think your issue is that OnAwake only runs once when the module is created and so always runs before OnLoad does (which is when KSPFields load data from the save file). If this is the case, things are working correctly, just your Debug.Log line will always say FALSE as it runs before the KSPField can load its state from the save file.

D.

I know that it's not just the debug log because it still uses the normal rate, rather than the low power rate but would it work if I put it in OnLoad as this is where it toggles the low power mode based on whether or not the variable is true/false?

Edited by JoePatrick1
Link to comment
Share on other sites

The entire point of the KSPField being persistent is that you don't have to use the OnSave/OnLoad methods to save/load data, it's done by the KSPField for you.

My suspicion is that it is still an order of operations issue between the different methods. Can you comment out the OnAwake method and see if LowPowerMode saves?

D.

Link to comment
Share on other sites

1 minute ago, Diazo said:

The entire point of the KSPField being persistent is that you don't have to use the OnSave/OnLoad methods to save/load data, it's done by the KSPField for you.

My suspicion is that it is still an order of operations issue between the different methods. Can you comment out the OnAwake method and see if LowPowerMode saves?

D.

I just tried changing onAwake to onLoad and now it works perfectly. The state is saved with isPersistent but I cannot save the rate of power drain as this is saved in the part files and is loaded reloaded by the stock modules each time. I have to load the state, then set the rate accordingly

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...