Jump to content

Adding a PartModule to an EVA


Recommended Posts

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#post442982

like 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

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

  • 3 months later...

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 object

also 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

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 object

also 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#L187

which is called from:

https://github.com/taraniselsu/TacLifeSupport/blob/master/Source/AddLifeSupport.cs#L176

Note 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

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...