Saving data with patch
I'd like to save some datas of an ui external (essentially a dynamic array of longs) with the patcher, so that they are automatically restored on reload.
It should be the same thing as preserving values on copy/paste, so I've seen the thread https://cycling74.com/forums/function-copypaste where emmanuel suggested to add a method
void yourobject_jsave(t_yourobject *x, t_dictionary *d){
dictionary_appendlong(d, gensym("hey"), 74);
}
I did it: the method is correctly called, but still the message "hey" with argument 74 is never executed, neither on pasting, nor on reloading. (obviously i've also created a "hey" method, A_LONG - but it is never evaluated).
Shouldn't the "hey 74" be parsed automatically at the object initialization with attr_dictionary_process(x, d)? Am I wrong in some part of the process?
Thanks a lot,
Daniele Ghisi
Hi,
Is your object a UI object?
Cheers
The jsave method only works for ui objects, so that's why it did not work for you.
For non-ui objects you should instead use the appendtodictionary method. In your class definition:
class_addmethod(c, (method)myobj_appendtodictionary, "appendtodictionary", A_CANT, 0);
Then you can implement the method like this:
void myobj_appendtodictionary(t_myobj *x, t_dictionary *bd)
{
// bd is a dictionary for our object that is going to be stored in the patcher
// we can append data to the dictionary using the usual dictionary methods
t_atom a[2];
atom_setlong(a+0, 1234);
atom_setsym(a+1, gensym("foo"));
dictionary_appendatoms(bd, gensym("my_data"), 2, a);
}
To restore the data when the patcher is opened (and your object is created, you can get the dictionary for your object in the new method using the #D symbol like this:
t_dictionary *d = (t_dictionary *)gensym("#D")->s_thing;
if (d)
// do something with the dictionary, like get the atoms associated with the "my_data" key
HTH,
Tim
Hey, hopefully this isn't too old but what if I want something like appendtodictionary above, but the name of the dictionary is specified by an argument to the object like a buffer? Therefore if I have multiple instantiations of the object, they each can use their own database...
Thanks!
@timothy is this method still a thing? I know I've seen it but I can't find any documentation of embedding a dictionary in a patcher. Like @embed 1
Hi...
"appendtodictionary" is still a thing, yes. You should be able to call something like dictionary_appenddictionary() within that method to add your dictionary to the patcher dictionary.
Cheers