Jump to content

[Question] How to get Vessel ElectricCharge Total?


Recommended Posts

How do I go about retrieving the ElectricCharge Total for the current Vessel?

I've kinda taken it from the ResourceDisplay by the following Method.

var ElecQuery = from item in ResourceDisplay.Instance.resourceItems where item.nameText.text == "ElectricCharge" select item;
_ElectricResource = ElecQuery.First();

Within the ResourceItem is the vesselResourceCurrent and vesselResourceTotal, but I can not grab it until the resource display has been shown. There has got to be a better way to get the information about Resources.

Thanks in advance!

Link to comment
Share on other sites

This is how I do it. Like Faark said, just iterating through all the parts and looking at the electrical charge in it.


double electricCharge = 0;
double electricMax = 0;
Vessel active = FlightGlobals.ActiveVessel;
foreach (Part p in active.parts)
{
if (p.Resources.Contains("ElectricCharge"))
{
foreach (PartResource pr in p.Resources)
{
if (pr.resourceName.Equals("ElectricCharge"))
{
electricCharge += pr.amount;
electricMax += pr.maxAmount;
break;
}
}
}
}

Link to comment
Share on other sites

KethaneGenerator does this:

var resources = Misc.GetConnectedResources(part, "ElectricCharge");
var ratio = resources.Sum(r => r.amount) / resources.Sum(r => r.maxAmount);

Misc.GetConnectedResources is just a convenience wrapper for Part.GetConnectedResources(). This works for other resources, too, and will only look at resources that can be accessed through a RequestResource call.

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