Skip to content

Commit

Permalink
Fix for #115
Browse files Browse the repository at this point in the history
* decouple was called twice in 2 consecutive frames causing the copy
* switched to a update based experiment check
  • Loading branch information
N3h3miah committed Feb 25, 2015
1 parent 522fd7b commit 6153b9e
Showing 1 changed file with 22 additions and 19 deletions.
41 changes: 22 additions & 19 deletions Plugin/NE Science/KEES_PEC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ class KEES_PEC : PartModule
private AttachNode node = null;
private KEESExperiment exp = null;

private int counter = 0;

[KSPField(isPersistant = true)]
public bool decoupled = false;

public override void OnStart(PartModule.StartState state)
{
base.OnStart(state);
Expand All @@ -43,19 +48,6 @@ public override void OnStart(PartModule.StartState state)
NE_Helper.logError("KEES PEC: AttachNode not found");
node = part.attachNodes.First();
}

/* Run this as a coroutine so the experiment aborts if the
* PEC decouples from the ship. */
StartCoroutine(checkNode());
}

public System.Collections.IEnumerator checkNode()
{
while (true)
{
checkForExp();
yield return new UnityEngine.WaitForSeconds(1f);
}
}

private void checkForExp()
Expand All @@ -69,29 +61,40 @@ private void checkForExp()
{
exp = newExp;
exp.dockedToPEC(true);
NE_Helper.log("New KEES Experiment installed");
}
else if (exp != newExp)
{
exp.dockedToPEC(false);
exp = newExp;
exp.dockedToPEC(true);
NE_Helper.log("KEES Experiment switched");
}
}
else if (exp != null)
{
exp.dockedToPEC(false);
NE_Helper.log("KEES Experiment undocked");
exp = null;
}
}
if (exp != null)
{
exp.dockedToPEC(false);
exp = null;
}

}

public override void OnUpdate()
{
base.OnUpdate();
if (vessel != null && !vessel.isEVA && vessel.geeForce > maxGforce)
if (!decoupled && vessel != null && !vessel.isEVA && vessel.geeForce > maxGforce)
{
NE_Helper.log("KEES PEC over max G, decouple");
decoupled = true;
part.decouple();
}
if (counter == 0)//don't run this every frame
{
checkForExp();
}
counter = (++counter) % 6;
}

}
Expand Down

0 comments on commit 6153b9e

Please sign in to comment.