Jump to content

How do i check whether a part has a specific resource in it?


Recommended Posts

So anyways, im working on BDA and what i need to do is 2 fold:

First when a specific event occurs (part gets hit by bullet, pretty obvious), i need to check whether a part contains a specific resource type in it at all (resource name is "Armor").

Second, if the resource is present, i need to set a variable to the amount of that resource present in the part (so if there is 30 units of the resource the variable will have a value of 30 after operation), (variable is called "thickness").

Also, i would prefer to do this check in the least resource intensive method possible, since the mod will potentially be doing this check at a very high frequency (try doing penetration checks at over 5000 times a minute for autocannons).

 

I know this is probably really simple to do, but i havent done any coding for KSP for over 2 years, and i sorta forgot how to do all of these seemingly basic things :(, that and probably the methods for doing it have changed

Edited by panzer1b
Link to comment
Share on other sites

If you're doing it from within a PartModule:

var res = this.part.Resources.FirstOrDefault(r => r.resourceName == "Armor");
double thickness = res?.amount; // will be 0 if part doesn't have any storage for the resource or if storage is empty

You should be able to cache the result of the first line so future checks only need to read the value since the presence or absence of tankage for the resource generally doesn't change unless you need to account for flight switchable tanks.

Edited by Aelfhe1m
changed to specific resource
Link to comment
Share on other sites

Or something like this       

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

        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 += GetResourceAmount(mypart, resourceName);
                }
            }
            return amount;
        }

        public static double GetResourceMax(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 += mypart.Resources.Get(resource.id).maxAmount;
                }
            }
            return amount;
        }

Now, this assumes you don't know the resource id and instead are using the resource name.  It may be more efficient to get the resource ID first, store it and pass that to the functions so it doesn't have to get that every pass.  I'm sure with some testing you can speed these up a bit.

You'd call these functions as such:

GetResourceTotal(FlightGlobals.ActiveVessel, "ElectricCharge");

 

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