Project

General

Profile

Feature #2414

Improvements to the way modules are loaded.

Added by swamp_ig almost 10 years ago. Updated almost 10 years ago.

Status:
New
Severity:
Normal
Assignee:
-
Category:
Parts
Target version:
-
% Done:

0%

Platform:
Any
Expansion:
Mod Related:
No
Votes:
Arrow u r green
Arrow d r red

Description

It would be really great if the way modules were loaded in the game were a bit more robust.

Currently modules are loaded from the save files and saved ships in strict order of definition as in the part config. This presents a problem if you want to add in extra modules when you're doing any modding, especially when those modules have tweakable controls in them because the order of the modules determines the order the controls appear. It will also cause stock KSP issues if you ever want to add new modules to an existing part, especially if they are tweakable.

What I might suggest is to change Part.LoadPart(ConfigNode node, int moduleIndex) to ignore the index, pull the name field out of the node, and just use PartModuleList[string] with the name instead of using PartModuleList[int]. This would be way more robust.

The other modification I might request will require a small amount of extra work, but would enable having more than one copy of the same PartModule in a part, plus the ability to switch out a different PartModule class if you needed / wanted to for modding purposes.

The way to do it would be to have an optional 'class' attribute for MODULE definitions that can be used to define what class you want to use. If this is defined then the module name is just used as a name and nothing else.

The easiest place to do this is in Part.AddModule(ConfigNode), plus you'd also need to modify PartModule.Load to store the new name in a field, and also alter PartModuleList[string] to use the name attribute.

Incidentally, there's a potential and hard-to-track down bug in PartModuleList[string] which I'll report separately.

History

#1 Updated by swamp_ig almost 10 years ago

change Part.LoadPart(ConfigNode node, int moduleIndex) should read Part.LoadModule(ConfigNode node, int moduleIndex).

Here's the fixed code:

public PartModule LoadModule(ConfigNode node, int moduleIndex)
{
    if(!node.HasValue("name")) 
    {
        Debug.LogWarning("In part " + base.name + " cannot load unnamed module from save file", gameObject);
        return null;
    }

    string moduleName = node.GetValue("name");

    if(!Modules.Contains(moduleName))
    {
        Debug.LogWarning("In part " + base.name + " module " + moduleName + " does not exist so cannot be loaded from the save file", gameObject);
        return null;
    }

    PartModule partModule = this.Modules[name];
    partModule.Load(node);
    return partModule;
}

#2 Updated by hermes47 almost 10 years ago

  • Tracker changed from Bug to Feature

#3 Updated by swamp_ig almost 10 years ago

I'm working on a solid fix for this which will be part of the ModuleManager plugin. I would of course be happy to contribute the code back in the longer term.

Also available in: Atom PDF