HISE Logo Forum
    • Categories
    • Register
    • Login

    Variable Persistence

    Scheduled Pinned Locked Moved Scripting
    29 Posts 4 Posters 5.5k Views
    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.
    • d.healeyD
      d.healey
      last edited by

      I've found a small issue with the solution. Check out this example:

      function myModule()
      {
      	this.chicken = "Bird";
      
      	this.getChicken = function()
      	{
      	    return this.chicken;
      	};
      }
      
      var module = new myModule();
      module.chicken = "Dog";
      Console.print(module.chicken);
      

      chicken should be a private member that the user doesn't have direct access to but using the above could makes everything public. In my previous example I was using an iife (hence the extra brackets) which would usually allow private and public members but it wasn't working as I expected in HISE.

      The other solution I've tried is to declare chicken as var chicken = "Bird" which should make it accessible from within the getChicken function but not from outside of myModule. Is there a solution that would provide both private and public members?

      Libre Wave - Freedom respecting instruments and effects
      My Patreon - HISE tutorials
      YouTube Channel - Public HISE tutorials

      1 Reply Last reply Reply Quote 0
      • Christoph HartC
        Christoph Hart
        last edited by

        Hmm, do you really need this level of encapsulation? TBH I think Javascript isn't designed for this language paradigm and the solutions are kind of hacky.

        For >200.000 line projects written in C++ encapsulation is a must but for scripts it seems a bit over the top.

        Why do you bother about this (is there a special use case that I am not seeing that definetily needs private members?

        1 Reply Last reply Reply Quote 0
        • d.healeyD
          d.healey
          last edited by

          I can certainly move on without it, I'm just being pedantic really because I like encapsulation. My main purpose for this is for the framework I'm putting together so I can keep all my namespaces separate and safe. I think as long as I put comments to make sure the classes are used as intended everything should be fine.

          Libre Wave - Freedom respecting instruments and effects
          My Patreon - HISE tutorials
          YouTube Channel - Public HISE tutorials

          1 Reply Last reply Reply Quote 0
          • Christoph HartC
            Christoph Hart
            last edited by

            This advice gives the most encapsulation for the least complicated implementation IMHO.

            Possibly the best known way to achieve some form of privacy in JavaScript is to simply prefix all “private” members with “” or something similar. It’s very comfortable to use since it doesn’t add much (or any) complexity to your code, and requires no additional operations or memory at runtime. The obvious downside is that it doesn’t provide any real privacy at all, it only marks the members as “don’t touch that” for any developers working with the code. It’s what I do most of the time, though there is a minor variation that I have been trying out: Instead of prefixing the actual properties, I create a single property called “” which is an object that holds all the other “private” properties. (Let’s call that Method 1b.)

            (taken from here)

            1 Reply Last reply Reply Quote 0
            • d.healeyD
              d.healey
              last edited by

              I like that idea, nice and simple. Thanks for bringing it to my attention, I shall use it!

              Libre Wave - Freedom respecting instruments and effects
              My Patreon - HISE tutorials
              YouTube Channel - Public HISE tutorials

              1 Reply Last reply Reply Quote 0
              • Christoph HartC
                Christoph Hart
                last edited by

                Alright the glitch is fixed (I didn't find out the exact reason why it did that, but it should not occur again)...

                1 Reply Last reply Reply Quote 0
                • d.healeyD
                  d.healey
                  last edited by

                  Excellent!

                  Libre Wave - Freedom respecting instruments and effects
                  My Patreon - HISE tutorials
                  YouTube Channel - Public HISE tutorials

                  1 Reply Last reply Reply Quote 0
                  • d.healeyD
                    d.healey
                    last edited by

                    I remember getting on with this before but I'm working on a little module now and the persistent storage doesn't seem to be working for me. Would you mind taking a look when you have the time? This module is an articulation switcher that seeks out containers that have a midiMuter with the same name as the container and counts them as articulations which can be muted or unmuted.

                    HiseSnippet 2683.3oc6astbaabEFTRH0j1HMIi+Qm9qsb5jP5KTjV4Vkpq0U6xwVwbDk8zY73IcEvRxMBDfC1ERVwidm5iPej7aP64rKtRAJAYSKmKVw1ivhy4re645GHX5E3ayDB+.iJWe+SlvLpbCy9m3IGs0HJ2yn61FU9DSISHM17jITgf4XToxhOBuWkpKYn94MOXSpK0ylktjgwy841rmvGykoq1a8GycceH0gsOebFo+506Z66skuqeHfiEMaaLgZeHcH6GnnXKXZbDmcrvnRayUtWqfC6bvOuQqje1W8ui1czeSzGWda7xdJALrCCBXdxmCpaTwrx+C9oh4NNboePeIENWvpa56bR+Q9G6o25myE7CbY3EcL5CXRu7C8ccvCO96c8jrfATaVF0L1ZD20oWr6TX.VtWpycQsy8ll6xc3Iqm5j+L0MH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rd2N34aqnEOSHS2XKgcsdzSjIdITzc6NM0ac03FSIxlWTLNABOSX93H3koeThx4wZeINBU.nT6SgztwTANDB6cmevVFjlwH47ghDe3czoup38DGraZ5iwDG.lE7wit9.fGzhPdAfJh62bFVpLxy7d1pUtWjksl5VFA8855wkOcBK5EzEOD4LqLXP1kRKBytppxn.whWpvWlYDcRCiJKEINHpL5ErF8Vzvmexf6fucu7O6ogx6l88WZLsQ9rHiDyUUYmaj9MSPLsMdyCJqMRIM+1ZiOwD6XUf1e7k4+6vWl+M0P4KL6Kgp8wbugQOz9GxWoeu.lqO0oO+myH1+c8MCA2UP9U+Oqq1pMFCSiysWQGi8XSXTID8yAu816QA9gSlVq27.Utx9A.8SvSj8FO0C+RxIyZjsB7EhAvwQYKQ1a0KLXH5qxririf597qAzxfPuGznA+56UoBrjF06Rmn91BN8WwuqUtuZCf5p1WYxS1jOLiowUxWDmQzji093yHjY2N+jpJexud9FSDAuqal7oyD8ckH45hR6md.3hkCWm67mEltk8mF0xdyPoD5UgMr+TSt5g7iV5hG+TfM9ilC3upuLz9Pbxpn.ibk08ewx0B6BBg+gXL1mio063cDzx.VQ2xeaMy9jUuPPBLzgZ9zUt456AzAApXoK8WW+IvraZP9dIWpyWmxe9NuXvexTCWB1Gk7aiIwK969IwmmqCpn0eP8vtGH+Edb9FlpAhDbR+GJntmenDlfuKEdv6WAyQgQs8A5E1rrCbW.6cputMdMBn9LOm1weE6itYmjoyvM6DeSC6XSAmsneW87AUVj4o930PBltvbe2rozBfDCBIX7tg90gLFGIuD54f05SOho6vnwyLYprK2tmO3LwOzSzMr1Uyg92J6wXpcf+OZqmbhgvqoVA7jdp+e0np4t30jNwiJwjN78C7i11X2g6BocEqw8tzZrxkViu9Rqw2bo03auzZ7cWZM99yQCjK1FgR+w5YEFF+e8fXD6B
                    

                    Libre Wave - Freedom respecting instruments and effects
                    My Patreon - HISE tutorials
                    YouTube Channel - Public HISE tutorials

                    1 Reply Last reply Reply Quote 0
                    • Christoph HartC
                      Christoph Hart
                      last edited by

                      I think the problem is that you are storing the initial object in the onInit callback which gets overwritten by any value in the onControl callback of the ScriptPanel.

                      A better solution is to use a dedicated variable for the object and think of the panel as "restorer" (not as actual container for data). This script might explain this idea:

                      /** The restorer.
                      *
                      *    This panel will restore the storage object in its control callback. If the value is not an object, it won't do anything but use the given storage variable as value (see the onControl callback)
                      */
                      const var restorer = Content.addPanel("restorer", 0, 0);
                      restorer.set("saveInPreset", true);
                      restorer.set("visible", false);
                      
                      /** The data object. This will be overwritten by the onControl callback of the restorer */
                      reg storage = {};
                      
                      /** Checks if a given variable is an object. */
                      inline function isObject(variable)
                      {
                          return typeof(variable) == "object";
                      };
                      
                      const var Knob = Content.addKnob("Knob", 0, 0);
                      
                      // Make the knob non persistent for demonstrating purposes
                      Knob.set("saveInPreset", false);
                      
                      
                      inline function onKnobControl(component, value)
                      {
                          // Just use the storage object (ignore the restorer)
                          storage.a = value;
                      };
                      
                      Knob.setControlCallback(onKnobControl);
                      
                      function onNoteOn() {}
                      function onNoteOff() {}
                      function onController() {}
                      function onTimer() {}
                      function onControl(number, value)
                      {
                          if(number == restorer)
                          {
                              // Check in the callback if the value is an object
                              // If not, set the value to the storage object
                              
                              if(isObject(value))
                              {
                                  // Copy the object from the restorer to the storage variable
                                  storage = value;
                                  Console.print("Restoring storage");
                                  
                                  Console.print("The restored knob value is: " + storage.a);
                              }
                              else    
                              {    
                                  restorer.setValue(storage);
                                  Console.print("Initialising restorer");
                              }
                          }
                      }
                      
                      d.healeyD 1 Reply Last reply Reply Quote 0
                      • d.healeyD
                        d.healey
                        last edited by

                        Thanks Christoph, I think I get it (again). This is what happens when I stop coding in HISE for a month I forget everything

                        Libre Wave - Freedom respecting instruments and effects
                        My Patreon - HISE tutorials
                        YouTube Channel - Public HISE tutorials

                        1 Reply Last reply Reply Quote 0
                        • d.healeyD
                          d.healey
                          last edited by

                          What's the advantage of using an inline function for the control callback over putting it in the usual onControl callback?

                          Libre Wave - Freedom respecting instruments and effects
                          My Patreon - HISE tutorials
                          YouTube Channel - Public HISE tutorials

                          1 Reply Last reply Reply Quote 0
                          • Christoph HartC
                            Christoph Hart
                            last edited by

                            It might be a bit faster with big scripts because it doesn't need to resolve the big switch statement. Also you can assign the functions dynamically but in the end it's just a matter of taste :)

                            1 Reply Last reply Reply Quote 1
                            • LindonL
                              Lindon @Christoph Hart
                              last edited by

                              @Christoph-Hart So if I :

                              this.panel.set("saveInPreset", false);

                              in your function - do i get to persist a piece of data but NOT store it in any preset?

                              HISE Development for hire.
                              www.channelrobot.com

                              d.healeyD 1 Reply Last reply Reply Quote 0
                              • d.healeyD
                                d.healey @Lindon
                                last edited by

                                @Lindon It's been a long time since I used this since I haven't needed to persist any data that wasn't stored already in another control. What's your specific use case? it will probably turn out there is a more straightforward solution.

                                Libre Wave - Freedom respecting instruments and effects
                                My Patreon - HISE tutorials
                                YouTube Channel - Public HISE tutorials

                                1 Reply Last reply Reply Quote 0
                                • D
                                  dxmachina
                                  last edited by dxmachina

                                  Hi @d-healey @Christoph-Hart ,

                                  Reading through these solutions for data persistence and wanted to ask about scale. Would this still be the approach if you were storing 1,000,000 points of data? And before you ask "is it really necessary" let's assume it is. :)

                                  Similarly, is there any solution for serialization directly to/from disk to avoid storing that quantity of data in every preset (when such data is shared among multiple presets?

                                  Thanks!

                                  d.healeyD LindonL 2 Replies Last reply Reply Quote 0
                                  • d.healeyD
                                    d.healey @dxmachina
                                    last edited by

                                    @dxmachina said in Variable Persistence:

                                    Would this still be the approach if you were storing 1,000,000 points of data?

                                    Try it and report back.

                                    Libre Wave - Freedom respecting instruments and effects
                                    My Patreon - HISE tutorials
                                    YouTube Channel - Public HISE tutorials

                                    1 Reply Last reply Reply Quote 0
                                    • LindonL
                                      Lindon @dxmachina
                                      last edited by

                                      @dxmachina

                                      "Similarly, is there any solution for serialization directly to/from disk to avoid storing that quantity of data in every preset (when such data is shared among multiple presets?"

                                      Theres the all new saveAsJSON() and loadFromJSON()

                                      HISE Development for hire.
                                      www.channelrobot.com

                                      1 Reply Last reply Reply Quote 0
                                      • d.healeyD
                                        d.healey @Christoph Hart
                                        last edited by d.healey

                                        @Christoph-Hart said in Variable Persistence:

                                        I think the problem is that you are storing the initial object in the onInit callback which gets overwritten by any value in the onControl callback of the ScriptPanel.

                                        A better solution is to use a dedicated variable for the object and think of the panel as "restorer" (not as actual container for data). This script might explain this idea:

                                        This script doesn't seem to work as expected. The first time I set the knob to a value and hit compile the console shows the restored value correctly. But, after that the stored value can't be changed.

                                        I found a fix, update the panel's value after changing the storage object.

                                        inline function onKnobControl(component, value)
                                        {
                                            // Just use the storage object (ignore the restorer)
                                            storage.a = value;
                                            restorer.setValue(storage);
                                        };
                                        

                                        Libre Wave - Freedom respecting instruments and effects
                                        My Patreon - HISE tutorials
                                        YouTube Channel - Public HISE tutorials

                                        1 Reply Last reply Reply Quote 0
                                        • d.healeyD
                                          d.healey
                                          last edited by

                                          I wrapped it into a namespace with some get/set helper functions.

                                          namespace PersistentData
                                          {
                                              //The data object. This will be overwritten by the onControl callback of the restorer
                                              reg _persistentData = {};
                                           
                                              /*This panel will restore the storage object in its control callback. 
                                              If the value is not an object, it won't do anything but use the given
                                              storage variable as value*/
                                              const var pnlDataRestorer = Content.addPanel("pnlDataRestorer", 0, 0);
                                              pnlDataRestorer.set("saveInPreset", true);
                                              pnlDataRestorer.set("visible", false);
                                              
                                              pnlDataRestorer.setControlCallback(onpnlDataRestorerControl);    
                                              inline function onpnlDataRestorerControl(component, value)
                                              {
                                                  // Check if the panel's value is an object
                                                  // If not, set the value to the storage object
                                                  
                                                  if (typeof(value) == "object")
                                                      _persistentData = value; //Copy the object from the panel to the storage object
                                                  else    
                                                      component.setValue(_persistentData); //Initialize the restorer
                                              };
                                              
                                              inline function set(id, value)
                                              {
                                                  _persistentData[id] = value;
                                                  pnlDataRestorer.setValue(_persistentData);
                                              }
                                              
                                              inline function get(id)
                                              {
                                                  return _persistentData[id];
                                              }
                                          }
                                          

                                          Libre Wave - Freedom respecting instruments and effects
                                          My Patreon - HISE tutorials
                                          YouTube Channel - Public HISE tutorials

                                          1 Reply Last reply Reply Quote 1
                                          • First post
                                            Last post

                                          53

                                          Online

                                          1.7k

                                          Users

                                          11.7k

                                          Topics

                                          101.8k

                                          Posts