linuxgurugamer

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

Share this post


Link to post
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");
                        }
                    }
                }
            }
        }
    }

 

 

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
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.  

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now