post-kerbin-mining-corporation/NearFutureExploration

ModuleAntennaFeed bonus won't get applied on unloaded vessels

gotmachine opened this issue · 2 comments

Note that I didn't test it, just gave a look at your code.

You are patching the target ModuleDataTransmitter antennaPower at runtime but CommNet is designed to work on unloaded vessels too (for relays). This is done with the ICommAntenna interface (implemented by ModuleDataTransmitter) that has specific methods to get power, etc on unloaded vessels.

Unfortunately, ICommAntenna.CommPowerUnloaded() checks antennaPower on the prefab, so any changes to antennaPower on a specific part while loaded will be ignored after being unloaded.
Moreover, antennaPower isn't persistent.

I've not looked too much and I'm writing all this without any testing, but it seems to me that the only solution is to create a ModuleDataTransmitter derivative that re-implements ICommAntenna and hide the CommPowerUnloaded method (it unfortunately isn't virtual), something like :

public class ModuleDataTransmitterFeedeable : ModuleDataTransmitter, ICommAntenna
{
  [KSPField(isPersistant = true)]
  public double persistedAntennaPower;

  public override void OnAwake()
  {
    persistedAntennaPower = antennaPower;
    base.OnAwake();
  }

  new public double CommPowerUnloaded(ProtoPartModuleSnapshot ppm)
  {
    // get the value from the protomodule saved confignode, and copy it to the prefab (could use a local variable, but well...)
    if (ppm != null && ppm.moduleValues.TryGetValue("persistedAntennaPower", ref persistedAntennaPower))
    {
      return persistedAntennaPower;
    }
    else
    {
      return base.CommPowerUnloaded(ppm);
    }
  }
}

Then you will need to set both persistedAntennaPower and antennaPower from ModuleAntennaFeed.ApplyReflectorBonus()

Embarrassed to say I didn't even think of this particular case. I'll look into it asap.

Resolved with 1.0.1