Questions about handling persistent data in panels
-
So I have a bit of data that I need to keep persistent so it loads every time without the user having to save a preset.
Most of it is in multi-dimensional arrays.
I have been using the panel.setValue() and panel.getValue() to handle this data,
but the Docs say this:"It's heavily recommended that this will be just a simple number, but you can choose to use more complex types if your widget that demands that (we'll cover this case in a example later on). But even then it might be more efficient to store the actual value as an array in the data object and use the Control Value as index:"
// Not very efficient this.data.setValue("First Item"); // Better, as it doesn't need to create the string each time this.data.values = ["First Item", "Second Item", "Third Item"]; this.data.setValue(0);
So...
- I don't understand why anything other than a single number value is discouraged.
- I don't understand the example above, as I don't see where it is even comparing the methods as the first method is "this.data.setValue()" which is not either method? Seems to be a hybrid of the two ("UI" data and "Control" data)?
Underneath that I see so clear way the two lines are related. How is "this.data.setValue(0)" acting on the previous line?
It seems that somehow the suggestion is to store data some data as "Control" data then have it populate analogs of that data on the panel.data by putting an inline function on the "OnControl" callback.
There is later an example using the ButtonPack doing this sort of thing, but I have to ask what is the utility of having to get and set the data from the panel.setValue() and panel.getValue() versions to their panel.data versions? Wouldn't it be easier to just store the data directly using the setValue() method and retrieve using the getValue() method, using less code and having less redundancy? It seems like it would be less efficient to have to store data to 2 separate places every time you are recording/editing data that is to be persistent.
-
@VirtualVirgin said in Questions about handling persistent data in panels:
But even then it might be more efficient...
I think the word here is efficiency, so it's not discouraged as I understand it. I use object stored in panel's value for a very long time and I'm sure many of us are doing so.
The example looks erroneous, it should probably be:
this.data.values = ["First Item", "Second Item", "Third Item"]; Panel.setValue(this.data.values[0]); // <- so not in the data object
But, since the
data
object isn't persistent, it would make even more sense to hard script the values:const var values = ["First Item", "Second Item", "Third Item"]; // script the array if the project allows for it... Panel.setValue(values[0]); // <- so not in the data object
But, as I said at first, storing an object directly to the
value
works on is own (even with a bit of efficiency drawback) so if this is what you need, you can go for it -
@ustk
Thank you very much for that response!I have been planning on more data storage using the panel.setValue() method and wanted make sure I'm not shooting myself in the foot with it, so after reading that section in the Docs (probably for the 5th or 6th time now) I was just getting confused.
It's great to get some help and be able to move past a sticking point :)
Much appreciated. -
@ustk You can't store strings to
value
. I believe the example was assuming that when retrieving the value, you'd gothis.data.values = ["First Item", "Second Item", "Third Item"]; this.data.setValue(0); var gimmeTheValue = this.data.values[this.data.getValue()];
More generally I think Christoph was getting at the idea of only storing integers to
value
whenever possible, and using a lookup table if you need something more complex.That said I've been using a panel to store a big ol object with nested objects and arrays, and haven't noticed anything suspicious, so I'm not sure if he knows something we don't or if it's just cleaner that way.
I tried to provide some clarity to the docs here, feel free to contribute: https://github.com/christophhart/hise_documentation/pull/48
-
@Simon Oh absolutely yes! My mistake!
You can store objects, not strings. My example is very bad, I should have placed the string in an object firstconst var values = ["First Item", "Second Item", "Third Item"]; // script the array if the project allows for it... Panel.setValue([values[0]]); // or Panel.setValue({myVal: values[0]});
-
I would guess you can store references to global objects inside a panel right?
So you could have a global array, store a reference to it in this.data.values, and then access that array from inside the panel by setting and getting a value?
So...
const global_array = [0, 1, 2, 3, 4, 5]
Then later:
this.data.values = [global_array]; this.data.setValue(0); var access_global_array = this.data.values[this.data.getValue()]; Console.print(access_global_array[3]);
Would something like that work?
-
@Simon said in Questions about handling persistent data in panels:
@ustk You can't store strings to
value
. I believe the example was assuming that when retrieving the value, you'd gothis.data.values = ["First Item", "Second Item", "Third Item"]; this.data.setValue(0); var gimmeTheValue = this.data.values[this.data.getValue()];
More generally I think Christoph was getting at the idea of only storing integers to
value
whenever possible, and using a lookup table if you need something more complex.That said I've been using a panel to store a big ol object with nested objects and arrays, and haven't noticed anything suspicious, so I'm not sure if he knows something we don't or if it's just cleaner that way.
I tried to provide some clarity to the docs here, feel free to contribute: https://github.com/christophhart/hise_documentation/pull/48
So I am confused on the purpose of this method...
If the set/getValue() is only going to be storing an index of an array and not the actual array value, then how does the value "persist" and load on compile?
"this.data.values" array will just get wiped on compile so if that array is supposed to be dynamic, then none of the changes actually "persist" to get reloaded.