Variable Persistence
-
Thanks Christoph, I think I get it (again). This is what happens when I stop coding in HISE for a month I forget everything
-
What's the advantage of using an inline function for the control callback over putting it in the usual onControl callback?
-
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 :)
-
@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?
-
@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.
-
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!
-
@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.
-
"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()
-
@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); };
-
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]; } }