Saving data with patch

May 24, 2010 at 2:49pm

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 http://cycling74.com/forums/topic.php?id=26378 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

#50535
Jul 19, 2010 at 5:28am

Hello maxers,

I want to save an array of double with the patch, as CLASS_ATTR_SAVE does ;

but as i don’t know how many values : i want to use a dynamic array to collect them ; (weights of a neural network so i don’t want / need user interaction) ….

so :

- have you any clue/strategy/idea for saving that kind of stuff ? (my external is not UI).

- in case of using CLASS_ATTR_DOUBLE_ARRAY ; is it safe to deal with big size array (thousands of values) ?

Thanks.

#181287
Jul 23, 2010 at 12:25pm

Hi,

Is your object a UI object?

Cheers

#181288
Jul 23, 2010 at 2:22pm

Hello Timothy,

NO, that’s not an UI.

i’m doing an absurd/nut external who scan the patch and create a “neural network” with jbox and jpatcherline ; and my problem was to save synapse weights with the patcher.

Finally i start to do it using two CLASS_ATTR_DOUBLE_ARRAY in a very very complex approach ;-) I set the size of these arrays : 2048 ; hope that’s not too much.

Another idea (should be great, because what i’m currently doing is crazy) ?

#181289
Jul 23, 2010 at 4:27pm

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

#181290
Jul 24, 2010 at 10:58am

Hello Timothy,

Thank you ; much more easy to code my external now.

#181291
Aug 26, 2010 at 8:39am

Hello maxers,

i implement a SaveDataWithPatcher stuff with Tim’s appendtodictionnary tips ; but i suspect this to crash sometimes my [papa] external (once for now freeing the patcher, but …) ; is it possible than a thread conflict between myobj_free function and myobj_appendtodictionnary function occurs as both are referring to the same data structure ? or are they called strictly one before the other ?

thanks for help.

#181292
Jan 6, 2011 at 6:42pm

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!

#181293
Jan 7, 2011 at 3:18pm

Hello gerladhammings,

i was not sure so i did a quick test (mac only, sources with) ; keys you add to the t_dictionary is local for each instantiation of your object so each object can use/save is own database ; Somebody to correct me ?

but the name of the dictionary is specified by an argument to the object like a buffer ?

i don’t really understand what you mean with “the name of the dictionary” ;

t_dictionary *d = (t_dictionary *)gensym("#D")->s_thing ;

HTH

Attachments:
  1. Dico.zip
#181294

You must be logged in to reply to this topic.