Jump to content

[SOLVED] Resource Manipulation Help


Recommended Posts

I have started writing my first plugin for my mod, Impossible Innovations, and I'm adding intakes for collecting hydrogen and a converter for converting it into deuterium or tritium. But I ran into a problem:

Everything works perfectly, but I have been manipulating resources with:


public override void OnUpdate()
{
base.OnUpdate();

this.part.RequestResource("Hydrogen", -1);
}

But the code above is highly dependent on the set Delta Time and it is not affected by time warp. Also, when using physical time warp the rate goes down by a factor of 2 for each warp step.

I want to know how to manipulate resources based on ingame-seconds, not delta seconds. Can anyone help me out on this?

Edited by jandcando
NOW its solved
Link to comment
Share on other sites

(-X * Timewarp.fixedDeltaTime) is what you want (where X is units per second of UT), and you need to do this in OnFixedUpdate (which is done per tick) rather than OnUpdate, which is done per visual frame.

Ok I'll give that a try!

Link to comment
Share on other sites

(-X * Timewarp.fixedDeltaTime) is what you want (where X is units per second of UT), and you need to do this in OnFixedUpdate (which is done per tick) rather than OnUpdate, which is done per visual frame.

So this is what I need to do if I want 1 hydrogen per second?


public override void OnFixedUpdate()
{
this.part.RequestResource("Hydrogen", Timewarp.fixedDeltaTime * -1f);

base.OnFixedUpdate();
}

This code isn't being called for some reason, though.

Link to comment
Share on other sites

PartModule-derived class, or a KSPAddon?

If the former make sure it's been Started by KSP (and is active). If the latter, use FixedUpdate().

(That code looks correct to me, for a module)

It's a PartModule extension, and it worked when it was called OnUpdate(), but when I call OnFixedUpdate() it just gets ignored from what I can tell. I'm going to keep experimenting with this.

Link to comment
Share on other sites

Except that activates every PartModule on the part. Not a good practice. Just use FixedUpdate(), you won't need to to that. You'll just need to add something like "if (!HighLogic.LoadedSceneIsFlight) { return; }" at the top but that's all.

I don't see any documentation for FixedUpdate(). If you actually meant OnFixedUpdate, I tried that and it didn't work unless the part was activated.

I'm new to KSP modding, soo sorry if I'm being ignorant right now...

Link to comment
Share on other sites

OnFixedUpdate is run by KSP, only when the part is active (and only works on PartModule-derived classes).

FixedUpdate is run by Unity, always.

I tried this:


public override void FixedUpdate()
{
if (intakeActive == "Active")
{
part.RequestResource("Hydrogen", ((this.part.vessel.atmDensity * -1.4f) - 0.01f) * TimeWarp.fixedDeltaTime);
}
}

This code produced the error:

'ImpossibleInnovations.ModuleIIHydrogenIntake.FixedUpdate()': no suitable method found to override

Am I missing a reference? I have Assembly-CSharp.dll and UnityEngine.dll referenced already.

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