From 6153b9e5767f77d0fadb371411baf9cd61162e05 Mon Sep 17 00:00:00 2001 From: Emanuel Date: Wed, 25 Feb 2015 16:49:58 +0100 Subject: [PATCH] Fix for #115 * decouple was called twice in 2 consecutive frames causing the copy * switched to a update based experiment check --- Plugin/NE Science/KEES_PEC.cs | 41 +++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/Plugin/NE Science/KEES_PEC.cs b/Plugin/NE Science/KEES_PEC.cs index e901f6e..d76cb31 100644 --- a/Plugin/NE Science/KEES_PEC.cs +++ b/Plugin/NE Science/KEES_PEC.cs @@ -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); @@ -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() @@ -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; } }