Jump to content

Eva Kerbal - Duplicates on reboarding vessel


Recommended Posts

My EVA Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using KSP.IO;
using KSP;


namespace IFILifeSupport
{
public class IFILifeSupportEVA : PartModule // Life Support Consumption Module for EVA
{
private PluginConfiguration cfg = PluginConfiguration.CreateForType<IFILifeSupportEVA>();

public bool initialized = false;
private static double Rate_Per_Kerbal = 0.000046; // Per Second use based on Life support requirements from Life Support wiki
private int IFITimer; // Used to track LS use on inactive eva'd kerbals
private float IFICWLS = 5; // Used to track Kerbal death chance after life support runs out.
// Right Click Info display for Part
[KSPField(guiActive = true, guiName = "Life Support Pack Status", isPersistant = false)]
public string lifeSupportStatus;
[KSPField(guiActive = true, guiName = "Life Support ", guiUnits = " HOURS ", guiFormat = "F0", isPersistant = false)]
public int displayRate;



public override void OnLoad(ConfigNode node)
{
IFIDBLog(" OnLoad(): OnLoad Fired");
//if (initialized == true)
//{
// cfg.load();
// Vessel active = part.vessel;
// if (active)
// {
// string IFICfgLabel = "IFITimer" + active.id;
// IFITimer = cfg.GetValue<int>(IFICfgLabel);
// }
//}
}


public override void OnSave(ConfigNode node)
{

base.OnSave(node);
IFIDBLog(" OnSave() Fired");
Vessel active = part.vessel;
if (active)
{
cfg.load();
IFITimer = Convert.ToInt32(Planetarium.fetch.time);
IFIDBLog(" Init Eva Values");

string IFICfgLabel = "IFITimer" + active.id;
cfg[IFICfgLabel] = IFITimer;
IFICfgLabel = "IFIEVAPACK" + active.id;
double EvaLSRem = IFIGetAllResources("LifeSupport");
cfg[IFICfgLabel] = EvaLSRem;

IFICfgLabel = "IFIBool" + active.id;
cfg[IFICfgLabel] = 1;
cfg.save();
}
}
public override void OnInactive() // Called when part is destroyed
{
IFIDBLog(" EVA Inactive Fired");
base.OnInactive();

}

public override void OnUpdate()
{
base.OnUpdate();
Vessel active = part.vessel;
if (active.isEVA == true)
{
IFIDBLog(" EVA ACCESSED");
lifeSupportStatus = "Active";
displayRate = 4;
if (!initialized) Initialize();
int TTtest = Convert.ToInt32(Planetarium.fetch.time) - IFITimer;
double ResourceAval = IFIGetAllResources("LifeSupport");
displayRate = Convert.ToInt16(((ResourceAval / Rate_Per_Kerbal) / 60 / 60));
if (displayRate > 0 && displayRate < 1)
{
lifeSupportStatus = "CAUTION ";
}
else if (displayRate <= 0)
{
lifeSupportStatus = "Warning!";
}
if (TTtest >= 180) // only consume resources every 3 mins try to control lag
{
double resourceRequest;
double resourceReturn;
resourceRequest = Rate_Per_Kerbal * TTtest;//* TimeWarp.deltaTime; not sure if timewarp is needed but need to check.
resourceReturn = this.part.RequestResource("LifeSupport", resourceRequest);
IFIDBLog(" resource Avalible == " + Convert.ToString(ResourceAval));
IFIDBLog(" LS Resource Return == " + Convert.ToString(resourceReturn));

if (ResourceAval < resourceRequest)
{
resourceRequest = ResourceAval;
resourceReturn = this.part.RequestResource("LifeSupport", resourceRequest);

}

if (resourceReturn <= 0 )
{
IFIDBLog(" Crew has no LS Remaining ");
TimeWarp.SetRate(0, true);
CrewTest(); // Check for crew death
}

IFITimer = Convert.ToInt32(Planetarium.fetch.time);
if (active)
{
cfg.load();

IFITimer = Convert.ToInt32(Planetarium.fetch.time);
IFIDBLog(" Init Eva Values");

string IFICfgLabel = "IFITimer" + active.id;
cfg[IFICfgLabel] = IFITimer;
IFICfgLabel = "IFIEVAPACK" + active.id;
double EvaLSRem = IFIGetAllResources("LifeSupport");
cfg[IFICfgLabel] = EvaLSRem;

IFICfgLabel = "IFIBool" + active.id;
cfg[IFICfgLabel] = 1;
cfg.save();
}
}


}

}

private void Initialize()
{
double EvaLSRem;
cfg.load();
Vessel active = part.vessel;
GameEvents.onCrewBoardVessel.Add(OnCrewBoardVessel);
// resourceReturn = this.part.resor ("LifeSupport", resourceRequest);

string IFICfgLabel = "IFIBool" + active.id;
int IFIBool = cfg.GetValue<int>(IFICfgLabel);
if (IFIBool == 1)
{
IFICfgLabel = "IFITimer" + active.id;
IFITimer = cfg.GetValue<int>(IFICfgLabel);
IFICfgLabel = "IFIEVAPACK" + active.id;
EvaLSRem = cfg.GetValue<double>(IFICfgLabel);
}
else
{
IFITimer = Convert.ToInt32(Planetarium.fetch.time);
IFIDBLog(" Init Eva Values");

IFICfgLabel = "IFITimer" + active.id;
cfg[IFICfgLabel] = IFITimer;
IFICfgLabel = "IFIEVAPACK" + active.id;
cfg[IFICfgLabel] = Rate_Per_Kerbal * 60 * 60 * 4;
EvaLSRem = Rate_Per_Kerbal * 60 * 60 * 4;
IFICfgLabel = "IFIBool" + active.id;
cfg[IFICfgLabel] = 1;
cfg.save();
}
ConfigNode node = new ConfigNode("RESOURCE");
node.AddValue("name", "LifeSupport");
node.AddValue("maxAmount", Rate_Per_Kerbal * 60 * 60 * 4);
node.AddValue("amount", EvaLSRem);
part.AddResource(node);
initialized = true;
IFIDBLog(" Init(): OnInit Fired");
}


private void CrewTest()
{
float rand;
ProtoCrewMember iCrew;
for (int i = 0; i < part.protoModuleCrew.Count; i++)
{
rand = UnityEngine.Random.Range(0.0f, 100.0f);
if (IFICWLS > rand)
{
iCrew = part.protoModuleCrew[i];
part.RemoveCrewmember(iCrew);// Remove crew from part
iCrew.Die();// Kill crew after removal or death will reset to active.
IFIDBLog("Kerbal Killed due to no LS - " + iCrew.name);
}
}
IFICWLS += 5; // Increase chance of death on next check.
}

private double IFIGetAllResources(string IFIResource)
{
double IFIResourceAmt = 0.0;
Vessel active = part.vessel;
foreach (Part p in active.parts)
{
foreach (PartResource pr in p.Resources)
{
if (pr.resourceName.Equals(IFIResource))
{
IFIResourceAmt += pr.amount;
break;
}
}
}
return IFIResourceAmt;
}

private void OnCrewBoardVessel(GameEvents.FromToAction<Part, Part> action)
{
double IFIResourceAmt = 0.0;
foreach (Part p in action.from.vessel.parts)
{
foreach (PartResource pr in p.Resources)
{
if (pr.resourceName.Equals("LifeSupport"))
{
IFIResourceAmt += pr.amount;
break;
}
}
}
double resourceReturn = action.to.RequestResource("LifeSupport", 0.0 - IFIResourceAmt);
Debug.Log("IFI Life Support Message: EVA - Ended - " + action.from.name + " Boarded Vessel");

}

private void IFIDBLog(string IMessage) // Debug Strings to Log File
{
#if DEBUG
Debug.Log("IFI Life Support Message: EVA -" + IMessage);
#endif
}


}

}

When this code block is not commented out. if I send a Kerbal on eva then try and reboard. he stays on the ladder and a copy of him shows back up in the pod. Not sure what I'm missing but I'm sure it's simple.

        private void OnCrewBoardVessel(GameEvents.FromToAction<Part, Part> action)
{
double IFIResourceAmt = 0.0;
foreach (Part p in action.from.vessel.parts)
{
foreach (PartResource pr in p.Resources)
{
if (pr.resourceName.Equals("LifeSupport"))
{
IFIResourceAmt += pr.amount;
break;
}
}
}
double resourceReturn = action.to.RequestResource("LifeSupport", 0.0 - IFIResourceAmt);
Debug.Log("IFI Life Support Message: EVA - Ended - " + action.from.name + " Boarded Vessel");

}

Link to comment
Share on other sites

I would bet on an exception in your code preventing KSP from finishing the boarding logic. Have you checked your logs?

You are correct due to that parsing int spam in log I missed a null reference exception line. After using try and catch I found the problem.

Which was simple as I surmised. Since a Kerbal on eva is a 1 part vessel. I did not need the for-next loop looking for multiple parts. which was copied from my code running on a pod for resource counts. removing that loop fixed the null reference.

Link to comment
Share on other sites

  • 5 months later...
You are correct due to that parsing int spam in log I missed a null reference exception line. After using try and catch I found the problem.

Which was simple as I surmised. Since a Kerbal on eva is a 1 part vessel. I did not need the for-next loop looking for multiple parts. which was copied from my code running on a pod for resource counts. removing that loop fixed the null reference.

I just started having this problem too. KSP .25 with MecJeb2 and MCEC. It only seams to happen when Im on a rescue mission. Any other time i can EVA and reboard with no problem. I dont know how to find much less edit the code! Any help will be appreciated!

Link to comment
Share on other sites

me too, a solution would be nice

there is not "the solution"!

I mean it's stated above allready - but some seem to need it twice..

So here we go again:

you both have a mod installed that bugs the boarding process - find out wich one and go to the developper or update that mod or remove that mod.

If you inform the autor of that mod, the chances are it'll get fixed..

if not, nothing will happen and you will keep that issue.

Open your log, find the culprit - done..

Link to comment
Share on other sites

  • 1 year later...
Guest
This topic is now closed to further replies.
×
×
  • Create New...