Jump to content

A better way to update a module after OnStart()


Recommended Posts

I am working on extending the TestFlight failure mod and have run into a problem with re-implementing a failure when a vessel with a failure is reloaded.  Other failures can be redone during OnStart(), but the ModuleSAS failure has a problem.  If it's re-implemented during OnStart() the vessel will not activate SAS again if the part is repaired.  The repair works fine if the vessel never leaves context (i.e. you break and repair it from without reloading) and if you repair it then reload it, SAS returns.

Something happens soon after the vessel is loaded where it seems to pick up it's available SAS levels.  Neither OnStart nor OnStartFinished works.   If I re-implement the failure using OnUpdate, add in a small 20 frame delay then 'fail' the part, it works correctly and sees SAS when it's repaired, but only if it delays things for a few frames.

        //this one works, as long as the ticker is above 10
        public override void OnUpdate()
        {
            base.OnUpdate();
            if (failed && ticker++ == 20)
                DoFailure();
        }
		
        //doesn't work
        public override void OnUpdate()
        {
            base.OnStart();
            if (failed)
                DoFailure();
        }

Obviously this is a really ugly hack.  Is there a better way to run a function soon (but not too soon!) after a vessel has been loaded?  Or maybe there's a way to update a vessel to 'see' it now has SAS available when the part is repaired.

Link to comment
Share on other sites

There is probably a better way to achieve what you want, but hard to say without seeing what your DoFailure() method does.
Depending on how you disable the SAS module, I suspect you might run into the SAS level being available again after certain events (decoupling/docking for example).

As for implementing a delayed callback, you can use a coroutine :

public override void OnStart()
{
  base.OnStart();
  if (failed)
    StartCoroutine(DoFailureDelayed);
}

private IEnumerator DoFailureDelayed()
{
  for (int i = 0; i < 20; i++)
    yield return null;

  DoFailure();
}

 

Edited by Gotmachine
Link to comment
Share on other sites

Perfect!  This solution does the trick.  SAS is just being disabled/enabled with ModuleSAS.moduleIsEnabled, so definitely nothing fancy.   

Technically SAS does work (i.e. you can press the SAS button) if the reloaded vessel is repaired, it just doesn't have any SAS levels (not even 0) so it doesn't do anything.   

You are probably right about some events triggering re-evaluation for SAS.  My other option was to find if there's an accessible function that can manually trigger this, but I wasn't able to find it.

Thanks for the help with this. 

Link to comment
Share on other sites

As a followup, while this solution works, there's an edge case that still has this issue.  If a ModuleSAS fails and the craft is no longer infocus but still loaded (i.e. from undocking or EVA) and returned to while still loaded and then repaired, none of the SAS modes turn up. 

Leaving and returning to the craft (i.e. just pressing the '[' key) fixes the problem.  I haven't found a way to trigger the reevaluation that fixes this issue so at this point it's either bin the whole idea or live with this glitch.

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