wizzlebippi Posted November 1, 2015 Share Posted November 1, 2015 How would I get the quantity of a resource for the active vessel as well as the quantity of a resource in a given part? I know this is possible with the RequestResource method, but am struggling to pull it off. Thanks Link to comment Share on other sites More sharing options...
wasml Posted November 2, 2015 Share Posted November 2, 2015 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 More sharing options...
wizzlebippi Posted November 2, 2015 Author Share Posted November 2, 2015 I figured out finding the quantity of a resource for the vessel:Definition = PartResourceLibrary.Instance.GetDefinition("string")Amount = this.vessel.GetActiveResource(Definition).amountFor 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 More sharing options...
Crzyrndm Posted November 2, 2015 Share Posted November 2, 2015 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 More sharing options...
wizzlebippi Posted November 2, 2015 Author Share Posted November 2, 2015 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 More sharing options...
Crzyrndm Posted November 2, 2015 Share Posted November 2, 2015 part.Resources.Get(resourceID).amount // For the current quantity in a partpart.Resources.Get(resourceID).maxAmount // For the maximum a part can holdpart.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 accessibleYou 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 More sharing options...
Fengist Posted November 2, 2015 Share Posted November 2, 2015 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 fixedupdatedouble 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 More sharing options...
Crzyrndm Posted November 2, 2015 Share Posted November 2, 2015 You can then in update or fixedupdateYou 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 More sharing options...
wizzlebippi Posted November 3, 2015 Author Share Posted November 3, 2015 part.Resources.Get(resourceID).amount // For the current quantity in a partpart.Resources.Get(resourceID).maxAmount // For the maximum a part can holdpart.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 accessibleYou can get the resourceID from the instance of PartResourceLibrary by name or from any propellant / resource you have access toThat's it, thanks. I have more math issues to work out, but this gets me much closer. 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