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_observer
so 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!
Advertisement