Jump to content

[SOLVED] Adding resource consumption to a custom PartModule


Recommended Posts

Hello. I'm trying to make a part with a custom module that would need to consume resources (and more importantly) would not function without them.

Could anyone give me some example code or point me in the right direction on how to utilize resources on the same vessel as the part?

Greatly appreciated :)

EDIT: For clarification, I already have a custom resource created, but for the purposes of my question LiquidFuel can be assumed.

Edited by MYCRAFTisbest
Link to comment
Share on other sites

I have not done much with Resources myself, but I do know you need to look at the Part.RequestResource() methods. There are several overloads of that method and you'll have to decide which one works for your case.

Note that in your calculation of how much resource to request, make sure you include TimeWarp.DeltaTime in your calculation so that your resource consumption scales with timewarp correctly.

That should get you started,

D.

Link to comment
Share on other sites

I have not done much with Resources myself, but I do know you need to look at the Part.RequestResource() methods. There are several overloads of that method and you'll have to decide which one works for your case.

Note that in your calculation of how much resource to request, make sure you include TimeWarp.DeltaTime in your calculation so that your resource consumption scales with timewarp correctly.

That should get you started,

D.

Thank you, I think I can figure it out from here. For anyone else who is wondering this, you need to use "part" not "Part"

Link to comment
Share on other sites

Heh, welcome to the KSP API where things are not consistent.

Part is the overarching Part class, it does not actually reference an object in game. When actually initialized, the Part term gets replaced with whatever reference you are using.

part is the actual reference in a partModule to the parent part that partModule is attached to, so part is an actual reference and replaces the Part term to tell KSP which part you are executing the .RequestResource() method on.

Any reference that is a part can use it though, one of the more common ones being just p in a foreach statement. In that case, p.RequestResource() would also work.

A minor detail yes, but I want to clarify that any part reference can request methods in the part class, not just references actually named part. It just happens that on a partModule, part is the reference name of the part that partModule is attached to.

D.

Link to comment
Share on other sites

The RequestResource functions' return value tells you how much it actually managed to do but, for all flow modes except for NO_FLOW, the function will not always manage the whole amount even though there is enough resource (or space if adding) available due to a number of bugs in the underlying code. Until the problems in the function are fixed (hopefully in the next update), you should call the function repeatedly until it returns 0 before you can be sure there really is none left, e.g.


double remaining = demand;
while (abs(remaining) > 0)
{
double got = part.RequestResource(resid, remaining);
if (got == 0)
break;

remaining -= got;
}

// At this point you either got it all if remaining == 0 or you only got some or none if remaining is anything between 0 and demand

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