Jump to content

Can I prevent a PartModule from saving entirely?


Recommended Posts

Hey everyone,

How can I prevent a PartModule from saving entirely?  Mine are completely stateless, but there can be a lot of them; I'd like to not clutter up saves.

Longer explanation - I originally used ModuleManager to add these modules to ships - but I discovered that EVA Kerbals are immune to ModuleManager.  So I switched to scanning ships in the scene myself, and adding my helper modules to them in OnAwake.  However - on reload of ships that have saved them out, all the mismatched module indices make a lot of warning spam.

As always - any help is appreciated!

Link to comment
Share on other sites

You talk about scanning ships, would VesselModule work?

A class that inherits VesselModule gets a single copy of it added to every vessel in the game so would that work for you? (Remember that a Kerbal on EVA counts as a vessel.)

The big limitation is that VesselModule can not load or save data itself, but as you specifically say you don't need to do this that removes the biggest obstacle to using VesselModule.

D.

Link to comment
Share on other sites

Quote

Do your modules have to be PartModules? What do they do?

I have ModuleScienceHelper part modules, and necessarily so.  I create one whenever I detect existing ModuleScienceContainers or ModuleScienceExperiment modules in parts, and my module targets that.  So every ModuleScienceContainer has a ModuleScienceHelper dealing with it, and similarly for the experiments.  To be clear - my mod is partless; I'm altering existing parts behaviour, not adding new ones.

 

The ModuleScienceHelper modules exist to supress / mess with the target right click events (changing descriptions / visibility), and they also usurp those events with their own versions so I can get a notification of the right click event.  For example: for containers, I'm surpressing the ReviewData KSPEvent in the original module, providing my own identical looking version, and when my version is clicked, it notifies my mod and then passes through the call to the original containers handler method.  I've done it that way because some things in KSP just don't have an event, and I need a notify when those things happen.

 

Quote

You talk about scanning ships, would VesselModule work?

Probably not.  It solves the issue of stuff saving out, but I lose the ability to provide my own KSPEvents etc, which I need.  Furthermore - by using my pairwise helper modules whenever I find parts with my desired targets (ModuleScience[Container/Experiment]), I'm hoping to work with other mods more easily - as long as their crazy new parts have my target part modules, I'll find them, pair up helpers, and all should go smoothly.  Tbh though that's more of a future concern...solely considering working on stock 1.0.5 atm.

 

Let's talk about OnSave.  It appears that the whole contents of my PartModule is present in the ConfigNode passed to me.  Can I literally just erase all of it?  If I can somehow get the parent ConfigNode, erase the child (my partmodules MODULE section), would that save out as if my part module doesn't exist in the ship?  Ideally that's what I'd like - keep my PartModules, and have them never save to disk.

Edited by SixHourDays
Link to comment
Share on other sites

Erm, I suppose you could try something like:

public override void OnSave(ConfigNode node)
{
node.RemoveValues(); //can't remember the exact method, check the object broswer.
node.RemoveNodes();
}

I think OnSave runs after the KSFields save and so would wipe the data?

However, that would still leave an empty MODULE node in the .sfs file. I don't think it would break anything but something to watch for.

I will confirm that if you need to access to KSPEvents you have to use a partModule.

D.

Link to comment
Share on other sites

Another option would be to go back to using MM, and then just have a small VesselModule which just checks if its a Kerbal, adds the module(s) if yes, and then destroys itself

// blah blah, VM declarations...
public void Awake() // Might have to wait until start, can't remember
{
 	Vessel v =  GetComponent<Vessel>();
  if (v.isEVA)
    v.rootPart.AddModules();
  Destroy(this);
}
Edited by Crzyrndm
Link to comment
Share on other sites

Quote

 leave an empty MODULE node in the .sfs file. I don't think it would break anything but something to watch for.

I agree, hence wanting the parent, so I can remove that.

 

@Crzyrndm - I could do that, but I'm left in the same predicament if you leave the flight scene (and thus save) with Kerbals on EVA.  They would save out my helpers still - this kind of duality dealing with Kerbals in one way and ships another was what drove me to attempt a single solution to begin with.

 

Edited by SixHourDays
Link to comment
Share on other sites

5 hours ago, SixHourDays said:

For example: for containers, I'm surpressing the ReviewData KSPEvent in the original module, providing my own identical looking version, and when my version is clicked, it notifies my mod and then passes through the call to the original containers handler method.  I've done it that way because some things in KSP just don't have an event, and I need a notify when those things happen.

If there were an event that allowed you to intercept (and potentially veto) your target KSPEvent(s), would you still need to define your own? That's how I'd handle this. If you never need any more BaseFields than what your target modules implement, you can get away with just hijacking them and avoid being a PartModule altogether

Link to comment
Share on other sites

@xEvilReeperx I am adding my own unique KSPEvents via ModuleScienceHelpers, in addition to the supressing/altering/replacing my targets KSPEvents as I described above :-(  Good suggestion though.

 

Thanks to everyone who's helping out!  We're almost there folks - I just need a way to erase that MODULE configNode from the parent.  Does Part or Vessel have some equivalent to OnSave like PartModule does?  All I need is access to the ConfigNode's above my PartModule....I'd settle for the whole Part, the whole Vessel...anything?

 

(btw I'm still working, I'm replying when I can)

 

 

Edited by SixHourDays
Link to comment
Share on other sites

This is the best solution I could come up with:

[KSPScenario(ScenarioCreationOptions.AddToAllGames, GameScenes.FLIGHT)]
public class ScenarioHideTheNut : ScenarioModule
{
    public override void OnSave(ConfigNode node)
    {
        base.OnSave(node);

        foreach (var pps in HighLogic.CurrentGame.flightState.protoVessels.SelectMany(pv => pv.protoPartSnapshots))
            pps.modules.RemoveAll(
                ppms => (ppms.moduleRef != null && ppms.moduleRef.GetType() == typeof (ModuleNut)) ||
                        ppms.moduleName == typeof (ModuleNut).Name);
    }
}

public class ModuleNut : PartModule
{
    [KSPEvent(guiName = "Hello world", active = true, guiActive = true)]
    public void HelloWorld()
    {
        print("Nut says, \"Hello world!\"");
    }
}


public class AddNutToAllParts : VesselModule
{
    private void Start()
    {
        if (!HighLogic.LoadedSceneIsFlight) return;

        var vessel = GetComponent<Vessel>();

        foreach (var p in vessel.parts)
            if (!p.gameObject.GetComponents<ModuleNut>().Any())
            {
                print("Adding " + typeof (ModuleNut).Name + " to " + p.partInfo.name);
                p.AddModule(typeof (ModuleNut).Name);
            }
    }
}

The ScenarioModule will prevent any PartModule(s) you want from being saved to the ConfigNode without preventing them from working normally otherwise. I used a VesselModule to add the test PartModule to the parts in the flight scene but it should work with MM scripts as well* if one needed to hide those

Edit: *It occurs to me that you'd probably want to make sure your MM patch ran last to avoid those mismatch problems if you weren't adding PartModules on the fly

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