Jump to content

What triggers the onCrash event? Trying to properly identify when a vessel has been destroyed


Recommended Posts

I've been doing some testing, and have been getting inconsistent results.

I've created simple vessels (command pod + srb), launched them and let them crash, but the onCrash event isn't triggering.

So, two questions:

  • What triggers the onCrash event?
  • Is there another way to identify when a vessel has been destroyed?

Thanks in advance

Link to comment
Share on other sites

Not sure about checking on whether a vessel has been destroyed without having a list and searching said list of vessels and seeing if the vessel in question is still active

However, this might be useful for your purposes ... when the part (EMP Missile) is about to be destroyed it runs the specified code

Quote

    public class ModuleEMP : PartModule
    {
        [KSPField(isPersistant = true, guiActive = true, guiActiveEditor = false, guiName = "EMP Blast Radius"),
         UI_Label(affectSymCounterparts = UI_Scene.All, controlEnabled = true, scene = UI_Scene.All)]
        public float proximity = 5000;

        public override void OnStart(StartState state)
        {
            if (HighLogic.LoadedSceneIsFlight)
            {
                part.force_activate();
                part.OnJustAboutToBeDestroyed += DetonateEMPRoutine;
            }
            base.OnStart(state);
        }

        public void DetonateEMPRoutine()
        {
            foreach (Vessel v in FlightGlobals.Vessels)
            {
                if (!v.HoldPhysics)
                {
                    double targetDistance = Vector3d.Distance(this.vessel.GetWorldPos3D(), v.GetWorldPos3D());

                    if (targetDistance <= proximity)
                    {
                        var count = 0;

                        foreach (Part p in v.parts)
                        {
                            var wmPart = p.FindModuleImplementing<MissileFire>();

                            if (wmPart != null)
                            {
                                count = 1;
                                p.AddModule("ModuleDrainEC");
                            }
                        }

                        if (count == 0)
                        {
                            v.rootPart.AddModule("ModuleDrainEC");
                        }
                    }
                }
            }
        }
    }

 

 

Link to comment
Share on other sites

I think OnCrash is when a vessel crashes into another vessel. Maybe other things too, like buildings.

Have you tried onVesselWillDestroy? That might work. Though it might require waiting a frame or two for the vessel to actually be removed.

I know onVesselDestroy is not what you are looking for. That is fired when the vessel monobehaviour is destroyed, passing out of loading range would trigger it.

Link to comment
Share on other sites

You can use onVesselWillDestroy or OnVesselDestroy. They fire when a vessel is about to be destroyed or it's imminent.
OnCrash is fired by any Part that collides with something and the impact is greater than the crash tolerance of the part.

Link to comment
Share on other sites

22 hours ago, JPLRepo said:

You can use onVesselWillDestroy or OnVesselDestroy. They fire when a vessel is about to be destroyed or it's imminent.
OnCrash is fired by any Part that collides with something and the impact is greater than the crash tolerance of the part.

Oh, great.  Thank you 

I knew about those, but for some reason was drawing a blank.  

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