Jump to content

Spawning a Vessel


Recommended Posts

Consider, if you would, the lovely bit of code I finally got to do what I wanted:

        public static void UnhangarCraft(Vessel vVesselStored)
{
// Convert the stored protovessel to a new protovessel.
// Use BackupVessel because that seems to work and protovessel does not. `\o/`
ProtoVessel pVessel = vVesselStored.BackupVessel();

// Get rid of the original hidden vessel - even though it was unloaded KSP still 'sees' the original craft.
// I do not care why. :|
vVesselStored.state = Vessel.State.DEAD;

foreach (Part p in vVesselStored.Parts)
{
if (p != null && p.gameObject != null)
p.gameObject.DestroyGameObject();
}

// Load the new protovessel we made. KSP won't reload (or rather render) a vessel that was unloaded - it will only load a protovessel. :$
pVessel.Load(FlightDriver.FlightStateCache.flightState);
// I suspect this is actually a KSP bug since the same crap happens with newly spawned static objects. If you query the active state
// of the invisible craft, it claims it is active. And no, forcing the renderers doesn't work either.

// Convert protovessel to vessel
Vessel vNewVessel = pVessel.vesselRef;

// Unload then reload the vessel - this seems to be the way to properly re-initialise flightstate etc.
// Don't do this and you get a craft with a stuck surface velocity reading. It looks like KSP transposes orbital
// and surface velocity or some other stupid s**t. I don't care.
// And yes, this time KSP does load an unloaded vessel with no need for protovessel b******t. I don't care why.
vNewVessel.Unload();
vNewVessel.Load();
}

Here's the spec: A craft rolls up to a hangar. The crew get out. You use a simple GUI to 'hangar' the craft. The craft's id is saved to a text database/persistence file/whatever and is then set inactive and unloaded.

Doesn't matter that the vessel still 'exists'. There's a check every frame once you come back to a flight scene that makes any hangared craft inactive/invisible. This allows you to take control of a hangared craft from the tracking station or space center scene, which is intentional (and also handled).

When you unhangar a craft, the id is matched to the vessel in FlightGlobals, the hangar cleared and then the above code does its thing.

I wanted to pick the brains of fellow KSP modders on what exactly I am missing here, seeing if there is a less clunky solution. Because what I thought were obvious solutions just didn't work.

Any thoughts, advice, chastisement or hurling of rotten vegetables welcome.

Edited by AlphaAsh
Spolling and grimmar fixes
Link to comment
Share on other sites

I have about 300 lines of code to make spawning vessels work in Contract Configurator. There's about a dozen hacks in there where I just couldn't get something to work right in all situations and ended up just doing something weird "because it worked". So when it comes to spawning/loading/unloading vessels... I'd say you're doing pretty good with what you've got there. :)

Link to comment
Share on other sites

I think my favourite discovery was when I used Load and LoadObjects after each other. My twin engined aircraft with two braking parachutes became a monstrous four-engined, four parachute hybrid. Basically two craft treated as one. It cloned the pilot when he got in. It actually flew but I think I heard the craft's soul screaming "kiiiiilllll meeeee pllleeeassee" all the way. The Z tearing was intense.

Edited by AlphaAsh
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...