Global Variables

“loop” is not a Ruby keyword (like it is in Pascal.)

The construct you show is simply a Ruby do … end block that evaluates only once.

Do you know any dialog that can keep my data and return it when I call dialog?

Dear Dan, As you know your words are so important for me and I started this topic because of your advice but if you let me this time I disagree you because of following reasons.
1- As Rumi says, the sun came because of the sun. In the following video I enter the wrong information and as you can see loop works well.
2- If you simply enter loop do and end (Endless loop) and run it, your SU will freezes.
3- I learn it from a ruby course.
4- Ruby’s loop is basically the same thing as the while( true ) - in fact, you can use while( true ) almost interchangeably with it.

Actually it is global method that I never noticed before.
Module: Kernel (Ruby 2.7.1)

I would say that using it is poor programming practice. Use while or until instead.

As I said in my first respone, you can use instance variables at your extension namespace module.

module Me
  module MyPlugin
    @some_variable = 1234

      def self.some_variable
        @some_variable
      end

      def self.some_variable=(some_variable)
        @some_variable = some_variable
      end
  end
end

In the rest of the code you can use MyPlugin.some_variable and MyPlugin.some_variable= to get and set the value.

This persists throughout the SketchUp session but not between sessions (just like the number of segments for native Circle tool).

Please forgive me if sometimes I understand something late. You are completely right. Following codes work exactly the same as Singleton and till we restart SU keep data. Thank you so much. I know also dialogs have the capability to keep data. Do you know any dialog that can keep data for us? dialog.show_modal cause SU freeze completely but if we run dialog.add_action_callback before it, dialog_callback is active even when SU is freeze. I believe dialogs can be very useful for us this is why I ask you again for it.

module MajidMahmoudi
  module MyPlugin
    @some_variable = 1234

      def self.some_variable
        @some_variable
      end

      def self.some_variable=(some_variable)
        @some_variable = some_variable
      end
  end
  module MajTest
    class MajTest
      def activate
        test = MyPlugin.some_variable
        p test
        MyPlugin.some_variable = "4321"
      end

    end #class MAJTest
    cmd1 = UI::Command.new("MAJ Test") {
      Sketchup.active_model.select_tool MajTest.new
    }
    cmd1.set_validation_proc { true ? MF_ENABLED : MF_GRAYED }
    cmd1.small_icon = "./images/small_icon_wall.png"
    cmd1.large_icon = "./images/large_icon_wall.png"
    cmd1.tooltip = "Wall Maker"
    maj_toolbar = UI::Toolbar.new "MAJ Test"
    maj_toolbar.add_item cmd1
    maj_toolbar.show
  end #MajWall
end #MajidMahmoudi#

I don’t know what you mean by tracking data by a dialog. Dialogs are user interfaces, not places for data storage.

Hi Majid,

Sounds to me like you are getting into the realm of Preferences.

In my plugin FlatText, I ended up keeping three sets of user preferences:

  1. The “factory defaults” that are hard coded into the plugin itself,
  2. The “model preferences” that are stored as attributes on a model, and
  3. The “default preferences” which are stored in an external file. I use a simple text file for this.

When FlatText needs to refer to the preferences, it first looks at the model to see if there is a preferences attributes dictionary on the model (“model preferences”). If there is none, it then looks for the external file of default prefs. If that does not exist, it then falls back to the factory default prefs.

I used to store the default preferences text file in the Plugins folder. The problem with this is that when the User upgrades SketchUp, they are in effect moving to a new Plugins folder, and the prefs file does not move with them. I solved this (with help from this forum) by putting my prefs files in a different directory than Plugins. Now it is available to any version of SketchUp that can run FlatText.

This all sounds complicated, and it is, but it is the sort of trouble we should go to as developers to make things simple and easy for our Users.

Hope this helps!

Dear friend,
Thank you so much for your information and for sure it will be very useful for my plugins. As I learned from Dezmo we can save our data in a special file by using the following codes. File address is “c:\Users\USER_NAME\AppData\Local\SketchUp\SketchUp2020\SketchUp\PrivatePreferences.json”
and data saved like this…
“MajWall”: {
“0”: “"7.0,125.0,10.0,3.0,0.0,0.0,0.4,0.0,0.0"”,
“3”: “"20.0,315.0,0.0,5.0,0.0,0.0,1.0,0.0,0.0"”,
“4”: “"0.2,3.2,0.0,0.05,0.0,0.0,0.01,0.0,0.0"”
},
As you said this file will clear when we upgrade SU and it is possible to use a special file and save our information there. Would you please let me know by which code we can save data and how to read data? If your solution is JSON codes, please let us know (I think JSON also can solve this problem). Thank you in advance.

I’m sure JSON will work just fine. I’m just partial to .txt files.
As for the file location, I make a “company” directory and put my prefs files in a subdirectory named for the plugin. (My company directory contains prefs directories for several of my plugins.) The company directory is in the same directory that the SketchUp app uses for its own prefs (including Plugins).
Here’s the code I use:

    appDataStr = ''
    if Sketchup.platform == :platform_win # Windows
      appDataStr = ENV['APPDATA'].tr File::ALT_SEPARATOR, File::SEPARATOR
    else # Mac
      appDataStr = File.expand_path('~/Library/Application Support')
    end
    COMPANY_DIR = File.join( appDataStr, COMPANY_APP_DIR )
    PATH_PREFS  = File.join( COMPANY_DIR, PLUGIN_NAME )

This code is in the unencrypted .rb file named for the plugin (the one that’s NOT in the plugin’s folder of the same name) so that a sophisticated user can edit the paths, to something like a central prefs directory on a shared server, for example.
In my plugin’s (encrypted) code, I use the COMPANY_DIR and PATH_PREFS constants to look for, and if necessary create, my company directory and my prefs directory.

[quote=“majid866, post:50, topic:147071”]
Thank you in advance.

The above is wrong because the key name is not the same in both method calls.

You would use either "key" or "variable" but not both.