HISE Logo Forum
    • Categories
    • Register
    • Login

    Persistent Data Recommendation

    Scheduled Pinned Locked Moved Solved Scripting
    7 Posts 3 Posters 951 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.
    • HISEnbergH
      HISEnberg
      last edited by

      I have a few buttons on my UI and I want their data to persist before and after the UI is constructed/deconstructed. Just wondering what the best folder to dump JSON Data into would be? I imagine putting it in the AppData folder that is created would be the best but I am open to recommendations. Currently I am just saving it to the Expansions folder for no particular reason. Also any recommendations on best practices here is welcome!

      	const var STATE_FILENAME = "buttonStates.json";
      	const var btns= Content.getAllComponents("Button");
      	const var stateFilePath = FileSystem.getFolder(FileSystem.Expansions).getChildFile(STATE_FILENAME);
      // Presumably just change this to FileSystem.AppData
      	
          /** Save button states to file */
          inline function saveStates(states)
          {
              Engine.dumpAsJSON(states, stateFilePath.toString(0));
              return true;
          }
          
          /** Load button states */
          inline function loadButtonStates()
          {
              if (stateFilePath.isFile())
              {
                  return Engine.loadFromJSON(stateFilePath.toString(0));
              }
              
              return {};
          }
          
          /** Save state for a specific button */
          inline function saveButtonState(buttonId, state)
          {
              local states = loadButtonStates();
              states[buttonId] = state;
              return saveStates(states);
          }
          
          /** Get state for a specific button */
          inline function getButtonState(buttonId, defaultValue)
          {
              local states = loadButtonStates();
             
              if (isDefined(states[buttonId]))
              {
                  return states[buttonId];
              }
              
              return defaultValue;
          }
          
          /** Button callback function */
          inline function onButtonControl(component, value)
          {
              // Use component's ID as the button ID
              local id = component.getId();
              saveButtonState(id, value);
          }
          
          /** Initialize buttons on startup */
          inline function initializeButtons()
          {
              for (i = 0; i < btns.length; i++)
              {
                  local id = btns[i].getId();
                  local savedState = getButtonState(id, 0);
                  btns[i].setValue(savedState);
              }
          }
          
          // Set up callbacks for all buttons
          for (i = 0; i < btns.length; i++)
          {
              btns[i].setControlCallback(onButtonControl);
          }
          
          // Initialize buttons
          initializeButtons();
      
           
      }
      

      Sonic Architect && Software Mercenary

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

        @HISEnberg said in Persistent Data Recommendation:

        before and after the UI is constructed/deconstructed.

        What do you mean by this?

        Free HISE Bootcamp Full Course for beginners.
        YouTube Channel - Public HISE tutorials
        My Patreon - HISE tutorials

        HISEnbergH 1 Reply Last reply Reply Quote 0
        • HISEnbergH
          HISEnberg @d.healey
          last edited by

          @d-healey For each instance when the plugin is loaded (initiated)*

          Sonic Architect && Software Mercenary

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

            @HISEnberg Writing it to a file is the correct method, I'd put it in the app data folder. I'd use the File/FileSystem APIs rather than dumpAsJSON.

            Do you need special handling if there are multiple instances of the plugin in use?

            Free HISE Bootcamp Full Course for beginners.
            YouTube Channel - Public HISE tutorials
            My Patreon - HISE tutorials

            ustkU HISEnbergH 2 Replies Last reply Reply Quote 0
            • ustkU
              ustk @d.healey
              last edited by ustk

              @d-healey said in Persistent Data Recommendation:

              Do you need special handling if there are multiple instances of the plugin in use?

              That is the question that made me abandon this idea... Gosh... You have a solution for this?

              Hise made me an F5 dude, browser just suffers...

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

                @ustk I've seen someone else came up with a solution to give each instance a temporary unique id per session, but I haven't explored it myself.

                Free HISE Bootcamp Full Course for beginners.
                YouTube Channel - Public HISE tutorials
                My Patreon - HISE tutorials

                1 Reply Last reply Reply Quote 0
                • HISEnbergH
                  HISEnberg @d.healey
                  last edited by

                  @d-healey Wonderful I'll see if I can rewrite with just the File (System) APIs in that case! Luckily for me these are "universal settings" so they should be applied cross each instance of the plugin.

                  Sonic Architect && Software Mercenary

                  1 Reply Last reply Reply Quote 0
                  • HISEnbergH HISEnberg has marked this topic as solved on
                  • HISEnbergH HISEnberg referenced this topic on
                  • First post
                    Last post

                  24

                  Online

                  2.0k

                  Users

                  12.9k

                  Topics

                  111.7k

                  Posts