Holding variables
-
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. -
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
-
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 namesYou should read a bit about Ruby.
azuby
-
@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?
Advertisement