3 Replies Latest reply on Oct 17, 2014 12:07 PM by Serhit

    How to keep a few values between calls to the menu handlers?

    TigrouMeow Level 1

      Hello,

       

      I am displaying a dialog when clicking on a custom LrLibraryMenuItem.

       

      LrLibraryMenuItems = {

        title = 'My Dialog',

        file = 'MyDialog.lua',

        },

       

      When I close this dialog I don't want the values in it to be lost. I cannot save them through LrPrefs because those values are usually a LrTasks or a LrProgressScope.

       

      Maybe there is a global scope I can use throughout my plugin?

       

      Thanks for your help again

        • 1. Re: How to keep a few values between calls to the menu handlers?
          johnrellis Most Valuable Participant

          Yes, the global scope for your plugin persists across invocations of the plugin (until LR exits).  So if your code refers to the variable "x", and x is not declared in that scope as a function parameter or with "local", then that "x" is in the global scope and its value persists across invocations of the plugin.

           

          If you are using "strict" to detect undeclared uses of globals, and you want the value of the global "x" to persist across plugin invocations, you can do something like this at the top of the file that "declares" x:

           

          x = rawget (_G, "x") == nil and 1 or rawget (_G, "x")

           

          The first time the file is loaded, the global "x" is assigned 1; on subsequent loads, the current value of "x" is left undisturbed.

          • 2. Re: How to keep a few values between calls to the menu handlers?
            TigrouMeow Level 1

            Thanks a lot, I didn't knew about rawget and I am using it from now.

             

            I actually tried to use global variable before (without strict) but it wasn't working for one specific reason : the "Reload plug-in on each export" was checked! That also resets the whole plugin every time a plugin menu handler is called. I only noticed that yesterday

            • 3. Re: How to keep a few values between calls to the menu handlers?
              Serhit

              Actually, you may also want to look into LrPrefs package - it let you persist the "preferences" for plugin in the form of table.

               

              propertyTable = import 'LrPrefs'.prefsForPlugin()