Jump to content

VesselModules, docking, undocking, and you


Recommended Posts

Or maybe it's just me, because I didn't get the memo. :) But this may be useful for other modders.

I've been chasing some nasty bugs in RasterPropMonitor where docking two vessels and undocking them in the same game session leads to really bad behavior.  I tracked it down to unexpected behavior of the VesselModules (well, *I* didn't expect it).  Here's a sketch of what happens:

Go to flight with two vessels ready to dock.  I track the VesselModules by the GUID of the craft.  I'll call the GUIDs A and B.  The VesselModule caches the guid so I can see if the vessel changes.

When the two craft dock, the vessel that was the FROM vessel in the OnPartCouple callback disappears.  The VesselModule sees the onVesselDestroy event, so I remove the GUID from the static dictionary of <GUID, VesselModule> mappings.  The VesselModule is not destroyed, however. The cached reference to the vessel that I stored in the VesselModule is set to null, but the VesselModule still sees FixedUpdates, etc.

If I undock the craft *without* leaving the scene, the VesselModule for A sees an onVesselCreate callback that contains a new vessel.  If I GetComponent<Vessel> in the callback, I see that same vessel, so the VesselModule now knows it belongs to new vessel C.

However, when I leave the scene (return to SpaceCenter), the OnSave method for the VesselModule for vessel C is never called.  Vessel B's is called, but the repurposed VesselModule's onSave doesn't fire.  I assume this is not intended.

In summary, VesselModule instances can be detached from a vessel when two craft dock; if one of these zombie VesselModules is available when craft undock, the zombie is assigned to the new craft.  However, the reassigned VesselModule doesn't fire OnSave.

Link to comment
Share on other sites

That's annoying to say the least, it drastically reduces their usefulness.

I see there's a VesselModuleManager class. I don't recall a Manager class for any of the other classes we work with. I wonder if something's gone screwy in there?

Still, not something I think we can deal with though so we'll have to see what happens.

D.

Link to comment
Share on other sites

Update to this:

On 6/4/2016 at 3:41 PM, MOARdV said:

An additional note: all of the VESSEL entries in the persistent file are missing if you do enough docks / undocks without changing scenes.

It looks like the problem was in my code.  When my VesselModule's OnDestroy was called, I looked at my cached copy of Vessel to determine if I had a legitimate vessel to destroy.  However, Vessel can be set to null by KSP.  The problem was that if Vessel was null, I didn't unregister callbacks, and later the destroyed VesselModule saw an OnVesselCreate callback while at the Space Center, and when it tried to GetComponent<Vessel>, it threw an exception that appears to have prevented the game state from saving.

In my original implementation, I set vessel to null if it's a craft I don't care about (unmanned), and I skipped callback registration.  However, since KSP can set vessel to null as well when it detaches a VesselModule from its vessel, I need an additional field to keep track of whether I'm registered, and, if I am, make sure I unregister in OnDestroy (or always unregister callbacks, provided no one throws exceptions for unregistering an unregistered callback).

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