Observer Usage
-
This may be a stupid question to some of you wizards, but can anyone tell me why this code does not work when pasted into a .rb file and dropped into the plugins directory?
straight out of the API...
# This is an example of an observer that watches tool interactions. class MyToolsObserver < Sketchup;;ToolsObserver def onActiveToolChanged(tools, tool_name, tool_id) UI.messagebox("onActiveToolChanged; " + tool_name.to_s) end end # Attach the observer. Sketchup.active_model.tools.add_observer(MyToolsObserver.new)
This is the resulting error...
@unknownuser said:
Error Loading File section_view.rb
undefined method `tools' for nil:NilClassThe script works perfectly however if I call the last part manually using the Ruby Console...
Sketchup.active_model.tools.add_observer(MyToolsObserver.new)
I suspect that my inability to understand this line of the class intro from the API is why I'm lost...
@unknownuser said:
To implement this observer, create a Ruby class of this type, override the desired methods, and add an instance of the observer to the Tools object.
WTF does override the desired methods mean!!??
-
I think it means active_model is nil, which I guess is a possibility on Macs. What happens if you wrap the code in a function and call it from the Ruby Console?
-
Thanks Jim. Your intuition was right - a call from within a function worked. It's annoying that the API isn't a little clearer about this but I am a noob and there are many things that remain unclear to me.
-
Hanks, lots of us are going through it. There is a thread at the top of this forum called API typos and something or another. If you have suggestions for the API, put it in that thread. The Google guys who write the API and SketchUp Ruby watch that thread and incorporate the suggestions and typos that are posted there. If it was confusing to you, it probably will be for someone else!
Chris
Advertisement