Jump to content

VesselModules, PartResources and save sequences


Recommended Posts

I have a VesselModule that makes some changes to one or more PartResources present on a vessel in response to some changes. I want these changes to be reverted before the game is saved so that those changes don't make it into the persistence file. 

I can't quite work out how to do this.

In my current attempt, ideally this function should fire before the save sequence starts. It really just reverts the amount and maxAmount of a PartResource to their original values that the VesselModule is storing.

protected void ClearBufferStorage()
        {
           Debug.Log("Trying to clear buffer storage");
          if (bufferStorage != null)
          {
              bufferStorage.amount = (double)Mathf.Clamp((float)bufferStorage.amount, 0f, (float)(originalMax));
            bufferStorage.maxAmount = originalMax;

            Debug.Log(String.Format("{0}, {1}", bufferStorage.amount, bufferStorage.maxAmount));
            
          }
        }

I've tried to do this by overriding the OnSave method in VesselModule, simple enough.

        protected override void OnSave(ConfigNode node)
        {
            ClearBufferStorage();
            base.OnSave(node);
        }

Based on my logging, this should work. The log shows the game being paused, then the ClearBufferStorage() being called (the log calls to the PartResource showing the correct 270, 270 that should be preesent), then the save occurring after the storage has been cleared

[LOG 08:52:15.008] Game Paused!
[LOG 08:52:16.558] Trying to clear buffer storage
[LOG 08:52:16.558] 270, 270
[LOG 08:52:16.558] Flight State Captured
[LOG 08:52:16.559] Saving Achievements Tree...
[LOG 08:52:16.559] [MessageSystem] Save Messages
[LOG 08:52:16.560] Trying to clear buffer storage
[LOG 08:52:16.562] 270, 270
[LOG 08:52:16.568] Game State Saved to saves/default/persistent

However, when I inspect the persistence file or reload the game, it turns out that the resource has not been reverted to 270, 270 correctly. 

Am I not understanding the saving sequence correctly here? This seems like it should work.

 

Edited by Nertea
Link to comment
Share on other sites

I think I found the answer to this. At this stage, updating the amount and maxAmount fields of the PartResource doesn't correctly serialize to the associated ProtoPartResource. I guess that would happen next tick.

Directly setting the fields of the ProtoPartResource appears to successfully reverts the changes. 

Edited by Nertea
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...