Jump to content

NullReferenceException Help


Recommended Posts

Hello I am trying to add fnctionallity/compatiblity between the mods Advanced Jet Engine and Davon Throttle Control. I believe I have the code setup correctly, but in game I am getting a NRE when activating DTC. I am very very new to coding and am not sure where or why I am getting this error other than something is null instead of what it should be. Any help is greatly appreciated. Thanks! I will provide any info requested. Forgot to say this is the latest build of 1.0.5 NOT 1.1

Error

Spoiler

[EXC 13:43:54.286] NullReferenceException: Object reference not set to an instance of an object
    DifferentialThrustMod.DifferentialThrustEngineModule.OnUpdate ()
    Part.ModulesOnUpdate ()
    Part.Update ()

Code

Spoiler

//Written by Flip van Toly for KSP community
//License GPL v2.0 (GNU General Public License)
// Namespace Declaration 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using System.Reflection;
using AJE;

namespace DifferentialThrustMod
{
	public class DifferentialThrustEngineModule : PartModule
	{
		private bool booted = false;
		public int enginemoduletype = 0;

		public ModuleEngines PartmoduleModuleEngines;
		public MultiModeEngine PartmoduleMultiModeEngine;
		public ModuleEnginesFX PartmoduleModuleEnginesFX;
		public ModuleEnginesAJERotor PartmoduleModuleEnginesAJERotor;
		public ModuleEnginesAJEJet PartmoduleModuleEnginesAJEJet;

		[KSPField(guiName = "Level Thrust", isPersistant = false, guiActive = true, guiActiveEditor = true)]
		[UI_FloatRange(stepIncrement = 1f, maxValue = 100f, minValue = 0f)]
		public float levelThrust = 100;

		[KSPField(guiName = "Throttle", isPersistant = false, guiActive = true, guiActiveEditor = true)]
		[UI_FloatRange(stepIncrement = 1f, maxValue = 4f, minValue = 0f)]
		public float throttleFloatSelect;
		public int throttleSelect;
		public float THRAIM = 0;

		public float throttleSvalue;

		//[KSPField(guiName = "Center Thrust", isPersistant = false, guiActive = true, guiActiveEditor = true)]
		public string CenterThrustMode = "available";
		public bool CenterThrust = false;

		[KSPField(isPersistant = false, guiActive = false, guiName = "CoT calc", guiUnits = "")]
		[UI_Toggle(disabledText = "Include", enabledText = "Exclude")]
		public bool CoTcalc = true;


		[KSPField(isPersistant = false, guiActive = false, guiName = "Aim", guiUnits = "")]
		[UI_FloatRange(stepIncrement = 0.001f, maxValue = 100f, minValue = 0f)]
		public float aim = 100;

		[KSPField(isPersistant = false, guiActive = true, guiName = "net", guiUnits = "")]
		[UI_Toggle(disabledText = "Connected", enabledText = "Isolated")]
		public bool isolated = false;

		public bool StoredOuseEngineResponseTime;
		public float StoredOengineAccelerationSpeed;
		public float StoredOengineDecelerationSpeed;

		[KSPEvent(name = "cycleCenterThrustMode", isDefault = false, guiActive = true, guiName = "Center thrust: available")]
		public void CycleCenterThrustMode()
		{
			if (CenterThrustMode == "available")
				CenterThrustMode = "designated";
			else if (CenterThrustMode == "designated")
				CenterThrustMode = "ignore";
			else if (CenterThrustMode == "ignore")
				CenterThrustMode = "available";
			else
				CenterThrustMode = "available";

			Events["CycleCenterThrustMode"].guiName = "Center thrust: " + CenterThrustMode;
		}

		//Adjust engines every cycle. Purposfull OnUpdate instead of OnFixedUpdate.
		public override void OnUpdate()
		{
			if (booted == false)
			{
				boot();
				return;
			}
			
			if (enginemoduletype == 0)
			{
				if (PartmoduleModuleEngines.throttleLocked == true)
				{
					return;
				}
			}
			if (enginemoduletype == 3) {
				if (PartmoduleModuleEnginesAJERotor.throttleLocked == true)
				{
					return;
				}
			}
			if (enginemoduletype == 4) {
				if (PartmoduleModuleEnginesAJEJet.throttleLocked == true)
				{
					return;
				}
			}
			else
			{
				if (PartmoduleModuleEnginesFX.throttleLocked == true)
				{
					return;
				}
			}

			if (enginemoduletype == 2)
			{
				if (PartmoduleModuleEnginesFX.engineID != PartmoduleMultiModeEngine.mode)
				{
					PartmoduleModuleEnginesFX.useEngineResponseTime = StoredOuseEngineResponseTime;
					PartmoduleModuleEnginesFX.engineAccelerationSpeed = StoredOengineAccelerationSpeed;
					PartmoduleModuleEnginesFX.engineDecelerationSpeed = StoredOengineDecelerationSpeed;
					booted = false;
					return;
				}
			}

			if (enginemoduletype == 0)
			{
				if (!PartmoduleModuleEngines.EngineIgnited || PartmoduleModuleEngines.engineShutdown)
				{
					PartmoduleModuleEngines.currentThrottle = 0;
					return;
				}
			}
			if (enginemoduletype == 3)
			{
				if (!PartmoduleModuleEnginesAJERotor.EngineIgnited || PartmoduleModuleEnginesAJERotor.engineShutdown)
				{
					PartmoduleModuleEnginesAJERotor.currentThrottle = 0;
					return;
				}
			}
			if (enginemoduletype == 4)
			{
				if (!PartmoduleModuleEnginesAJEJet.EngineIgnited || PartmoduleModuleEnginesAJEJet.engineShutdown)
				{
					PartmoduleModuleEnginesAJEJet.currentThrottle = 0;
					return;
				}
			}
			else
			{
				if (!PartmoduleModuleEnginesFX.EngineIgnited || PartmoduleModuleEnginesFX.engineShutdown)
				{
					PartmoduleModuleEnginesFX.currentThrottle = 0;
					return;
				}
			}




			//set to correct throttle
			throttleSelect = (int)Math.Round(throttleFloatSelect, 0);


			//retrieve correct throttle value based on selected throttle
			if (throttleSelect == 0)
			{
				throttleSvalue = vessel.ctrlState.mainThrottle;
			}
			else
			{
				throttleSvalue = THRAIM / 100;
			}

			//if center thrust is enabled for this engine, set it to the desired aimpoint
			if (CenterThrust == true)
			{
				if (enginemoduletype == 0)
				{
					PartmoduleModuleEngines.thrustPercentage = aim;
				}
				if (enginemoduletype == 3)
				{
					PartmoduleModuleEnginesAJERotor.thrustPercentage = aim;
				}
				if (enginemoduletype == 4)
				{
					PartmoduleModuleEnginesAJEJet.thrustPercentage = aim;
				}
				else
				{
					PartmoduleModuleEnginesFX.thrustPercentage = aim;
				}
				Fields["aim"].guiActive = true;

				levelThrust = 100f;
				Fields["levelThrust"].guiActive = false;
			}
			else
			{
				Fields["aim"].guiActive = false;

				Fields["levelThrust"].guiActive = true;
			}


			
			float thrustperc = 100;
			if (enginemoduletype == 0)
			{
				thrustperc = PartmoduleModuleEngines.thrustPercentage;
			}
			if (enginemoduletype == 3)
			{
				thrustperc = PartmoduleModuleEnginesAJERotor.thrustPercentage;
			}
			if (enginemoduletype == 4)
			{
				thrustperc = PartmoduleModuleEnginesAJEJet.thrustPercentage;
			}
			else
			{
				thrustperc = PartmoduleModuleEnginesFX.thrustPercentage;
			}

			if ((levelThrust / 100) / (throttleSvalue * (thrustperc / 100)) < 1)
			{
				setThrottle(levelThrust / 100);
			}
			else
			{
				setThrottle(throttleSvalue * (thrustperc / 100));
			}
		}


		private void setThrottle(float Throttle)
		{
			//PartmoduleModuleEngines.currentThrottle = Throttle;


			if (enginemoduletype == 0)
			{
				//With thanks to ZRM, maker of Kerbcom Avionics, and the help of the code of the Throttle Steering mod made by ruffus.
				if (StoredOuseEngineResponseTime && !CenterThrust)
				{
					if (PartmoduleModuleEngines.currentThrottle > Throttle)
						PartmoduleModuleEngines.currentThrottle = Mathf.Lerp(PartmoduleModuleEngines.currentThrottle, Throttle, StoredOengineDecelerationSpeed * Time.deltaTime);
					else
						PartmoduleModuleEngines.currentThrottle = Mathf.Lerp(PartmoduleModuleEngines.currentThrottle, Throttle, StoredOengineAccelerationSpeed * Time.deltaTime);
				}
				else
				{
					PartmoduleModuleEngines.currentThrottle = Throttle;
				}
			}
			if (enginemoduletype == 3)
			{
				//With thanks to ZRM, maker of Kerbcom Avionics, and the help of the code of the Throttle Steering mod made by ruffus.
				if (StoredOuseEngineResponseTime && !CenterThrust)
				{
					if (PartmoduleModuleEnginesAJERotor.currentThrottle > Throttle)
						PartmoduleModuleEnginesAJERotor.currentThrottle = Mathf.Lerp(PartmoduleModuleEnginesAJERotor.currentThrottle, Throttle, StoredOengineDecelerationSpeed * Time.deltaTime);
					else
						PartmoduleModuleEnginesAJERotor.currentThrottle = Mathf.Lerp(PartmoduleModuleEnginesAJERotor.currentThrottle, Throttle, StoredOengineAccelerationSpeed * Time.deltaTime);
				}
				else
				{
					PartmoduleModuleEnginesAJERotor.currentThrottle = Throttle;
				}
			}
			if (enginemoduletype == 4)
			{
				//With thanks to ZRM, maker of Kerbcom Avionics, and the help of the code of the Throttle Steering mod made by ruffus.
				if (StoredOuseEngineResponseTime && !CenterThrust)
				{
					if (PartmoduleModuleEnginesAJEJet.currentThrottle > Throttle)
						PartmoduleModuleEnginesAJEJet.currentThrottle = Mathf.Lerp(PartmoduleModuleEnginesAJEJet.currentThrottle, Throttle, StoredOengineDecelerationSpeed * Time.deltaTime);
					else
						PartmoduleModuleEnginesAJEJet.currentThrottle = Mathf.Lerp(PartmoduleModuleEnginesAJEJet.currentThrottle, Throttle, StoredOengineAccelerationSpeed * Time.deltaTime);
				}
				else
				{
					PartmoduleModuleEnginesAJEJet.currentThrottle = Throttle;
				}
			}
			else
			{
				//With thanks to ZRM, maker of Kerbcom Avionics, and the help of the code of the Throttle Steering mod made by ruffus.
				if (StoredOuseEngineResponseTime && !CenterThrust)
				{
					if (PartmoduleModuleEnginesFX.currentThrottle > Throttle)
						PartmoduleModuleEnginesFX.currentThrottle = Mathf.Lerp(PartmoduleModuleEnginesFX.currentThrottle, Throttle, StoredOengineDecelerationSpeed * Time.deltaTime);
					else
						PartmoduleModuleEnginesFX.currentThrottle = Mathf.Lerp(PartmoduleModuleEnginesFX.currentThrottle, Throttle, StoredOengineAccelerationSpeed * Time.deltaTime);
				}
				else
				{
					PartmoduleModuleEnginesFX.currentThrottle = Throttle;
				}
			}
		}

		//first startup boot sequence
		private void boot()
		{
			//print("booting");

			//Euid = (int)part.uid;
			enginemoduletype = 0;
			foreach (PartModule pm in part.Modules)
			{
				if (pm.ClassName == "MultiModeEngine")
				{
					enginemoduletype = 2;
					PartmoduleMultiModeEngine = (MultiModeEngine)pm;
					ChooseMultiModeEngine();

					//store original values before engine control takeover
					StoredOuseEngineResponseTime = PartmoduleModuleEnginesFX.useEngineResponseTime;
					StoredOengineAccelerationSpeed = PartmoduleModuleEnginesFX.engineAccelerationSpeed;
					StoredOengineDecelerationSpeed = PartmoduleModuleEnginesFX.engineDecelerationSpeed;

					//This settings must be set to true to be able to control engines with currentThrottle. 
					//Found this with the help of the code of the Throttle Steering mod made by ruffus. 
					PartmoduleModuleEnginesFX.useEngineResponseTime = true;

					//This eliminates the influence of the main throttle on engines
					PartmoduleModuleEnginesFX.engineAccelerationSpeed = 0.0f;
					PartmoduleModuleEnginesFX.engineDecelerationSpeed = 0.0f;

					//set aim to chosen limit thrust
					aim = PartmoduleModuleEnginesFX.thrustPercentage;
				}
			}
			if (enginemoduletype != 2)
			{
				foreach (PartModule pm in part.Modules)
				{
					if (pm.ClassName == "ModuleEngines")
					{
						enginemoduletype = 0;
						PartmoduleModuleEngines = (ModuleEngines)pm;

						//store original values before engine control takeover
						StoredOuseEngineResponseTime = PartmoduleModuleEngines.useEngineResponseTime;
						StoredOengineAccelerationSpeed = PartmoduleModuleEngines.engineAccelerationSpeed;
						StoredOengineDecelerationSpeed = PartmoduleModuleEngines.engineDecelerationSpeed;

						//This settings must be set to true to be able to control engines with currentThrottle. 
						//Found this with the help of the code of the Throttle Steering mod made by ruffus. 
						PartmoduleModuleEngines.useEngineResponseTime = true;

						//This eliminates the influence of the main throttle on engines
						PartmoduleModuleEngines.engineAccelerationSpeed = 0.0f;
						PartmoduleModuleEngines.engineDecelerationSpeed = 0.0f;

						//set aim to chosen limit thrust
						aim = PartmoduleModuleEngines.thrustPercentage;

					}
					if (pm.ClassName == "ModuleEnginesFX")
					{
						enginemoduletype = 1;
						PartmoduleModuleEnginesFX = (ModuleEnginesFX)pm;

						//store original values before engine control takeover
						StoredOuseEngineResponseTime = PartmoduleModuleEnginesFX.useEngineResponseTime;
						StoredOengineAccelerationSpeed = PartmoduleModuleEnginesFX.engineAccelerationSpeed;
						StoredOengineDecelerationSpeed = PartmoduleModuleEnginesFX.engineDecelerationSpeed;

						//This settings must be set to true to be able to control engines with currentThrottle. 
						//Found this with the help of the code of the Throttle Steering mod made by ruffus. 
						PartmoduleModuleEnginesFX.useEngineResponseTime = true;

						//This eliminates the influence of the main throttle on engines
						PartmoduleModuleEnginesFX.engineAccelerationSpeed = 0.0f;
						PartmoduleModuleEnginesFX.engineDecelerationSpeed = 0.0f;

						//set aim to chosen limit thrust
						aim = PartmoduleModuleEnginesFX.thrustPercentage;
					}
					if (pm.ClassName == "ModuleEnginesAJERotor")
					{
						enginemoduletype = 3;
						PartmoduleModuleEnginesAJERotor = (ModuleEnginesAJERotor)pm;

						//store original values before engine control takeover
						StoredOuseEngineResponseTime = PartmoduleModuleEnginesAJERotor.useEngineResponseTime;
						StoredOengineAccelerationSpeed = PartmoduleModuleEnginesAJERotor.engineAccelerationSpeed;
						StoredOengineDecelerationSpeed = PartmoduleModuleEnginesAJERotor.engineDecelerationSpeed;

						//This settings must be set to true to be able to control engines with currentThrottle. 
						//Found this with the help of the code of the Throttle Steering mod made by ruffus. 
						PartmoduleModuleEnginesAJERotor.useEngineResponseTime = true;

						//This eliminates the influence of the main throttle on engines
						PartmoduleModuleEnginesAJERotor.engineAccelerationSpeed = 0.0f;
						PartmoduleModuleEnginesAJERotor.engineDecelerationSpeed = 0.0f;

						//set aim to chosen limit thrust
						aim = PartmoduleModuleEnginesAJERotor.thrustPercentage;
					}
					if (pm.ClassName == "ModuleEnginesAJEJet")
					{
						enginemoduletype = 4;
						PartmoduleModuleEnginesAJEJet = (ModuleEnginesAJEJet)pm;

						//store original values before engine control takeover
						StoredOuseEngineResponseTime = PartmoduleModuleEnginesAJEJet.useEngineResponseTime;
						StoredOengineAccelerationSpeed = PartmoduleModuleEnginesAJEJet.engineAccelerationSpeed;
						StoredOengineDecelerationSpeed = PartmoduleModuleEnginesAJEJet.engineDecelerationSpeed;

						//This settings must be set to true to be able to control engines with currentThrottle. 
						//Found this with the help of the code of the Throttle Steering mod made by ruffus. 
						PartmoduleModuleEnginesAJEJet.useEngineResponseTime = true;

						//This eliminates the influence of the main throttle on engines
						PartmoduleModuleEnginesAJEJet.engineAccelerationSpeed = 0.0f;
						PartmoduleModuleEnginesAJEJet.engineDecelerationSpeed = 0.0f;

						//set aim to chosen limit thrust
						aim = PartmoduleModuleEnginesAJEJet.thrustPercentage;z
					}
				}
			}

			Events["transferToAllEngineOfType"].guiName = "Sync all " + part.partInfo.name;

			booted = true;//boot completed
		}

		private void ChooseMultiModeEngine()
		{
			foreach (PartModule pm in part.Modules)
			{
				if (pm.ClassName == "ModuleEnginesFX")
				{
					ModuleEnginesFX cModuleEnginesFX = (ModuleEnginesFX)pm;
					if (cModuleEnginesFX.engineID == PartmoduleMultiModeEngine.mode)
					{
						PartmoduleModuleEnginesFX = (ModuleEnginesFX)pm;
					}
				}
			}
		}

		[KSPEvent(name = "transferToAllEngineOfType", isDefault = false, guiActive = true, guiName = "Sync all enginetype")]
		public void transferToAllEngineOfType()
		{
			foreach (Part p in vessel.parts)
			{

				if (p.partInfo.name == part.partInfo.name)
				{
					foreach (PartModule pm in p.Modules)
					{
						if (pm.ClassName == "DifferentialThrustEngineModule")
						{
							DifferentialThrustEngineModule aDifferentialThrustEngineModule;
							aDifferentialThrustEngineModule = p.Modules.OfType<DifferentialThrustEngineModule>().FirstOrDefault();

							if (aDifferentialThrustEngineModule.isolated == false)
							{
								aDifferentialThrustEngineModule.levelThrust = levelThrust;
								aDifferentialThrustEngineModule.throttleFloatSelect = throttleFloatSelect;
								aDifferentialThrustEngineModule.CenterThrustMode = CenterThrustMode;
								aDifferentialThrustEngineModule.Events["CycleCenterThrustMode"].guiName = "Center thrust: " + aDifferentialThrustEngineModule.CenterThrustMode;
								aDifferentialThrustEngineModule.aim = aim;
								aDifferentialThrustEngineModule.isolated = isolated;

								foreach (PartModule pmt in p.Modules)
								{
									if (pmt.ClassName == "ModuleEngines")
									{
										ModuleEngines aModuleEngines;
										aModuleEngines = (ModuleEngines)pmt;

										aModuleEngines.thrustPercentage = PartmoduleModuleEngines.thrustPercentage;
									}
									if (pmt.ClassName == "ModuleEnginesFX")
									{
										ModuleEnginesFX aModuleEnginesFX;
										aModuleEnginesFX = (ModuleEnginesFX)pmt;

										aModuleEnginesFX.thrustPercentage = PartmoduleModuleEnginesFX.thrustPercentage;
									}
									if (pmt.ClassName == "ModuleEnginesAJERotor")
									{
										ModuleEnginesAJERotor aModuleEnginesAJERotor;
										aModuleEnginesAJERotor = (ModuleEnginesAJERotor)pmt;

										aModuleEnginesAJERotor.thrustPercentage = PartmoduleModuleEnginesAJERotor.thrustPercentage;
									}
									if (pmt.ClassName == "ModuleEnginesAJEJet")
									{
										ModuleEnginesAJEJet aModuleEnginesAJEJet;
										aModuleEnginesAJEJet = (ModuleEnginesAJEJet)pmt;

										aModuleEnginesAJEJet.thrustPercentage = PartmoduleModuleEnginesAJEJet.thrustPercentage;
									}
								}
							}
						}
					}
				}
			}
		}
	}
}

 

 

Edited by Svm420
Link to comment
Share on other sites

A quick and dirty way would to put a series of Debug.Log("ID"); Where ID would be different for each Debug.Log statement (ie "1", "2", "3"...) - when the NullRef hits the rest of the method is skipped and so you'll be missing some of the Debug outputs and that will tell you where the code was last executing before the NullRef. Can be a bit painful with the compile/run KSP/examine logs cycle but you'll likely find the problem area. By the way - the Debug.Log statements output to the KSP.log file. You could also put something in output in addition to the ID that you could then search for in the log file - lot easier with a large log file.

Link to comment
Share on other sites

15 hours ago, wasml said:

A quick and dirty way would to put a series of Debug.Log("ID"); Where ID would be different for each Debug.Log statement (ie "1", "2", "3"...) - when the NullRef hits the rest of the method is skipped and so you'll be missing some of the Debug outputs and that will tell you where the code was last executing before the NullRef. Can be a bit painful with the compile/run KSP/examine logs cycle but you'll likely find the problem area. By the way - the Debug.Log statements output to the KSP.log file. You could also put something in output in addition to the ID that you could then search for in the log file - lot easier with a large log file.

 

5 hours ago, sarbian said:

Or set proper debugging and set it up to break on exception (an option in the UnityVS config)

Alright I added some debug.log lines, and did what sarbian posted though I unfortunately don't know enough about coding to use it to help myself :(,  to the code and I know the issue is in the OnUpdate() section. As it runs through the code it something ends up null. Still way too new at this to figure out anymore. Here is a copy of the latest code...

Spoiler

//Written by Flip van Toly for KSP community
//License GPL v2.0 (GNU General Public License)
// Namespace Declaration 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using System.Reflection;
using AJE;

namespace DifferentialThrustMod
{
	public class DifferentialThrustEngineModule : PartModule
	{
		private bool booted = false;
		public int enginemoduletype = 0;

		public ModuleEngines PartmoduleModuleEngines;
		public MultiModeEngine PartmoduleMultiModeEngine;
		public ModuleEnginesFX PartmoduleModuleEnginesFX;
		public ModuleEnginesAJERotor PartmoduleModuleEnginesAJERotor;
		public ModuleEnginesAJEJet PartmoduleModuleEnginesAJEJet;

		[KSPField(guiName = "Level Thrust", isPersistant = false, guiActive = true, guiActiveEditor = true)]
		[UI_FloatRange(stepIncrement = 1f, maxValue = 100f, minValue = 0f)]
		public float levelThrust = 100;

		[KSPField(guiName = "Throttle", isPersistant = false, guiActive = true, guiActiveEditor = true)]
		[UI_FloatRange(stepIncrement = 1f, maxValue = 4f, minValue = 0f)]
		public float throttleFloatSelect;
		public int throttleSelect;
		public float THRAIM = 0;

		public float throttleSvalue;

		//[KSPField(guiName = "Center Thrust", isPersistant = false, guiActive = true, guiActiveEditor = true)]
		public string CenterThrustMode = "available";
		public bool CenterThrust = false;

		[KSPField(isPersistant = false, guiActive = false, guiName = "CoT calc", guiUnits = "")]
		[UI_Toggle(disabledText = "Include", enabledText = "Exclude")]
		public bool CoTcalc = true;


		[KSPField(isPersistant = false, guiActive = false, guiName = "Aim", guiUnits = "")]
		[UI_FloatRange(stepIncrement = 0.001f, maxValue = 100f, minValue = 0f)]
		public float aim = 100;

		[KSPField(isPersistant = false, guiActive = true, guiName = "net", guiUnits = "")]
		[UI_Toggle(disabledText = "Connected", enabledText = "Isolated")]
		public bool isolated = false;

		public bool StoredOuseEngineResponseTime;
		public float StoredOengineAccelerationSpeed;
		public float StoredOengineDecelerationSpeed;

		[KSPEvent(name = "cycleCenterThrustMode", isDefault = false, guiActive = true, guiName = "Center thrust: available")]
		public void CycleCenterThrustMode()
		{
			if (CenterThrustMode == "available")
				CenterThrustMode = "designated";
			else if (CenterThrustMode == "designated")
				CenterThrustMode = "ignore";
			else if (CenterThrustMode == "ignore")
				CenterThrustMode = "available";
			else
				CenterThrustMode = "available";

			Events["CycleCenterThrustMode"].guiName = "Center thrust: " + CenterThrustMode;
		}

		//Adjust engines every cycle. Purposfull OnUpdate instead of OnFixedUpdate.
		public override void OnUpdate()
		{
			Debug.Log ("0");
			if (booted == false)
			{
				boot();
				Debug.Log ("1");
				return;
			}
	
			if (enginemoduletype == 0)
			{
				if (PartmoduleModuleEngines.throttleLocked == true)
				{
					Debug.Log ("2");
					return;
				}
			}
			if (enginemoduletype == 3) {
				if (PartmoduleModuleEnginesAJERotor.throttleLocked == true)
				{
					Debug.Log ("3");
					return;
				}
			}
			if (enginemoduletype == 4) {
				if (PartmoduleModuleEnginesAJEJet.throttleLocked == true)
				{
					Debug.Log ("4");
					return;
				}
			}
			else
			{
				if (PartmoduleModuleEnginesFX.throttleLocked == true)
				{
					Debug.Log ("5");
					return;
				}
			}

			if (enginemoduletype == 2)
			{
				if (PartmoduleModuleEnginesFX.engineID != PartmoduleMultiModeEngine.mode)
				{
					PartmoduleModuleEnginesFX.useEngineResponseTime = StoredOuseEngineResponseTime;
					PartmoduleModuleEnginesFX.engineAccelerationSpeed = StoredOengineAccelerationSpeed;
					PartmoduleModuleEnginesFX.engineDecelerationSpeed = StoredOengineDecelerationSpeed;
					booted = false;
					Debug.Log ("6");
					return;
				}
			}

			if (enginemoduletype == 0)
			{
				if (!PartmoduleModuleEngines.EngineIgnited || PartmoduleModuleEngines.engineShutdown)
				{
					PartmoduleModuleEngines.currentThrottle = 0;
					Debug.Log ("7");
					return;
				}
			}
			if (enginemoduletype == 3)
			{
				if (!PartmoduleModuleEnginesAJERotor.EngineIgnited || PartmoduleModuleEnginesAJERotor.engineShutdown)
				{
					PartmoduleModuleEnginesAJERotor.currentThrottle = 0;
					Debug.Log ("8");
					return;
				}
			}
			if (enginemoduletype == 4)
			{
				if (!PartmoduleModuleEnginesAJEJet.EngineIgnited || PartmoduleModuleEnginesAJEJet.engineShutdown)
				{
					PartmoduleModuleEnginesAJEJet.currentThrottle = 0;
					Debug.Log ("9");
					return;
				}
			}
			else
			{
				if (!PartmoduleModuleEnginesFX.EngineIgnited || PartmoduleModuleEnginesFX.engineShutdown)
				{
					PartmoduleModuleEnginesFX.currentThrottle = 0;
					Debug.Log ("10");
					return;
				}
			}




			//set to correct throttle
			throttleSelect = (int)Math.Round(throttleFloatSelect, 0);
			Debug.Log ("11");


			//retrieve correct throttle value based on selected throttle
			if (throttleSelect == 0)
			{
				throttleSvalue = vessel.ctrlState.mainThrottle;
				Debug.Log ("12");
			}
			else
			{
				throttleSvalue = THRAIM / 100;
				Debug.Log ("13");
			}

			//if center thrust is enabled for this engine, set it to the desired aimpoint
			if (CenterThrust == true)
			{
				if (enginemoduletype == 0)
				{
					PartmoduleModuleEngines.thrustPercentage = aim;
				}
				if (enginemoduletype == 3)
				{
					PartmoduleModuleEnginesAJERotor.thrustPercentage = aim;
				}
				if (enginemoduletype == 4)
				{
					PartmoduleModuleEnginesAJEJet.thrustPercentage = aim;
				}
				else
				{
					PartmoduleModuleEnginesFX.thrustPercentage = aim;
				}
				Fields["aim"].guiActive = true;

				levelThrust = 100f;
				Fields["levelThrust"].guiActive = false;
			}
			else
			{
				Fields["aim"].guiActive = false;
				Debug.Log ("14");
				Fields["levelThrust"].guiActive = true;
			}



			float thrustperc = 100;
			if (enginemoduletype == 0)
			{
				thrustperc = PartmoduleModuleEngines.thrustPercentage;
			}
			if (enginemoduletype == 3)
			{
				thrustperc = PartmoduleModuleEnginesAJERotor.thrustPercentage;
				Debug.Log ("15");
			}
			if (enginemoduletype == 4)
			{
				thrustperc = PartmoduleModuleEnginesAJEJet.thrustPercentage;
				Debug.Log ("16");
			}
			else
			{
				thrustperc = PartmoduleModuleEnginesFX.thrustPercentage;
			}

			if ((levelThrust / 100) / (throttleSvalue * (thrustperc / 100)) < 1)
			{
				setThrottle(levelThrust / 100);
			}
			else
			{
				setThrottle(throttleSvalue * (thrustperc / 100));
			}
		}


		private void setThrottle(float Throttle)
		{
			//PartmoduleModuleEngines.currentThrottle = Throttle;


			if (enginemoduletype == 0)
			{
				//With thanks to ZRM, maker of Kerbcom Avionics, and the help of the code of the Throttle Steering mod made by ruffus.
				if (StoredOuseEngineResponseTime && !CenterThrust)
				{
					if (PartmoduleModuleEngines.currentThrottle > Throttle)
						PartmoduleModuleEngines.currentThrottle = Mathf.Lerp(PartmoduleModuleEngines.currentThrottle, Throttle, StoredOengineDecelerationSpeed * Time.deltaTime);
					else
						PartmoduleModuleEngines.currentThrottle = Mathf.Lerp(PartmoduleModuleEngines.currentThrottle, Throttle, StoredOengineAccelerationSpeed * Time.deltaTime);
				}
				else
				{
					PartmoduleModuleEngines.currentThrottle = Throttle;
				}
			}
			if (enginemoduletype == 3)
			{
				//With thanks to ZRM, maker of Kerbcom Avionics, and the help of the code of the Throttle Steering mod made by ruffus.
				if (StoredOuseEngineResponseTime && !CenterThrust)
				{
					if (PartmoduleModuleEnginesAJERotor.currentThrottle > Throttle)
						PartmoduleModuleEnginesAJERotor.currentThrottle = Mathf.Lerp(PartmoduleModuleEnginesAJERotor.currentThrottle, Throttle, StoredOengineDecelerationSpeed * Time.deltaTime);
					else
						PartmoduleModuleEnginesAJERotor.currentThrottle = Mathf.Lerp(PartmoduleModuleEnginesAJERotor.currentThrottle, Throttle, StoredOengineAccelerationSpeed * Time.deltaTime);
				}
				else
				{
					PartmoduleModuleEnginesAJERotor.currentThrottle = Throttle;
				}
			}
			if (enginemoduletype == 4)
			{
				//With thanks to ZRM, maker of Kerbcom Avionics, and the help of the code of the Throttle Steering mod made by ruffus.
				if (StoredOuseEngineResponseTime && !CenterThrust)
				{
					if (PartmoduleModuleEnginesAJEJet.currentThrottle > Throttle)
						PartmoduleModuleEnginesAJEJet.currentThrottle = Mathf.Lerp(PartmoduleModuleEnginesAJEJet.currentThrottle, Throttle, StoredOengineDecelerationSpeed * Time.deltaTime);
					else
						PartmoduleModuleEnginesAJEJet.currentThrottle = Mathf.Lerp(PartmoduleModuleEnginesAJEJet.currentThrottle, Throttle, StoredOengineAccelerationSpeed * Time.deltaTime);
				}
				else
				{
					PartmoduleModuleEnginesAJEJet.currentThrottle = Throttle;
				}
			}
			else
			{
				//With thanks to ZRM, maker of Kerbcom Avionics, and the help of the code of the Throttle Steering mod made by ruffus.
				if (StoredOuseEngineResponseTime && !CenterThrust)
				{
					if (PartmoduleModuleEnginesFX.currentThrottle > Throttle)
						PartmoduleModuleEnginesFX.currentThrottle = Mathf.Lerp(PartmoduleModuleEnginesFX.currentThrottle, Throttle, StoredOengineDecelerationSpeed * Time.deltaTime);
					else
						PartmoduleModuleEnginesFX.currentThrottle = Mathf.Lerp(PartmoduleModuleEnginesFX.currentThrottle, Throttle, StoredOengineAccelerationSpeed * Time.deltaTime);
				}
				else
				{
					PartmoduleModuleEnginesFX.currentThrottle = Throttle;
				}
			}
		}

		//first startup boot sequence
		private void boot()
		{
			//print("booting");
			Debug.Log ("17");
			//Euid = (int)part.uid;
			enginemoduletype = 0;
			foreach (PartModule pm in part.Modules)
			{
				if (pm.ClassName == "MultiModeEngine")
				{
					enginemoduletype = 2;
					PartmoduleMultiModeEngine = (MultiModeEngine)pm;
					ChooseMultiModeEngine();

					//store original values before engine control takeover
					StoredOuseEngineResponseTime = PartmoduleModuleEnginesFX.useEngineResponseTime;
					StoredOengineAccelerationSpeed = PartmoduleModuleEnginesFX.engineAccelerationSpeed;
					StoredOengineDecelerationSpeed = PartmoduleModuleEnginesFX.engineDecelerationSpeed;

					//This settings must be set to true to be able to control engines with currentThrottle. 
					//Found this with the help of the code of the Throttle Steering mod made by ruffus. 
					PartmoduleModuleEnginesFX.useEngineResponseTime = true;

					//This eliminates the influence of the main throttle on engines
					PartmoduleModuleEnginesFX.engineAccelerationSpeed = 0.0f;
					PartmoduleModuleEnginesFX.engineDecelerationSpeed = 0.0f;

					//set aim to chosen limit thrust
					aim = PartmoduleModuleEnginesFX.thrustPercentage;
				}
			}
			if (enginemoduletype != 2)
			{
				foreach (PartModule pm in part.Modules)
				{
					if (pm.ClassName == "ModuleEngines")
					{
						Debug.Log ("18");
						enginemoduletype = 0;
						PartmoduleModuleEngines = (ModuleEngines)pm;

						//store original values before engine control takeover
						StoredOuseEngineResponseTime = PartmoduleModuleEngines.useEngineResponseTime;
						StoredOengineAccelerationSpeed = PartmoduleModuleEngines.engineAccelerationSpeed;
						StoredOengineDecelerationSpeed = PartmoduleModuleEngines.engineDecelerationSpeed;

						//This settings must be set to true to be able to control engines with currentThrottle. 
						//Found this with the help of the code of the Throttle Steering mod made by ruffus. 
						PartmoduleModuleEngines.useEngineResponseTime = true;

						//This eliminates the influence of the main throttle on engines
						PartmoduleModuleEngines.engineAccelerationSpeed = 0.0f;
						PartmoduleModuleEngines.engineDecelerationSpeed = 0.0f;

						//set aim to chosen limit thrust
						aim = PartmoduleModuleEngines.thrustPercentage;

					}
					if (pm.ClassName == "ModuleEnginesFX")
					{
						Debug.Log ("19");
						enginemoduletype = 1;
						PartmoduleModuleEnginesFX = (ModuleEnginesFX)pm;

						//store original values before engine control takeover
						StoredOuseEngineResponseTime = PartmoduleModuleEnginesFX.useEngineResponseTime;
						StoredOengineAccelerationSpeed = PartmoduleModuleEnginesFX.engineAccelerationSpeed;
						StoredOengineDecelerationSpeed = PartmoduleModuleEnginesFX.engineDecelerationSpeed;

						//This settings must be set to true to be able to control engines with currentThrottle. 
						//Found this with the help of the code of the Throttle Steering mod made by ruffus. 
						PartmoduleModuleEnginesFX.useEngineResponseTime = true;

						//This eliminates the influence of the main throttle on engines
						PartmoduleModuleEnginesFX.engineAccelerationSpeed = 0.0f;
						PartmoduleModuleEnginesFX.engineDecelerationSpeed = 0.0f;

						//set aim to chosen limit thrust
						aim = PartmoduleModuleEnginesFX.thrustPercentage;
					}
					if (pm.ClassName == "ModuleEnginesAJERotor")
					{
						Debug.Log ("20");
						enginemoduletype = 3;
						PartmoduleModuleEnginesAJERotor = (ModuleEnginesAJERotor)pm;

						//store original values before engine control takeover
						StoredOuseEngineResponseTime = PartmoduleModuleEnginesAJERotor.useEngineResponseTime;
						StoredOengineAccelerationSpeed = PartmoduleModuleEnginesAJERotor.engineAccelerationSpeed;
						StoredOengineDecelerationSpeed = PartmoduleModuleEnginesAJERotor.engineDecelerationSpeed;

						//This settings must be set to true to be able to control engines with currentThrottle. 
						//Found this with the help of the code of the Throttle Steering mod made by ruffus. 
						PartmoduleModuleEnginesAJERotor.useEngineResponseTime = true;

						//This eliminates the influence of the main throttle on engines
						PartmoduleModuleEnginesAJERotor.engineAccelerationSpeed = 0.0f;
						PartmoduleModuleEnginesAJERotor.engineDecelerationSpeed = 0.0f;

						//set aim to chosen limit thrust
						aim = PartmoduleModuleEnginesAJERotor.thrustPercentage;
					}
					if (pm.ClassName == "ModuleEnginesAJEJet")
					{
						Debug.Log ("21");
						enginemoduletype = 4;
						PartmoduleModuleEnginesAJEJet = (ModuleEnginesAJEJet)pm;

						//store original values before engine control takeover
						StoredOuseEngineResponseTime = PartmoduleModuleEnginesAJEJet.useEngineResponseTime;
						StoredOengineAccelerationSpeed = PartmoduleModuleEnginesAJEJet.engineAccelerationSpeed;
						StoredOengineDecelerationSpeed = PartmoduleModuleEnginesAJEJet.engineDecelerationSpeed;

						//This settings must be set to true to be able to control engines with currentThrottle. 
						//Found this with the help of the code of the Throttle Steering mod made by ruffus. 
						PartmoduleModuleEnginesAJEJet.useEngineResponseTime = true;

						//This eliminates the influence of the main throttle on engines
						PartmoduleModuleEnginesAJEJet.engineAccelerationSpeed = 0.0f;
						PartmoduleModuleEnginesAJEJet.engineDecelerationSpeed = 0.0f;

						//set aim to chosen limit thrust
						aim = PartmoduleModuleEnginesAJEJet.thrustPercentage;
					}
				}
			}

			Events["transferToAllEngineOfType"].guiName = "Sync all " + part.partInfo.name;

			booted = true;//boot completed
			Debug.Log ("22");
		}

		private void ChooseMultiModeEngine()
		{
			foreach (PartModule pm in part.Modules)
			{
				if (pm.ClassName == "ModuleEnginesFX")
				{
					ModuleEnginesFX cModuleEnginesFX = (ModuleEnginesFX)pm;
					if (cModuleEnginesFX.engineID == PartmoduleMultiModeEngine.mode)
					{
						PartmoduleModuleEnginesFX = (ModuleEnginesFX)pm;
					}
				}
			}
		}

		[KSPEvent(name = "transferToAllEngineOfType", isDefault = false, guiActive = true, guiName = "Sync all enginetype")]
		public void transferToAllEngineOfType()
		{
			foreach (Part p in vessel.parts)
			{

				if (p.partInfo.name == part.partInfo.name)
				{
					foreach (PartModule pm in p.Modules)
					{
						if (pm.ClassName == "DifferentialThrustEngineModule")
						{
							DifferentialThrustEngineModule aDifferentialThrustEngineModule;
							aDifferentialThrustEngineModule = p.Modules.OfType<DifferentialThrustEngineModule>().FirstOrDefault();

							if (aDifferentialThrustEngineModule.isolated == false)
							{
								aDifferentialThrustEngineModule.levelThrust = levelThrust;
								aDifferentialThrustEngineModule.throttleFloatSelect = throttleFloatSelect;
								aDifferentialThrustEngineModule.CenterThrustMode = CenterThrustMode;
								aDifferentialThrustEngineModule.Events["CycleCenterThrustMode"].guiName = "Center thrust: " + aDifferentialThrustEngineModule.CenterThrustMode;
								aDifferentialThrustEngineModule.aim = aim;
								aDifferentialThrustEngineModule.isolated = isolated;

								foreach (PartModule pmt in p.Modules)
								{
									if (pmt.ClassName == "ModuleEngines")
									{
										ModuleEngines aModuleEngines;
										aModuleEngines = (ModuleEngines)pmt;

										aModuleEngines.thrustPercentage = PartmoduleModuleEngines.thrustPercentage;
									}
									if (pmt.ClassName == "ModuleEnginesFX")
									{
										ModuleEnginesFX aModuleEnginesFX;
										aModuleEnginesFX = (ModuleEnginesFX)pmt;

										aModuleEnginesFX.thrustPercentage = PartmoduleModuleEnginesFX.thrustPercentage;
									}
									if (pmt.ClassName == "ModuleEnginesAJERotor")
									{
										ModuleEnginesAJERotor aModuleEnginesAJERotor;
										aModuleEnginesAJERotor = (ModuleEnginesAJERotor)pmt;

										aModuleEnginesAJERotor.thrustPercentage = PartmoduleModuleEnginesAJERotor.thrustPercentage;
									}
									if (pmt.ClassName == "ModuleEnginesAJEJet")
									{
										ModuleEnginesAJEJet aModuleEnginesAJEJet;
										aModuleEnginesAJEJet = (ModuleEnginesAJEJet)pmt;

										aModuleEnginesAJEJet.thrustPercentage = PartmoduleModuleEnginesAJEJet.thrustPercentage;
									}
								}
							}
						}
					}
				}
			}
		}
	}
}

 

...and here is the what I got in my log.

Spoiler


0
UnityEngine.Debug:Internal_Log(Int32, String, Object)
UnityEngine.Debug:Log(Object)
DifferentialThrustMod.DifferentialThrustEngineModule:OnUpdate() (at /home/mcfadds2/Documents/ThrottleControl/ThrottleControl/DifferentialThrustEngineModule.cs:76)
Part:ModulesOnUpdate()
Part:Update()
 
(Filename: /home/mcfadds2/Documents/ThrottleControl/ThrottleControl/DifferentialThrustEngineModule.cs Line: 76)

11
UnityEngine.Debug:Internal_Log(Int32, String, Object)
UnityEngine.Debug:Log(Object)
DifferentialThrustMod.DifferentialThrustEngineModule:OnUpdate() (at /home/mcfadds2/Documents/ThrottleControl/ThrottleControl/DifferentialThrustEngineModule.cs:170)
Part:ModulesOnUpdate()
Part:Update()
 
(Filename: /home/mcfadds2/Documents/ThrottleControl/ThrottleControl/DifferentialThrustEngineModule.cs Line: 170)

12
UnityEngine.Debug:Internal_Log(Int32, String, Object)
UnityEngine.Debug:Log(Object)
DifferentialThrustMod.DifferentialThrustEngineModule:OnUpdate() (at /home/mcfadds2/Documents/ThrottleControl/ThrottleControl/DifferentialThrustEngineModule.cs:177)
Part:ModulesOnUpdate()
Part:Update()
 
(Filename: /home/mcfadds2/Documents/ThrottleControl/ThrottleControl/DifferentialThrustEngineModule.cs Line: 177)

14
UnityEngine.Debug:Internal_Log(Int32, String, Object)
UnityEngine.Debug:Log(Object)
DifferentialThrustMod.DifferentialThrustEngineModule:OnUpdate() (at /home/mcfadds2/Documents/ThrottleControl/ThrottleControl/DifferentialThrustEngineModule.cs:212)
Part:ModulesOnUpdate()
Part:Update()
 
(Filename: /home/mcfadds2/Documents/ThrottleControl/ThrottleControl/DifferentialThrustEngineModule.cs Line: 212)

16
UnityEngine.Debug:Internal_Log(Int32, String, Object)
UnityEngine.Debug:Log(Object)
DifferentialThrustMod.DifferentialThrustEngineModule:OnUpdate() (at /home/mcfadds2/Documents/ThrottleControl/ThrottleControl/DifferentialThrustEngineModule.cs:231)
Part:ModulesOnUpdate()
Part:Update()
 
(Filename: /home/mcfadds2/Documents/ThrottleControl/ThrottleControl/DifferentialThrustEngineModule.cs Line: 231)

0
UnityEngine.Debug:Internal_Log(Int32, String, Object)
UnityEngine.Debug:Log(Object)
DifferentialThrustMod.DifferentialThrustEngineModule:OnUpdate() (at /home/mcfadds2/Documents/ThrottleControl/ThrottleControl/DifferentialThrustEngineModule.cs:76)
Part:ModulesOnUpdate()
Part:Update()
 
(Filename: /home/mcfadds2/Documents/ThrottleControl/ThrottleControl/DifferentialThrustEngineModule.cs Line: 76)

NullReferenceException: Object reference not set to an instance of an object
  at DifferentialThrustMod.DifferentialThrustEngineModule.OnUpdate () [0x000bb] in /home/mcfadds2/Documents/ThrottleControl/ThrottleControl/DifferentialThrustEngineModule.cs:108 
  at Part.ModulesOnUpdate () [0x00000] in <filename unknown>:0 
  at Part.Update () [0x00000] in <filename unknown>:0 
 
(Filename: /home/mcfadds2/Documents/ThrottleControl/ThrottleControl/DifferentialThrustEngineModule.cs Line: 108)

 

After that it just repeats over and over. 

Hmm looking closer line 108 is this

	if (PartmoduleModuleEnginesFX.throttleLocked == true)

And the log says that is where the NRE came from? IDK how that would happen can anyone advise?

Edited by Svm420
Link to comment
Share on other sites

Your sequence of if (engineModuleType == xxx) statements don't use else if so, for all values except 4 it is also running the bit in the final else and trying to use PartmoduleModuleEnginesFX which has only been set up when engineModuleType is 1 (or when you have multiple engine modules of different types).

You would probably be better off changing all the lists of if (engineModuleType) statements to switch (engineModuleType) instead.

Edit: also, testing if a boolean value is equal to true is pointless, e.g. you can just write:

if (PartmoduleModuleEnginesFX.throttleLocked)

Edited by Padishar
Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...