Observer question
-
must me the heat here but some how i'm puzzled.
I want to remove an observer.It's added like this:
Sketchup.active_model.selection.add_observer(MySelectionObserver.new)and here is the observer code:
class MySelectionObserver < Sketchup::SelectionObserver
def onSelectionBulkChange(selection)
#code to execute when the selection changes
end
endNow how do i remove this observer?
Thx!
-
Make an enduring reference to the observer as you set it...
@@myselectionobserver=Sketchup.active_model.selection.add_observer(MySelectionObserver.new)later on to remove it use...
Sketchup.active_model.selection.remove_observer(@@myselectionobserver)OR if the parts of the code are not within the same class/module use a global
$BUT make sure it's a 'unique' name - like$this_is_pouts_extra_special_selection_observerso no one else's similar observer by the same name gets messed with!!! -
@tig said:
Make an enduring reference to the observer as you set it...
@@myselectionobserver=Sketchup.active_model.selection.add_observer(MySelectionObserver.new)The API states that Selection.add_observer returns a boolean true || false indicating sucess.
perhaps:
@@myselectionobserver = MySelectionObserver.new() Sketchup.active_model.selection.add_observer(@@myselectionobserver) -
Dan you are right - I rushed that off without thinking

Your way is correct - make a new observer, apply it... remove it!
` @@myselectionobserver = MySelectionObserver.new()
Sketchup.active_model.selection.add_observer(@@myselectionobserver)Sketchup.active_model.selection.remove_observer(@@myselectionobserver)`
IS the correct way... -
suberb, and this on a sunday!!!
Thx tig and dan, much appreciated!
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better π
Register LoginAdvertisement