Posted May 29, 20196 yr Possibly a silly question, but I'm having a dickens of a time finding an answer anywhere and was wondering whether someone here might know. Details below, but it boils down to this: If I register for notification that a BaseField's value has changed on a PartModule, by registering for OnValueModified... how and where do I un-register it in order not to leak? (Or is it necessary?) Details: Let's say I have a PartModule that has some KSPField on it, and I want to register for notifications when the field's value changes. I note that BaseField<KSPAttribute>, has an OnValueModified event that I can register for. So I could do something like this in my PartModule: public override void OnAwake() { base.OnAwake(); myBaseField.OnValueModified += OnMyBaseFieldModified; } private void OnMyBaseFieldModified(object o) { // this gets called when the value of the field changes } ...So far, so good; that can make the desired thing happen. What troubles me, though, is concern about event registration hygiene. Specifically, do I need to un-register somewhere? If so, where? Here's why I ask: As is probably obvious from my asking this question, I don't have experience registering for field-change events on PartModules. I do, however, register for events all the time (e.g. for GameEvents or whatever) from KSPAddon scripts. And for such scripts, it's important that every event registration be matched with a corresponding un-registration, like this: [KSPAddon(KSPAddon.Startup.EditorAny, false)] public class SomeAddon : MonoBehaviour { public void Awake() { // register all events here GameEvents.onEditorLoad.Add(OnShipLoaded); } public void OnDestroy() { // unregister all events here GameEvents.onEditorLoad.Remove(OnShipLoaded); } private void OnShipLoaded(ShipConstruct construct, CraftBrowserDialog.LoadType loadType) { // this gets called when a ship is loaded } } ...because if I don't unregister the handlers I've registered, then I "leak" the registration and various hilarity ensues. However... I see that PartModule has an OnAwake() method that I can override, but I'm not seeing any corresponding OnDestroy(). So I don't see any place that leaps out at me as the "right" place to un-register... nor do even know whether it's necessary (and if not, why not, since it is necessary for KSPAddon scripts). Can anyone enlighten me?
May 29, 20196 yr PartModule inherits from MonoBehavior, so by virtue of that, it has OnDestroy(). Or at least that's how I've been doing it.
May 29, 20196 yr Author 1 hour ago, cakepie said: PartModule inherits from MonoBehavior, so by virtue of that, it has OnDestroy(). Or at least that's how I've been doing it. So, the same "just make a method, even though there's no base class method to override" thing with Awake() and OnDestroy() applies to PartModules, too? Ah, okay. I'll give that a whirl, see what happens. Thanks!
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.