Jump to content

Notification in VAB


Recommended Posts

I am writing a mod that displays information about the vessel while it is in the VAB.

Is there a way to get a notification whenever a part is added to or removed from the vessel or the resources or staging of a part have changed?

Testing the entries in EditorLogic.SortedShipList for changes is possible, but not very elegant.

Link to comment
Share on other sites

There definitely are events for attaching/detaching. I believe they were GameEvents.OnPartAttach and GameEvents.OnPartRemove. Beware that most other events do not work in the VAB/SPH and that there is no active vessel in the editors. I'm also not sure if OnPartAttach will fire for the root part (even though I just played around with these events a few days ago…).

I would also recommend wrapping access to SortedShipList in a try/catch as I experienced a bug with it (in case you're still working with it): http://bugs.kerbalspaceprogram.com/issues/2021

Edited by Airblader
Link to comment
Share on other sites

There definitely are events for attaching/detaching. I believe they were GameEvents.OnPartAttach and GameEvents.OnPartRemove. Beware that most other events do not work in the VAB/SPH and that there is no active vessel in the editors. I'm also not sure if OnPartAttach will fire for the root part (even though I just played around with these events a few days ago…).

I would also recommend wrapping access to SortedShipList in a try/catch as I experienced a bug with it (in case you're still working with it): http://bugs.kerbalspaceprogram.com/issues/2021

The perfect answer to my question ... did not know about GameEvents.

onPartAttach and onPartRemove take care about parts, with onInputLocksModified I can test if the mouse left the staging-configuration-area and onPartActionUIDismiss fires whenever the ActionGUI of a part gets closed.

That is good enough for my application.

        public void start_triggers()
{
GameEvents.onPartAttach.Add(myfun);
GameEvents.onPartRemove.Add(myfun);
GameEvents.onInputLocksModified.Add(myfun2);
GameEvents.onPartActionUIDismiss.Add(myfun3);
}

public void end_triggers()
{
GameEvents.onPartAttach.Remove(myfun);
GameEvents.onPartRemove.Remove(myfun);
GameEvents.onInputLocksModified.Remove(myfun2);
GameEvents.onPartActionUIDismiss.Remove(myfun3);
}

Thanks also for the hint about the exception. Since this is only called from an attached part, I did not experience any problems yet.

Link to comment
Share on other sites

I would also recommend wrapping access to SortedShipList in a try/catch as I experienced a bug with it (in case you're still working with it): http://bugs.kerbalspaceprogram.com/issues/2021

It's not bug. I don't know why you wrote it into KSP bugtracker. It's normal, that in C# you can't work with empty list. Just write this before your code.


if (EditorLogic.startPod != null)
{
[Your Code]
}

Link to comment
Share on other sites

I'm not trying to work with the list. Granted, I am new to C# itself (coming from Java), but so much as assigning a property to a local variable should not throw an exception. If it's null, that's alright. But accessing any property should never throw.

See http://msdn.microsoft.com/en-us/library/ms229006.aspx:

X AVOID throwing exceptions from property getters.

Property getters should be simple operations and should not have any preconditions. If a getter can throw an exception, it should probably be redesigned to be a method. Notice that this rule does not apply to indexers, where we do expect exceptions as a result of validating the arguments.

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