Jump to content

onVesselsUndocking() doesn't appear to be working


Recommended Posts

I have been trying to catch an undocking of vessels.  I found the really cool callback called:  GameVents.onVesselsUndocking(Vessel, Vessel), but it doesn't appear to be working.

I added the following to the Start() of a vessel  module:

GameEvents.onVesselsUndocking.Add(undocking);

...

void undocking(Vessel v1, Vessel v2)
{
     Debug.Log("undocking");
}

 

I know it was added to the event list, I had a debug statement right before the Add.  But the function never gets called

Link to comment
Share on other sites

2 hours ago, peteletroll said:

I don't know about onVesselsUndocking, but onPartUndock and onPartUndockComplete work for me.

But only for one of the parts, not for both of them.  I need to be able to know both vessels, the original, and the new one which was created via the undocking.

Edit 1:  A bit more info:  I tested these three both in a vessel module and in a regular MonoBehaviour class.:

In the VesselModule, the Start() is called and adds these three callbacks for all vessels and objects in the system,.  Then, when an undocking is performed, Start is called again in the vessel module to add the callbacks for the new vessel which is created.  But, the callbacks are never called.

In the Monobehaviour class, the Start is called once.  But the callbacks are not called when an undocking is done.

Code for the regular MonoBehaviour class:

 [KSPAddon(KSPAddon.Startup.Flight, false)]
    public class BPB_Flight : MonoBehaviour
    {
        void Start()
        {
            Log.Info("BPB_Flight. Added Undocks");
            GameEvents.onPartUndock.Add(onPartUndock);
            GameEvents.onPartUndockComplete.Add(onPartUndockComplete);
            GameEvents.onVesselsUndocking.Add(onVesselsUndocking);
        }

        void onVesselsUndocking(Vessel v1, Vessel v2)
        {
            Log.Info("BPB_Flight.onVesselsUndocking");
        }
        void onPartUndock(Part p)
        {
            Log.Info("BPB_Flight.onPartUndock, part " + p.partInfo.title);
        }
        void onPartUndockComplete(Part p)
        {
            Log.Info("BPB_Flight.onPartUndockComplete, part " + p.partInfo.title);
        }
    }

 

Code for the VesselModule class:

    class BPB_VesselModule : VesselModule
    {
        Vessel lastActiveVessel;
        BPB_VesselModule vm;

        bool timeoutInProgress = false;

        void Start()
        {
            if (HighLogic.LoadedSceneIsFlight)
            {
                Vessel v = FlightGlobals.ActiveVessel;
                var vm = v.GetComponent<BPB_VesselModule>();

 
                if (FlightGlobals.ActiveVessel.missionTime > vm.disableAfter)
                {
                    vm.armed = false;
                }
                Log.Info("Added Undocks");
                GameEvents.onPartUndock.Add(onPartUndock);
                GameEvents.onPartUndockComplete.Add(onPartUndockComplete);
            }            
        }
        void onPartUndock(Part p)
        {
            Log.Info("onPartUndock, part " + p.partInfo.title);
        }
        void onPartUndockComplete(Part p)
        {
            Log.Info("onPartUndockComplete, part " + p.partInfo.title);
        }
                  
...
                  }

 

Edit 2:  Apparently "uncoupling" isn't the same as "undocking".  I've been testing with two vessels docked together in the VAB, I just tested by undocking and redocking, and they seemed to work better

Edited by linuxgurugamer
Link to comment
Share on other sites

5 hours ago, linuxgurugamer said:

I added the following to the Start() of a vessel  module:

Maybe you can try to set the callbacks in OnAwake(). Works for me (TM). :)

EDIT: but I'm working on a PartModule. Your situation could be different.

EDIT AGAIN: or maybe OnStart() instead of Start().

Edited by peteletroll
Link to comment
Share on other sites

It turns out that the callback GameEvents.onVesselsUndocking does work, but it does NOT fire if a couple of docking ports are together during the launch, in that case, when you separate, it is seen as an uncouple event.  In order to catch that, you need to use the GameEvents.onVesselCreate

This is a problem if the new vessel needs to know the old vessel.  In my case, I need to know the current mission time of the original vessel.  The following code seems to work.  I'm not done with my testing, but it looks like the callback for the onVesselCreate is called before the VesselModule.Start is done on the new vessel;  or maybe it's not being called.  I'll update this as I proceed with  my testing

Edit:  I know the code has a problem, the currentVessel is totally incorrect for now.

 

[KSPAddon(KSPAddon.Startup.Flight, false)]
public class BPB_Flight : MonoBehaviour
{
    Vessel currentVessel;

    void Start()
    {
        GameEvents.onVesselsUndocking.Add(onVesselsUndocking);
        GameEvents.onVesselCreate.Add(onVesselCreate);

        currentVessel = FlightGlobals.ActiveVessel;
    }

    void OnDestroy()
    {
        GameEvents.onVesselsUndocking.Remove(onVesselsUndocking);
        GameEvents.onVesselCreate.Remove(onVesselCreate);
    }

    void onVesselCreate(Vessel v)
    {
        var vm = v.GetComponent<BPB_VesselModule>();
        vm.missionTime = currentVessel.missionTime;
    }
  
    void onVesselsUndocking(Vessel v1, Vessel v2)
    {
        var vm1 = v1.GetComponent<BPB_VesselModule>();
        var vm2 = v2.GetComponent<BPB_VesselModule>();
        vm1.missionTime = Math.Max(v1.missionTime, v2.missionTime);
        vm2.missionTime = vm1.missionTime;
    }
}

 

 

Edited by linuxgurugamer
Link to comment
Share on other sites

There are lots of ways to solve this.  The trick is to do it in a way which won't impact the performance of the system.  If I start tracking each part, it will get slow very fast.

But, I do have the problem identified, and hope to release a working fix in the next day or so

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