Jump to content

How to "create" a vessel


Recommended Posts

I'm fairly new in mod development, started playing around in with some code.

I want to simulate a vessels trajectory in the background, and then display it on screen. I figure an easy way to do this would be to create a vessel, read the forces on it, move it to an updated location, repeat a sufficient number of times, and delete it before the game calculates the next frame.

I figured I'd start basic and build up from there so my first test was to save the active vessel to a ProtoVessel, delete the active vessel, and reload it from the ProtoVessel

Here's the code.

ProtoVessel pv = FlightGlobals.ActiveVessel.BackupVessel();

FlightGlobals.ActiveVessel.Unload();
FlightGlobals.RemoveVessel(FlightGlobals.ActiveVessel);
FlightGlobals.ActiveVessel.StartFromBackup(pv);
FlightGlobals.ActiveVessel.Load();
FlightGlobals.AddVessel(FlightGlobals.ActiveVessel);

Unfortunately it failed miserably. I've tried several variations on this code, in case I'm just getting the order wrong, but nothing works.

I've seen mods produce new functioning vessels in game, so it should be possible.

Anyone know what I'm missing?

Link to comment
Share on other sites

After examining the code for Hangar and ELP I managed to get the following working:

        public void LaunchEvent()
        {
            uint flightID = ShipConstruction.GetUniqueFlightID(HighLogic.CurrentGame.flightState);
            ConfigNode[] partNodes = new ConfigNode[1];
            partNodes[0] = ProtoVessel.CreatePartNode("KSI.FlareA", flightID, null);
            ConfigNode[] additionalNodes = new ConfigNode[0];
            ConfigNode protoVesselNode = ProtoVessel.CreateVesselNode("Flare", VesselType.Probe, part.vessel.GetCurrentOrbit(), 0, partNodes, additionalNodes);
            protoVesselNode.SetValue("lon", vessel.mainBody.GetLongitude(launchPoint.position));
            protoVesselNode.SetValue("lat", vessel.mainBody.GetLatitude(launchPoint.position));
            protoVesselNode.SetValue("alt", vessel.mainBody.GetAltitude(launchPoint.position));

            ProtoVessel protoVessel = HighLogic.CurrentGame.AddVessel(protoVesselNode);

            flare = protoVessel.vesselRef;
            if (flare != null)
            {
                flare.Load();
                StartCoroutine("Spawn");
            }
        }

        IEnumerator Spawn()
        {
            if ((flare.Parts[0] != null) && (flare.Parts[0].Rigidbody != null))
            {
                flare.Parts[0].Rigidbody.isKinematic = true;
            }

            while ((flare.packed) && (flare.acceleration.magnitude == 0))
            {
                flare.SetPosition(launchPoint.position);
                yield return null;
            }
            flare.SetWorldVelocity(launchPoint.up * velocity);

            flare.GoOffRails();
            flare.IgnoreGForces(250);
        }

This would create a vessel from a part named "KSI.FlareA".

Notes: 1) this is a single part that gets created as a vessel 2) some of the lines may not be needed - got here by trial and error (lots of error) 3) it wasn't crashing the game but I didn't leave the game up very long - use at your own risk!

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