Jump to content

ElectricCharge drain fixed vs per second?


Recommended Posts

Hello,

I have a question about how the game code handles draining ElectricCharge (or any resource for that matter) on a fixed, one-time versus per second basis. Currently, I have a naive understanding that everything operates around the part.RequestResource(string resourceName, double amntToDrain) method.

Is this the only method I need to reliably do a one-shot, fire-and-forget drain of any resource? There are no edge cases to consider or anything else which I need to write more complex wrapper code for? I ask because I've taken a look at the Github code for some other plugins, namely KSP Interstellar-Extended, which uses some very complex wrapper classes based on Open Resource System to encapsulate this logic, but then other mods, like the NearFuture mods seem to just use part.RequestResource.

Also, in addition to a one-time, fire-and-forget way of draining a resource, how does one reliably tell the game engine to drain a resource each second of game time? For example, the stock SAS module requires 5 EC/sec (I'm assuming this 'per second' is per second of game time?) in order to function. If I want to make a custom module which also drains 5 EC/sec of game time, how would I reliably do this? From the research I have done, it seems that it's as simple as doing part.RequestResource(string resourceName, double amountToDrain * TimeWarp.fixedDeltaTime) in the FixedUpdate method? So something like:

public void FixedUpdated()
{
	double consumeCharge = 5 * TimeWarp.fixedDeltaTime;	   // 5 EC/second
	double requested = part.RequestResource("ElectricCharge", consumeCharge);
	...
}

Is that really are there is to it for draining a resource per second? There are no other considerations or edge cases one needs to write custom logic for? I would greatly appreciate any help someone may provide about this.

Link to comment
Share on other sites

4 hours ago, trias702 said:

Is that really are there is to it for draining a resource per second?

Yes. Just be aware that the returned value may be less than what you asked for since the ressource may be exhausted.

Link to comment
Share on other sites

9 hours ago, sarbian said:

Yes. Just be aware that the returned value may be less than what you asked for since the ressource may be exhausted.

Thank you. And I am correct that any resource drain per second should only ever be done in FixedUpdate?

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