Jump to content

Are there guarantees about atomicity of Update vs OnFlyByWire ?


Recommended Posts

I'm trying to debug a problem that I think might be in the kOS mod that I didn't write, so I'm looking through someone else's code and trying to work out a few things, but I'm very very new to mod writing, so I have a basic question about the mod API:

It's possible for a mod writer to make "Update" hooks that get called by KSP on a regular fast basis.

It's also possible for a mod writer to make "OnFlyByWire" hooks that get called by KSP on a regular basis when KSP wants to ask you mod "tell me how to move the ship controls".

My question is this: Are you guaranteed that it's impossible for KSP to call your OnFlyByWire hook in the middle of you executing a call to your Update hook?

I see a case in the mod where a rather important variable used in flight control is temporarily in a nulled-out state during a particular small window of time during the execution of the Update hook, but then is set back to a good state by the end of the Update hook. If the OnFlyByWire hook was called in the midst of that, it could try to fly the ship using that temporarily null value, but I'm not sure if that's a problem or not because I don't know when the hooks are actually called. If the calling of all those hooks is single-threaded such that you are guaranteed you'll never get an OnFlyByWire call in the midst of an Update call then this isn't a problem. But if they get triggered independently in separate threads, then this small window of time could be a problem.

Link to comment
Share on other sites

Yes, the game logic in unity is single threaded.

because I don't know when the hooks are actually called.

This is exactly the problem. Why don't you know? You should plaster your source with Debug.Log(...)'s until you have verified everything is working as intended.

Link to comment
Share on other sites

  • 1 month later...
Whether or not Unity is threaded is irrelevant. What matters is whether or not KSP is threaded. KSP is calling the update and the flybywire, not Unity.

MonoBehaviour.Update() (and almost all other MonoBehaviour functions) are actually called by Unity using some reflection magic. I don't know what's causing your issue, but keep in mind that your callback is one of many and any previous code reacting to OnFlyByWire is also capable of causing the variable to be null

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