Jump to content

Is there a hook when switching vessels? I need to close my network sockets... somehow.


Recommended Posts

Hi,

I have a working prototype for a part that sends and receives over UDP sockets: https://github.com/jarpy/OpenSpaceKontrol

My current challenge is that it only works for the first vessel/flight of a game session. After that, I'm still holding the UDP sockets, so the next vessel can't bind to them.

'Would love to hear any suggestions for closing the sockets when the vehicle is destroyed or switched away. Is there a simple "OnNotFocussed" kind of method I'm missing? Or do I need to pull back and make my sockets part of a broader (non-part, non-vessel) context.

Thanks very much for any help you can offer.

Link to comment
Share on other sites

Note that .onVesselChange also fires on undocking, losing parts to an accident (overheating/collision/etc) so you'd have to check for that in your code.

However, looking at your code, I'd consider moving the actual UDP port binding and associated code to a KSPAddon.Flight class. Having it on a partModule like you do means that even just having two OskModule partModules on the same vessel could get interesting.

A KSPAddon.Flight class only runs once per flight scene load so you don't have to worry about binding/unbinding sockets and such regardless of what happens during the scene.

You would still need the OskModule partmodule class to control the vessel, but I think writing the logic for a single KSPAddon.Flight to recognize a vessel switch would be simpler then having multiple partModules trying to decide it between themselves.

D.

Link to comment
Share on other sites

Sounds like great advice. I like your split approach, with separation of concerns between "game things" (commands, telemetry) and "computer things", (UDP sockets).

Thanks for taking the time to look at my code; this is a nice place.

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