//float currentThrottleState;

        private void updateEngineModule(bool calledByPlayer, string callingFunction = "player")
        {
            string[] arrtargetPropellants, arrtargetRatios, arrtargetIgnoreForISP, arrtargetDrawGuage;
            float    targetRatio;
            bool     targetIgnoreForISP;
            float    maxISP = 0;
            float    floatParseResult;

            ConfigNode       newPropNode = new ConfigNode();
            Utilities        MiscFx      = new Utilities();
            PhysicsUtilities EngineCalc  = new PhysicsUtilities();

            writeScreenMessage();

            foreach (var moduleEngine in ModuleEngines)
            {
                //Debug.Log("moduleEngine.isEnabled: " + moduleEngine.isEnabled);
                //moduleEngine.isEnabled = !moduleEngine.isEnabled;
                //Debug.Log("moduleEngine.isEnabled: " + moduleEngine.isEnabled);
                //bool engineState = false;
                //float ThrottleState;

                currentModuleEngine = moduleEngine;
                //Get the Ignition state, i.e. is the engine shutdown or activated
                currentEngineState = currentModuleEngine.getIgnitionState;
                ShutdownEngine();

                //Deactivate effects to force KSP to update them as intended
                //currentModuleEngine.DeactivateLoopingFX();
                //currentModuleEngine.DeactivatePowerFX();
                //currentModuleEngine.DeactivateRunningFX();



                //currentThrottleState = FlightInputHandler.state.mainThrottle;
                //FlightInputHandler.state.mainThrottle = 0f;
                //currentModuleEngine.useEngineResponseTime = false;

                //moduleEngine.maxFuelFlow = 0f;

                //Invoke("ShutdownEngine",2f);

                ////Get the Ignition state, i.e. is the engine shutdown or activated
                //engineState = currentModuleEngine.getIgnitionState;
                ////Shutdown the engine --> Removes the gauges, and make sense to do before changing propellant
                ////Debug.Log("engine shutdown");
                //currentModuleEngine.Shutdown();
                ////Debug.Log("1) FlightInputHandler.state.mainThrottle : " + FlightInputHandler.state.mainThrottle);
                //ThrottleState = FlightInputHandler.state.mainThrottle;
                //FlightInputHandler.state.mainThrottle = 0f;

                //StartCoroutine(wait());
                //Debug.Log("2) FlightInputHandler.state.mainThrottle : " + FlightInputHandler.state.mainThrottle);

                moduleEngine.Flameout(
                    message: "Switch Engine State",
                    statusOnly: false,
                    showFX: false);

                //Invoke("shortwait", 2f);

                //Debug.Log("engine shutdown completed");

                //Split cfg subsettings into arrays
                arrtargetPropellants  = propList[selectedPropellant].Propellants.Split(',');
                arrtargetRatios       = propList[selectedPropellant].PropRatios.Split(',');
                arrtargetIgnoreForISP = propList[selectedPropellant].propIgnoreForISP.Split(',');
                arrtargetDrawGuage    = propList[selectedPropellant].propDrawGauge.Split(',');

                //Create new propellent nodes by looping them in.
                for (int i = 0; i < arrtargetPropellants.Length; i++)
                {
                    //Get and convert ratios to floats. They should already have been verified in the CustomTypes.PropellantList class
                    targetRatio = Convert.ToSingle(arrtargetRatios[i]);

                    //if ignoreForISP have been set wrong or not at all, then we config it to false
                    if (arrtargetIgnoreForISP.Length == arrtargetPropellants.Length)
                    {
                        if (!bool.TryParse(arrtargetIgnoreForISP[i], out targetIgnoreForISP))
                        {
                            targetIgnoreForISP = false;
                        }
                        //targetIgnoreForISP = arrtargetIgnoreForISP[i];
                    }
                    else
                    {
                        targetIgnoreForISP = false;
                    }

                    //Debug.Log("!bool.TryParse(arrtargetIgnoreForISP[i], out targetIgnoreForISP)\ntargetIgnoreForISP: " + targetIgnoreForISP);

                    ConfigNode propNode = newPropNode.AddNode("PROPELLANT");
                    propNode.AddValue("name", arrtargetPropellants[i]);
                    propNode.AddValue("ratio", targetRatio);
                    propNode.AddValue("ignoreForIsp", targetIgnoreForISP);      //For now we assume all is counted for ISP           //targetIgnoreForISP[i]
                    propNode.AddValue("DrawGauge", arrtargetDrawGuage[i]);      //I think the gauge  should always be shown
                }
                //Update the engine with new propellant configuration
                //NOTICE: The original propellant nodes are overwritten, so we do not need to delete them
                moduleEngine.Load(newPropNode);

                //Debug.Log("Start of curve editing");
                //Change the atmosphere curve (ISP)
                if (!atmosphereCurveEmpty)
                {
                    //Debug.Log("Setting atmosphere Curve (ISP) " + atmosphereCurveKeys);
                    moduleEngine.atmosphereCurve.Curve.keys =
                        MiscFx.KeyFrameFromString(
                            inKeys: propList[selectedPropellant].atmosphereCurve,
                            iniKeys: moduleEngine.atmosphereCurve.Curve.keys
                            );
                }
                //Change the Velocity curve
                if (!velCurveEmpty)                                         //(string.IsNullOrEmpty(velCurveKeys) || velCurveKeys.Trim().Length == 0))
                {
                    //Debug.Log("Setting Velocity Curve " + velCurveKeys);
                    moduleEngine.velCurve.Curve.keys =
                        MiscFx.KeyFrameFromString(
                            inKeys: propList[selectedPropellant].velCurve,
                            iniKeys: moduleEngine.velCurve.Curve.keys
                            );
                }
                //Change the Atm curve
                if (!atmCurveEmpty)                                         //(string.IsNullOrEmpty(atmCurveKeys) || atmCurveKeys.Trim().Length == 0))
                {
                    //Debug.Log("Setting Atm Curve: "+ atmCurveKeys);
                    moduleEngine.atmCurve.Curve.keys =
                        MiscFx.KeyFrameFromString(
                            inKeys: propList[selectedPropellant].atmCurve,
                            iniKeys: moduleEngine.atmCurve.Curve.keys
                            );
                }

                //Set which curves to use
                if (!atmChangeFlowsEmpty)
                {
                    moduleEngine.atmChangeFlow = bool.Parse(propList[selectedPropellant].atmChangeFlow);
                }
                if (!useVelCurvesEmpty)
                {
                    moduleEngine.useVelCurve = bool.Parse(propList[selectedPropellant].useVelCurve);
                }
                if (!useAtmCurvesEmpty)
                {
                    moduleEngine.useAtmCurve = bool.Parse(propList[selectedPropellant].useAtmCurve);
                }

                //Get maxISP from the atmosphere curve
                maxISP = MiscFx.KeyFrameGetMaxValue(moduleEngine.atmosphereCurve.Curve.keys);
                //moduleEngine.atmosphereCurve.FindMinMaxValue

                //Set max Thrust and the corresponding fuelflow
                if (!MaxThrustEmpty)
                {
                    moduleEngine.maxThrust = propList[selectedPropellant].maxThrust;
                }

                //maxFuelFlow = engine.maxThrust / (engine.atmosphereCurve.Evaluate(0f) * engine.g)
                moduleEngine.maxFuelFlow = EngineCalc.calcFuelFlow(
                    Thrust: moduleEngine.maxThrust,                             //Thrust: propList[selectedPropellant].maxThrust,
                    //Density: propList[selectedPropellant].propDensity,
                    ISP: maxISP
                    );

                if (Single.TryParse(propList[selectedPropellant].heatProduction, out floatParseResult) && !heatProductionEmpty)
                {
                    moduleEngine.heatProduction = floatParseResult;
                }

                //Set the engine type
                //[LiquidFuel, Nuclear, SolidBooster, Turbine, MonoProp, ScramJet, Electric, Generic, Piston]
                if (!EngineTypesEmpty)
                {
                    switch (propList[selectedPropellant].engineType)
                    {
                    case "LiquidFuel":
                        moduleEngine.engineType = EngineType.LiquidFuel;
                        break;

                    case "Nuclear":
                        moduleEngine.engineType = EngineType.Nuclear;
                        break;

                    case "SolidBooster":
                        moduleEngine.engineType = EngineType.SolidBooster;
                        break;

                    case "Turbine":
                        moduleEngine.engineType = EngineType.Turbine;
                        break;

                    case "MonoProp":
                        moduleEngine.engineType = EngineType.MonoProp;
                        break;

                    case "ScramJet":
                        moduleEngine.engineType = EngineType.ScramJet;
                        break;

                    case "Electric":
                        moduleEngine.engineType = EngineType.Electric;
                        break;

                    case "Generic":
                        moduleEngine.engineType = EngineType.Generic;
                        break;

                    case "Piston":
                        moduleEngine.engineType = EngineType.Piston;
                        break;

                    default:
                        moduleEngine.engineType = EngineType.LiquidFuel;
                        break;
                    }
                }

                moduleEngine.useEngineResponseTime   = useEngineResponseTimeEmpty ? false : bool.Parse(propList[selectedPropellant].useEngineResponseTime);
                moduleEngine.engineAccelerationSpeed = engineAccelerationSpeedEmpty ? 0 : Single.Parse(propList[selectedPropellant].engineAccelerationSpeed);
                moduleEngine.engineDecelerationSpeed = engineDecelerationSpeedEmpty ? 0 : Single.Parse(propList[selectedPropellant].engineDecelerationSpeed);

                //if (!useEngineResponseTimeEmpty)
                //{
                //    EngineEffectNode.SetValue("useEngineResponseTime", propList[selectedPropellant].useEngineResponseTime, true);
                //    Debug.Log("EngineEffectNode.SetValue('useEngineResponseTime', " + propList[selectedPropellant].useEngineResponseTime + ", true);");
                //}
                //if (!engineAccelerationSpeedEmpty)
                //{
                //    EngineEffectNode.SetValue("engineAccelerationSpeed", propList[selectedPropellant].engineAccelerationSpeed, true);
                //    Debug.Log("EngineEffectNode.SetValue('engineAccelerationSpeed', " + propList[selectedPropellant].engineAccelerationSpeed + ", true);");
                //}
                //if (!engineDecelerationSpeedEmpty)
                //{
                //    EngineEffectNode.SetValue("engineDecelerationSpeed", propList[selectedPropellant].engineDecelerationSpeed, true);
                //    Debug.Log("EngineEffectNode.SetValue('engineDecelerationSpeed', " + propList[selectedPropellant].engineDecelerationSpeed + ", true);");
                //}

                //Update the effects
                //updateEngineModuleEffects(
                //    calledByPlayer: calledByPlayer,
                //    callingFunction: callingFunction,
                //    moduleEngine: moduleEngine);

                //Write the propellant setup to the right click GUI
                GUIpropellantNames = propList[selectedPropellant].Propellants.Replace(",", ", ");

                if (iniGUIpropellantNamesEmpty)
                {   //Default naming if no user defined names are found
                    GUIpropellantNames = "[" + selectedPropellant + "] " + propList[selectedPropellant].Propellants.Replace(",", ", ");
                }
                else
                {                                                                         //User defined names
                    GUIpropellantNames = propList[selectedPropellant].GUIpropellantNames; //iniGUIpropellantNames.Trim().Split(';')[selectedPropellant];
                }

                //Write on screen message
                //ScreenMessages.PostScreenMessage("message", 3f, ScreenMessageStyle.UPPER_LEFT);
                ScreenMessages.PostScreenMessage(propList[selectedPropellant].GUIpropellantNames, 1.5f, ScreenMessageStyle.UPPER_CENTER);

                //Restart engine if it was on before switching
                //moduleEngine.Flameout("Switch Engine State", false, false);
                currentModuleEngine.UnFlameout(false);
                //FlightInputHandler.state.mainThrottle = currentThrottleState;
                //Invoke("ActivateEngine", 0f);
                //ActivateEngine();
                if (currentEngineState)
                {
                    moduleEngine.Activate();
                }
            }
        }