Jump to content

Weird problem with OnFixedUpdate


RCSub

Recommended Posts

Hi everyone

I am quite experienced in C#, so I decided to play around with making my own partmodules. But I am running into trouble getting OnFixedUpdate to trigger.

My code is:


public override void OnFixedUpdate()
{
Utility.DebugLog ("AK_GD", "Fixed update");
}

Utility.DebugLog is just a wrapper function for Debug.Log(string msg);

But it never triggers. I have put similar tests in OnActivate and OnInactivate, and they doesn't trigger either.

OnUpdate works fine, and through that I see that this.enabled is true as well as this.isEnabled

How come OnFixedUpdate is not triggered?

Best regards

rcsub

Link to comment
Share on other sites

Hi everyone

I am quite experienced in C#, so I decided to play around with making my own partmodules. But I am running into trouble getting OnFixedUpdate to trigger.

My code is:


public override void OnFixedUpdate()
{
Utility.DebugLog ("AK_GD", "Fixed update");
}

Utility.DebugLog is just a wrapper function for Debug.Log(string msg);

But it never triggers. I have put similar tests in OnActivate and OnInactivate, and they doesn't trigger either.

OnUpdate works fine, and through that I see that this.enabled is true as well as this.isEnabled

How come OnFixedUpdate is not triggered?

Best regards

rcsub

Part has to be activated. Either force activate, or use FixedUpdate

Link to comment
Share on other sites

Be careful using FixedUpdate. It works, but you may find that certain things break as it runs all the time. A good example is this:

This.vessel is null in the editor, so anything you've got in FixedUpdate that uses something to do with vessel will likely spam the log with null refs. It's not pretty. A much more elegant way to go about it is use OnFixedUpdate, and use the part.ForceActivate() method in OnStart() with a check to see if the bool for HighLogic.LoadedSceneIsFlight is true. Assuming that's what you want, but most of the time this is the case. OnFixedUpdate will not run until you nudge it into action like so, so this is where you be been having trouble.

On mobile and working from memory, but that should point you in roughly the right direction.

Happy coding :)

Disclaimer: I'm a C# newbie, but learned about this the hard way.

Link to comment
Share on other sites

true..

but I've grown used to the methode:


private bool isReady()
{
if (HighLogic.LoadedSceneIsEditor || this.vessel == null)
{
return false;
}
if (this.vessel != FlightGlobals.ActiveVessel)
{
return false;
}
return true;
}

void FixedUpdate()
{
if (!isReady())
{ return; }

//do stuff
}

Link to comment
Share on other sites

Be careful using FixedUpdate. It works, but you may find that certain things break as it runs all the time. A good example is this:

This.vessel is null in the editor, so anything you've got in FixedUpdate that uses something to do with vessel will likely spam the log with null refs. It's not pretty. A much more elegant way to go about it is use OnFixedUpdate, and use the part.ForceActivate() method in OnStart() with a check to see if the bool for HighLogic.LoadedSceneIsFlight is true. Assuming that's what you want, but most of the time this is the case. OnFixedUpdate will not run until you nudge it into action like so, so this is where you be been having trouble.

No, forcing the activation is a bad idea, you make the part and all its module think it was activated when its should no be.

You can check HighLogic.LoadedSceneIsFlight in FixedUpdate too, a simple "if (!HighLogic.LoadedSceneIsFlight) return;" as the first line works fine.

Edit : philotical answer is even cleaner.

Link to comment
Share on other sites

Absolutely. My previous experience has been writing embedded C on low power hardware, so I have an aversion to using computing cycles where they aren't needed. Running and if() fifty times a second doesn't seem to make a lot of sense when I can just activate another method only when I need it and forgo the check every frame. Granted, modern hardware is so powerful it's really not much of an issue, but we each stick to our ways :)

My 2p worth, anyway!

Link to comment
Share on other sites

No, forcing the activation is a bad idea, you make the part and all its module think it was activated when its should no be.

You can check HighLogic.LoadedSceneIsFlight in FixedUpdate too, a simple "if (!HighLogic.LoadedSceneIsFlight) return;" as the first line works fine.

Edit : philotical answer is even cleaner.

Ah, that's interesting. I missed that connotation of forcing activation. Thanks :)

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