sketchucation logo sketchucation
    • Login
    Oops, your profile's looking a bit empty! To help us tailor your experience, please fill in key details like your SketchUp version, skill level, operating system, and more. Update and save your info on your profile page today!
    πŸ«› Lightbeans Update | Metallic and Roughness auto-applied in SketchUp 2025+ Download

    $ versus @

    Scheduled Pinned Locked Moved Developers' Forum
    20 Posts 11 Posters 3.2k Views 11 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • J Offline
      Jim
      last edited by

      @unknownuser said:

      • replace all arrow codes with constants - e.g. $leftarrow -> LEFTARROW
      • replace rest of globals with class variables - e.g. $value -> @@value

      tested and it works (retains distance between calls of tool)

      So, should we be creating new instances of a Tool each time, or referencing the already existing instance? (Instance level variables will also continue to exists for the life of the session, if you don't instantiate a new class every time the tool is activated.)

      Hi

      1 Reply Last reply Reply Quote 0
      • AdamBA Offline
        AdamB
        last edited by

        I'd suggest always re-instance a Tool unless there is a compelling reason to make it persist.

        As a general rule, releasing resources as soon as possible is a GoodThing, and secondly - and probably more importantly in a Ruby context, to ensure you don't carry references to Ruby objects and therefore stop garbage collection happening - worse still carry stale references to objects that have subsequently been deleted which tends to make SU jump into the azure blue sea of unallocated heap store and commit hare-kiri.

        Adam

        Developer of LightUp Click for website

        1 Reply Last reply Reply Quote 0
        • T Offline
          tomot
          last edited by

          @didier bur said:

          Hi,
          But it is a lazy way of retaining values for your dialog boxes for instance, so user get the last values used.
          One can avoid globals when using classes and methods, classes variables (@@) and objects variables (@).

          I'm sorry but I cant let this topic die just yet. I have carefully reread all the
          comments. Does this quote offer a solution to retaining values in Dialog Boxes?

          [my plugins](http://thingsvirtual.blogspot.ca/)
          tomot

          1 Reply Last reply Reply Quote 0
          • TIGT Offline
            TIG Moderator
            last edited by

            For a dialog's default values that you want keeping from session to session within a particular model, I write them as attributes to the model itself: when the dialog initialises it looks for their values, if they are not there it takes defaults - otherwise you have them saved on a model by model basis... For an example see my TextTag.rb.

            .

            TIG

            1 Reply Last reply Reply Quote 0
            • R Offline
              RickW
              last edited by

              So, there you have it - three solutions that avoid globals:

              1. Sketchup.read_default and Sketchup.write_default (persistent across SketchUp sessions and models)
              2. Attributes (persistent within a given model, between sessions)
              3. Class variables (@@variable) (persistent only within a SketchUp session)

              RickW
              [www.smustard.com](http://www.smustard.com)

              1 Reply Last reply Reply Quote 0
              • T Offline
                todd burch
                last edited by

                For persistent data for Dialog Boxes, you should use Sketchup.write_default and Sketchup.read_default. The keys and values are stored in the registry (Windows) and the plist (Mac).

                Todd

                1 Reply Last reply Reply Quote 0
                • T Offline
                  tomot
                  last edited by

                  @rickw said:

                  So, there you have it - three solutions that avoid globals:

                  1. Sketchup.read_default and Sketchup.write_default (persistent across SketchUp sessions and models)
                  2. Attributes (persistent within a given model, between sessions)
                  3. Class variables (@@variable) (persistent only within a SketchUp session)

                  Thanks everyone; I took a quick look at TIG's, TextTag.rb. It appears to take a little bit more understanding of Ruby then just simply banging out a mass replacement of @ to $ or vise versa. πŸ˜„ Nevertheless I will try to implement
                  these attributes on an exisitng Ruby of mine.

                  [my plugins](http://thingsvirtual.blogspot.ca/)
                  tomot

                  1 Reply Last reply Reply Quote 0
                  • M Offline
                    Matt666
                    last edited by

                    Hi all !

                    Just one question : How can you do when you have that :

                    class ToolsObsTest < Sketchup;;ToolsObserver
                    	def onActiveToolChanged (tools_object, toolname, toolid)
                    		[b]@t[/b] = toolid
                    	end
                    end
                    

                    and this variable @t is used here :

                    module
                      def
                        [b]@t[/b]
                      end
                    end
                    

                    So the variable is not used inside the first class section, but inside the instance of a module section ???
                    To find variable, I use $....

                    How can I preserve variable value ????

                    Frenglish at its best !
                    My scripts

                    1 Reply Last reply Reply Quote 0
                    • AdamBA Offline
                      AdamB
                      last edited by

                      Just define your Observer class in the scope of your Module

                      module Foo
                      
                      class ToolsObsTest < Sketchup;;ToolsObserver
                         def onActiveToolChanged (tools_object, toolname, toolid)
                            @t = toolid
                         end
                      end
                      
                      end
                      
                      instance = Foo;;ToolsObsTest.new
                      

                      Developer of LightUp Click for website

                      1 Reply Last reply Reply Quote 0
                      • M Offline
                        Matt666
                        last edited by

                        Hi AdamB !
                        Thak you for your answer...

                        I tried it but it didn't work for me... @toolID (variable name in the code) always returns nil....
                        here is the real "tree" of the code (including your advice)

                        module Toto
                        	class ToolsObsTest < Sketchup;;ToolsObserver
                        		def onActiveToolChanged (tools_object, toolname, toolid)
                        			@toolID = toolid
                        		end
                        	end
                        	###
                        	def self.act
                        		model.tools.add_observer(Toto;;ToolsObsTest.new)
                        	end
                        	###
                        	def self.obs(id)
                        		@toolID
                        	end
                        end
                        

                        Do you know why your method doesn't work ?
                        Thank you ! πŸ˜„

                        Frenglish at its best !
                        My scripts

                        1 Reply Last reply Reply Quote 0
                        • fredo6F Offline
                          fredo6
                          last edited by

                          Matt,

                          The two @toolid are different. One is a Class instance variable, the other a Module variable.
                          If you want to track the toolid in module Toto, then use a method to set its value, which you can call from the class.
                          Note that normally, you might use a Module variable, with @@, (since module instance variables do not really have real application)

                          
                          def Toto.set_toolid(toolid)
                             @@toolid = toolid
                          end
                          def Toto.get_toolid()
                             @@toolid
                          end
                          
                          

                          Fredo

                          1 Reply Last reply Reply Quote 0
                          • M Offline
                            Matt666
                            last edited by

                            Hello Fredo6 !
                            Thank you for your answer ! It works great ! get_toolid & set_toolid are perfect !
                            Just one thing, @@variable doesn't work. Just @variable...

                            Thank you Fredo ! πŸ˜‰ πŸ˜„ πŸ˜„

                            Frenglish at its best !
                            My scripts

                            1 Reply Last reply Reply Quote 0
                            • 1 / 1
                            • First post
                              Last post
                            Buy SketchPlus
                            Buy SUbD
                            Buy WrapR
                            Buy eBook
                            Buy Modelur
                            Buy Vertex Tools
                            Buy SketchCuisine
                            Buy FormFonts

                            Advertisement