MYCRAFTisbest Posted August 13, 2015 Share Posted August 13, 2015 (edited) 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 August 13, 2015 by MYCRAFTisbest Link to comment Share on other sites More sharing options...
Diazo Posted August 13, 2015 Share Posted August 13, 2015 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 More sharing options...
MYCRAFTisbest Posted August 13, 2015 Author Share Posted August 13, 2015 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 More sharing options...
Diazo Posted August 13, 2015 Share Posted August 13, 2015 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 More sharing options...
Padishar Posted August 13, 2015 Share Posted August 13, 2015 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 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