TaranisElsu Posted June 25, 2013 Share Posted June 25, 2013 So I tried to copy the code by Fel:http://forum.kerbalspaceprogram.com/showthread.php/25305-0-20-2-Vanguard-Technologies-EVA-parachutes-Finally-updated-%28Jun-4%29?p=442982&viewfull=1#post442982like this:[KSPAddon(KSPAddon.Startup.MainMenu, true)]public class AddEvaModule : MonoBehaviour{ public void Awake() { ConfigNode node = new ConfigNode("MODULE"); node.AddValue("name", "MyEvaModule"); try { PartLoader.getPartInfoByName("kerbalEVA").partPrefab.AddModule(node); } catch (Exception ex) { Debug.LogError("TAC AddEvaModule [" + Time.time + "]: Failed to add the part module to EVA: " + ex.Message + "\n" + ex.StackTrace); } }}public class MyEvaModule : PartModule{ public override void OnAwake() { Debug.Log("TAC MyEvaModule [" + this.GetInstanceID().ToString("X") + "][" + Time.time + "]: OnAwake"); base.OnAwake(); } public override void OnStart(PartModule.StartState state) { Debug.Log("TAC MyEvaModule [" + this.GetInstanceID().ToString("X") + "][" + Time.time + "]: OnStart"); base.OnStart(state); } public override void OnLoad(ConfigNode node) { Debug.Log("TAC MyEvaModule [" + this.GetInstanceID().ToString("X") + "][" + Time.time + "]: OnLoad"); base.OnLoad(node); } public override void OnSave(ConfigNode node) { Debug.Log("TAC MyEvaModule [" + this.GetInstanceID().ToString("X") + "][" + Time.time + "]: OnSave"); base.OnSave(node); } public override void OnFixedUpdate() { base.OnFixedUpdate(); }}but I keep getting this error:[Error]: TAC AddEvaModule [4.394309]: Failed to add the part module to EVA: Object reference not set to an instance of an object at Part.AddModule (System.String moduleName) [0x00000] in <filename unknown>:0 at Part.AddModule (.ConfigNode node) [0x00000] in <filename unknown>:0 at AddEvaModule.Awake () [0x00000] in <filename unknown>:0 Am I doing something wrong? Or does that not work anymore?I would be content just adding resources to the EVA instead of a PartModule, if that would work. But I cannot get that to work either Link to comment Share on other sites More sharing options...
TaranisElsu Posted June 25, 2013 Author Share Posted June 25, 2013 So I got it to work, but it is still throwing an exception:[KSPAddon(KSPAddon.Startup.MainMenu, true)]public class AddEvaModule : MonoBehaviour{ private static bool initialized = false; public void Update() { if (!initialized) { initialized = true; AddModule(); AddResources(); } } private void AddModule() { try { ConfigNode node = new ConfigNode("MODULE"); node.AddValue("name", "MyEvaModule"); var partInfo = PartLoader.getPartInfoByName("kerbalEVA"); Debug.Log("TAC AddEvaModule [" + Time.time + "]: Part info = " + partInfo); var prefab = partInfo.partPrefab; Debug.Log("TAC AddEvaModule [" + Time.time + "]: Prefab = " + prefab); var module = prefab.AddModule(node); Debug.Log("TAC AddEvaModule [" + Time.time + "]: Module = " + module); } catch (Exception ex) { Debug.LogError("TAC AddEvaModule [" + Time.time + "]: Failed to add the part module to EVA: " + ex.Message + "\n" + ex.StackTrace); } } private void AddResources() { try { var partInfo = PartLoader.getPartInfoByName("kerbalEVA"); Debug.Log("TAC AddEvaModule [" + Time.time + "]: Part info = " + partInfo); var prefab = partInfo.partPrefab; Debug.Log("TAC AddEvaModule [" + Time.time + "]: Prefab = " + prefab); PartResource resource = prefab.gameObject.AddComponent<PartResource>(); resource.SetInfo(PartResourceLibrary.Instance.resourceDefinitions["MyResource"]); resource.amount = 10; resource.maxAmount = 20; Debug.Log("TAC AddEvaModule [" + Time.time + "]: resource = " + resource); prefab.Resources.list.Add(resource); Debug.Log("TAC AddEvaModule [" + Time.time + "]: added resource"); } catch (Exception ex) { Debug.LogError("TAC AddEvaModule [" + Time.time + "]: Failed to add resources to EVA: " + ex.Message + "\n" + ex.StackTrace); } }}public class MyEvaModule : PartModule{ private float lastUpdate; private float lastFixedUpdate; public override void OnAwake() { Debug.Log("TAC MyEvaModule [" + this.GetInstanceID().ToString("X") + "][" + Time.time + "]: OnAwake"); base.OnAwake(); lastUpdate = 0.0f; lastFixedUpdate = 0.0f; } public override void OnStart(PartModule.StartState state) { Debug.Log("TAC MyEvaModule [" + this.GetInstanceID().ToString("X") + "][" + Time.time + "]: OnStart"); base.OnStart(state); } public override void OnLoad(ConfigNode node) { Debug.Log("TAC MyEvaModule [" + this.GetInstanceID().ToString("X") + "][" + Time.time + "]: OnLoad"); base.OnLoad(node); } public override void OnSave(ConfigNode node) { Debug.Log("TAC MyEvaModule [" + this.GetInstanceID().ToString("X") + "][" + Time.time + "]: OnSave"); base.OnSave(node); } public override void OnUpdate() { base.OnUpdate(); float now = Time.time; if ((now - lastUpdate) > 5.0) { Debug.Log("TAC MyEvaModule [" + this.GetInstanceID().ToString("X") + "][" + Time.time + "]: OnUpdate"); lastUpdate = now; } } // Never gets called.... public override void OnFixedUpdate() { base.OnFixedUpdate(); float now = Time.time; if ((now - lastFixedUpdate) > 5.0) { Debug.Log("TAC MyEvaModule [" + this.GetInstanceID().ToString("X") + "][" + Time.time + "]: OnFixedUpdate"); lastFixedUpdate = now; } }}The error:[Log]: TAC AddEvaModule [3.922859]: Part info = AvailablePart[Log]: TAC AddEvaModule [3.922859]: Prefab = kerbalEVA_RD (Part)[Error]: TAC AddEvaModule [3.922859]: Failed to add the part module to EVA: Object reference not set to an instance of an object at Part.AddModule (System.String moduleName) [0x00000] in <filename unknown>:0 at Part.AddModule (.ConfigNode node) [0x00000] in <filename unknown>:0 at AddEvaModule.AddModule () [0x00000] in <filename unknown>:0 It seems to be good enough for now. The exception does not seem to effect anything. And I got resources to work, see the code. Link to comment Share on other sites More sharing options...
Fel Posted June 26, 2013 Share Posted June 26, 2013 Although you probably don't need it, http://forum.kerbalspaceprogram.com/showthread.php/26446-0-20-2-KerbalEVA-Lifesupport(Yeah, resource code looks right so nvm) Link to comment Share on other sites More sharing options...
DSSP Posted October 9, 2013 Share Posted October 9, 2013 basicly the error says exactly whats wrong.partInfo.partPrefab is not an instance of an object (the object is not instantiated).KerbalEVA eva = FlightGlobals.ActiveVessel.GetComponent<KerbalEVA>(); <---- this is an instance of an objectalso KerbalEVA is a PartModule are you sure you want to add a module to a module ?find the part that holds the module and add the module to the part. Link to comment Share on other sites More sharing options...
TaranisElsu Posted October 10, 2013 Author Share Posted October 10, 2013 basicly the error says exactly whats wrong.partInfo.partPrefab is not an instance of an object (the object is not instantiated).KerbalEVA eva = FlightGlobals.ActiveVessel.GetComponent<KerbalEVA>(); <---- this is an instance of an objectalso KerbalEVA is a PartModule are you sure you want to add a module to a module ?find the part that holds the module and add the module to the part.I cannot use "FlightGlobals.ActiveVessel" because I am not adding the PartModule during the flight scene. I am trying to add the module to the prefab part so that it is there whenever an EVA is started/created. And partInfo.partPrefab does point to an object. It is trying to use something in the AddModule method that is null.This is what I figured out (thanks to Fel):https://github.com/taraniselsu/TacLifeSupport/blob/master/Source/AddLifeSupport.cs#L187which is called from:https://github.com/taraniselsu/TacLifeSupport/blob/master/Source/AddLifeSupport.cs#L176Note that "kerbalEVA" is the name of the part, while "KerbalEVA" is the name of the PartModule. I am finding the "kerbalEVA" part and adding my PartModule to it. It works... but it still throws an exception which does not seem to cause any problems.I would like to know a better way to do it, but I have not figured anything out yet. Nor have I seen anything from anyone else. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now