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.
    • 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