Jump to content

OnEditorAttach for part attached to


Recommended Posts

The callback:   Part.OnEditorAttach

works very nicely to let you be able to change values on a part which is being attached.  

I need to do the other way:  I need to change values on the part which is already there, and which is being attached to.

Right now I'm using the GameEvents.onEditorShipModified, which works, but the code (which is in a module inserted into all tanks) runs every time a ship is  modified, on every tank on the ship;  on a big ship, this could be a lot of parts and I'm a bit concerned over performance.

So is there an equivalent way to notify a part that something is being attached to it?  I don't see anything relevant in the Part at all.

Thanks in advance.

Link to comment
Share on other sites

Moving to Plugin Development Help and Support.

8 hours ago, linuxgurugamer said:

So is there an equivalent way to notify a part that something is being attached to it?

Am not in front of a KSP computer at the moment, so I can't easily check this myself right now, but... does it not work to just fire on "attach" (or perhaps GameEvents.onEditorPartPlaced), and then just look at the parent of the attached thing?  (Or does the event fire before the "parent" field is set?)

Link to comment
Share on other sites

 

17 hours ago, Snark said:

Moving to Plugin Development Help and Support.

Am not in front of a KSP computer at the moment, so I can't easily check this myself right now, but... does it not work to just fire on "attach" (or perhaps GameEvents.onEditorPartPlaced), and then just look at the parent of the attached thing?  (Or does the event fire before the "parent" field is set?)

From what I can tell, it only fires after the part is attached, on the new part, but the existing part doesn't get any specific event.

It's not a big deal, I have it working using onEditorShipModified, but was hoping to restrict it to the part only when the part was modified or if another part was attached to it.

Link to comment
Share on other sites

2 hours ago, linuxgurugamer said:

From what I can tell, it only fires after the part is attached, on the new part, but the existing part doesn't get any specific event.

Right, but in effect, doesn't that give you exactly what you need?  Just register for GameEvents.onEditorPartPlaced.  And then when you receive the notification, instead of doSomethingWith(part), you just doSomethingWith(part.parent).  Would that not fit the bill?

I imagine you'd need to add some code to deal with symmetry groups (since only one part gets the event when a symmetry group is placed)-- would need to handle the case of "I placed a symmetry group on a symmetry group so I need to do it to all of them" versus "I placed a symmetry group on one part so I need to do the thing just once".  But that's a fairly minor detail and shouldn't need much code to handle.

Link to comment
Share on other sites

2 minutes ago, Snark said:

Right, but in effect, doesn't that give you exactly what you need?  Just register for GameEvents.onEditorPartPlaced.  And then when you receive the notification, instead of doSomethingWith(part), you just doSomethingWith(part.parent).  Would that not fit the bill?

I imagine you'd need to add some code to deal with symmetry groups (since only one part gets the event when a symmetry group is placed)-- would need to handle the case of "I placed a symmetry group on a symmetry group so I need to do it to all of them" versus "I placed a symmetry group on one part so I need to do the thing just once".  But that's a fairly minor detail and shouldn't need much code to handle.

Pretty much what I did, although I used the OnEditorShip Modified.  I didn't check symmetry groups, so I'll have to look into that

I don't have time now, but could you point me to some symmetry code I can reference which deals with this?

Thanks

Edited by linuxgurugamer
Link to comment
Share on other sites

Just now, linuxgurugamer said:

Pretty much what I did.  I didn't check symmetry groups, so I'll have to look into that

Yeah, the editor events are kind of a mess.  I had to do a lot of head-scratching when I was writing BetterCrewAssignment.  I came up with something that basically worked, but was kinda kludgy and I'd do it better if I were doing it all over again.  More recently, I used it on VesselCategorizer, and it worked much more nicely there.

In my case, I wanted to ensure that "every part that gets placed on the vessel goes through <my processing function>", so here are the cases I had to cover:

  • Placing the original root part of the vessel (since it doesn't get onEditorPartPlaced).  Fortunately, this has an event of its own, "onEditorPodPicked", so that covers that.
  • Placing a symmetry group only gets a notification for one of the symmetry counterparts.  I handled that by doing the part, then looping through all its symmetry counterparts.  I didn't care about the part being attached to, so that was plenty for me.  In your case, I expect you'd need to check "do my symmetry counterparts have the same parent as me" to avoid multiple processing if a symmetry group is placed around just one part.
  • Placing an entire tree of parts (e.g. from a subassembly, or by copying a part of the rocket) only gets a notification for the root part of the group of parts being placed.  So I handled that by recursing through all the part's children.  Not sure if that's relevant to your use case or not.

Anyway, my code that does all that is here, if you're interested:

https://github.com/KSPSnark/VesselCategorizer/blob/master/src/EditorCategorization.cs

 

Link to comment
Share on other sites

2 minutes ago, Snark said:

Yeah, the editor events are kind of a mess.  I had to do a lot of head-scratching when I was writing BetterCrewAssignment.  I came up with something that basically worked, but was kinda kludgy and I'd do it better if I were doing it all over again.  More recently, I used it on VesselCategorizer, and it worked much more nicely there.

In my case, I wanted to ensure that "every part that gets placed on the vessel goes through <my processing function>", so here are the cases I had to cover:

  • Placing the original root part of the vessel (since it doesn't get onEditorPartPlaced).  Fortunately, this has an event of its own, "onEditorPodPicked", so that covers that.
  • Placing a symmetry group only gets a notification for one of the symmetry counterparts.  I handled that by doing the part, then looping through all its symmetry counterparts.  I didn't care about the part being attached to, so that was plenty for me.  In your case, I expect you'd need to check "do my symmetry counterparts have the same parent as me" to avoid multiple processing if a symmetry group is placed around just one part.
  • Placing an entire tree of parts (e.g. from a subassembly, or by copying a part of the rocket) only gets a notification for the root part of the group of parts being placed.  So I handled that by recursing through all the part's children.  Not sure if that's relevant to your use case or not.

Anyway, my code that does all that is here, if you're interested:

https://github.com/KSPSnark/VesselCategorizer/blob/master/src/EditorCategorization.cs

 

thanks

 

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