Jump to content

How to get quantity of a resource


Recommended Posts

There is a vessel.getActiveResource(Resource def) but I'm not clear on what that does or how to use it. If I needed to do that right now I'd probably use List<PartResource> = part.Resources.GetAll(int id) then sum the contents of the list.

Link to comment
Share on other sites

I figured out finding the quantity of a resource for the vessel:

Definition = PartResourceLibrary.Instance.GetDefinition("string")

Amount = this.vessel.GetActiveResource(Definition).amount

For a part, I'm still stuck. Here's my line of code:

Amount = this.part.RequestResource("string", double)

Is this really the right approach? Is there something else that takes the place of double to convince the RequestResource method to spit out a number?

Link to comment
Share on other sites

What is your problem with part.requestResource? You tell it which resource to use and how much of it and it returns the amount you actually consumed. The only way I can think of to go wrong is forgetting to scale the request quantity by fixedDeltaTime and getting a consumption rate that is much higher than intended

Link to comment
Share on other sites

Apparently my problem is not understanding what RequestResource returns. I was thinking I could get the remaining amount of a resource in a part using it.

Still, if you can right click on a fuel tank and see how much fuel and oxidizer is left, there must be a way to get the remaining quantity of any resource.

Link to comment
Share on other sites

part.Resources.Get(resourceID).amount // For the current quantity in a part
part.Resources.Get(resourceID).maxAmount // For the maximum a part can hold
part.GetConnectedResources(resourceID, flowmode, resources) // returns the quantities of all parts that a part can request fuel from given a flowmode (by reference, "resources" is the resulting list) . The sum of the amounts in resources is the total quantity accessible

You can get the resourceID from the instance of PartResourceLibrary by name or from any propellant / resource you have access to

Link to comment
Share on other sites

Here's some functions I use in the plugin I use for my submarine that may help. They made my life easier.


//gets the id of the named resource found in a part
public static int GetResourceID(this Part part, string resourceName)
{
PartResourceDefinition resource = PartResourceLibrary.Instance.GetDefinition(resourceName);
return resource.id;
}

//gets the amount of resource in one part.
public static double GetResourceAmount(this Part part, string resourceName)
{
PartResourceDefinition resource = PartResourceLibrary.Instance.GetDefinition(resourceName);
return part.Resources.Get(resource.id).amount;
}

//gets how much empty resource space the part has
public static double GetResourceSpace(this Part part, string resourceName)
{
PartResourceDefinition resource = PartResourceLibrary.Instance.GetDefinition(resourceName);
double amt = part.Resources.Get(resource.id).amount;
double max = part.Resources.Get(resource.id).maxAmount;
return max - amt;
}

//returns the first part found with a named resource. I use this to locate a part that I'm going to use All_Vessel flow mode on.
public static Part GetResourcePart(Vessel v, string resourceName)
{
foreach (Part mypart in v.parts)
{
if (mypart.Resources.Contains(resourceName))
{
return mypart;
}
}
return null;
}

//gets the total amount of a resource found on the entire vessel
public static double GetResourceTotal(Vessel v, string resourceName)
{
PartResourceDefinition resource = PartResourceLibrary.Instance.GetDefinition(resourceName);
double amount = 0;
foreach (Part mypart in v.parts)
{
if (mypart.Resources.Contains(resourceName))
{
amount += MPFunctions.GetResourceAmount(mypart, resourceName);
}
}
return amount;
}

To find a part with a resource: Here, I'm looking for exactly two parts on the boat that have my ballast resource.


foreach (Part mypart in this.vessel.parts)
{
if (mypart.Resources.Contains("CompressedWater"))
{
if (rescount == 0)
{ Part1 = mypart; }
if (rescount == 1)
{ Part2 = mypart; }
rescount++;
}
}

To consume electric charge you'd do this:

In the start:


batid = MPFunctions.GetResourceID(Part1, "ElectricCharge");

where batid is an int and Part1 is a Part.

You can then in update or fixedupdate


double rFlow = rPart.RequestResource(batid,rConsume,ResourceFlowMode.ALL_VESSEL);

where rConsume is the amount of charge you're requesting to use and rFlow is how much it actually consumed.

Link to comment
Share on other sites

You can then in update or fixedupdate

You should always be using fixedUpdate for resource consumption since you need to scale it by fixed delta time to get a rate per game second. The rate of calls to Update (using delta time) is totally independent to how fast the game time is moving so if you use that you will get resource consumption that varies depending on the vessel and what you are doing with it

Link to comment
Share on other sites

part.Resources.Get(resourceID).amount // For the current quantity in a part
part.Resources.Get(resourceID).maxAmount // For the maximum a part can hold
part.GetConnectedResources(resourceID, flowmode, resources) // returns the quantities of all parts that a part can request fuel from given a flowmode (by reference, "resources" is the resulting list) . The sum of the amounts in resources is the total quantity accessible

You can get the resourceID from the instance of PartResourceLibrary by name or from any propellant / resource you have access to

That's it, thanks. I have more math issues to work out, but this gets me much closer.

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