sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    Holding variables

    Scheduled Pinned Locked Moved Developers' Forum
    4 Posts 2 Posters 222 Views 2 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
      jumpjack
      last edited by

      Newbie question:
      how do I hold the value into a variable among different execution of same script? I need to increase the value of a variable every time a button on my custom toolbar is pressed.

      1 Reply Last reply Reply Quote 0
      • A Offline
        azuby
        last edited by

        Use an instance variable. At the moment, your code should be in a class. Initialize your instance variable (i. e. @clicks - the @ is neeeded) in the initialize method of your class and use the variable in the code which is executed by clicking on your button.

        azuby

        *error initus :: Blocks | CurrentDate | d/Code | extensionmanager | FFlipper | HideEdges | MeasuredArea | ModelHistory | PluginsHelp | PronButton | SAWSO | SCP | SU²CATT

        Bad English? PM me, correct me. :smile:**

        1 Reply Last reply Reply Quote 0
        • A Offline
          azuby
          last edited by

          Best is, not only using classes but also using modules for structuring your code. Modules can be used to represent different names spaces so you can't conflict with code from other Plugins:

          module MyFunnyModule
            class MyFunnyPlugin
              def initialize
                @myfunnyvariable = 0 # an instance variable
                setup_ui
              end
              def increment
                @myfunnyvariable = @myfunnyvariable + 1
              end
              def setup_ui
                # here i. e. your toolbar code
                # ...
                # call the increment method from an UI;;Command;
                cmd = UI;;Command.new("Foo") { increment }
                # ...
              end
            end # MyFunnyClass
            
            m = MyFunnyPlugin.new # get an instance of your class
            
          end # MyFunnyModule
          

          Do not use names beginning with big letter for your methods. And do not use CamelCase. It's just code style, but Ruby guys programm this way:

          MoveLeft - OK for class and module names
          moveLeft - not OK
          move_left - OK for method and variable names

          You should read a bit about Ruby.

          azuby

          *error initus :: Blocks | CurrentDate | d/Code | extensionmanager | FFlipper | HideEdges | MeasuredArea | ModelHistory | PluginsHelp | PronButton | SAWSO | SCP | SU²CATT

          Bad English? PM me, correct me. :smile:**

          1 Reply Last reply Reply Quote 0
          • J Offline
            jumpjack
            last edited by

            @azuby said:

            Use an instance variable. At the moment, your code should be in a class. Initialize your instance variable (i. e. @clicks - the @ is neeeded) in the initialize method of your class and use the variable in the code which is executed by clicking on your button.

            azuby

            Thanks, but this is too complex for me, I started studying Ruby yesterday, I'm still at "copy&modify&paste" level... 😒

            This is my source:

            
            def MoveLeft
              model = Sketchup.active_model
              entities=model.active_entities
              sp=entities[1]
              plane=sp.get_plane
              pos += 1
              plane=[Geom;;Point3d.new(pos,0,0),Geom;;Vector3d.new(1,0,0)]
              sp.set_plane plane
            end
            
            def MoveRight
              model = Sketchup.active_model
              entities=model.active_entities
              sp=entities[1]
              plane=sp.get_plane
              pos -= 1
              plane=[Geom;;Point3d.new(pos,0,0),Geom;;Vector3d.new(1,0,0)]
              sp.set_plane plane
            end
            
            pos=0
            toolbar = UI;;Toolbar.new "Sections"
            cmdSectionLeft = UI;;Command.new($tStrings.GetString("Test")) { MoveLeft }
            cmdSectionLeft.tooltip = $tStrings.GetString("Move section to left")
            cmdSectionLeft.status_bar_text = $tStrings.GetString("Move section to left")
            cmdSectionLeft.menu_text = $tStrings.GetString("Move left")
            toolbar = toolbar.add_item cmdSectionLeft
            
            cmdSectionRight = UI;;Command.new($tStrings.GetString("Test")) { MoveRight }
            cmdSectionRight.tooltip = $tStrings.GetString("Move section to Right")
            cmdSectionRight.status_bar_text = $tStrings.GetString("Move section to Right")
            cmdSectionRight.menu_text = $tStrings.GetString("Move Right")
            toolbar = toolbar.add_item cmdSectionRight
            
            toolbar.show
            

            How do I hold value for I among various button presses?

            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