Forums > Dev

string attribute

Jan 02 2011 | 12:34 am

i want to declare a string attribute in the way annotation or hint is defined
(any string including blanks can be entered by the user).

which type should i use – i guess t_symbol ??
which CLASS_ATTR is to be used here ?

thanks a lot

Jan 02 2011 | 12:10 pm

i tried this before, but like this, i loose all values after a blank.
a typed "a b c" turns into "a" in the inspector.
maybe i miss another thing?
(its hard but fun learning C and the MaxAPI at the same time..:)

Jan 03 2011 | 5:21 pm

thanks nicolas!
actually i can live with the parenthesis of course.
i was just wondering how they are avoided at annotation.
currently i am more trying to parse these items into an atomarray, which is still blowing my head…

Jan 03 2011 | 6:27 pm

i am stuck here:
i do get my attributes into a setter.
(i am expecting any types, int, float symbol, so i defined the attribute as t_atom)
in the end, i want an array filled with the arguments of the attribute.
here is my clumsy try…
thanks for having a look

t_atom myattr;

t_max_err myobj_setattr_myattr(t_myobj *x, void *attr, long ac, t_atom *av)
	long i;
	t_atom *ap;
	t_atom args[16];

	post("there are %ld arguments",ac);

	for (i = 0, ap = av; i < ac; i++, ap++) {
		//all fine until here
		args[i] = ap;
		//gives unmatched type in compiler
		post("na? %s",atom_getsym(ap));
		//gives nonesense in the max window

	atom_setsym(&x->myattr, atom_getsym(ap));
	//just a try to set the attribute back into the inspector, since it turns "undefined" with this setter. 

	return MAX_ERR_NONE;
Jan 03 2011 | 6:54 pm

Hi Klaus.

Your problem here is that you’re assigning an atom pointer (ap) to an atom variable (args[i]). This is what your compiler is trying to tell you.

What you want to do is
args[i] = *ap

btw, you can copy all your atoms in one blow with
sysmem_copyptr(args, ap, ac * sizeof(t_atom));
provided that you have allocated enough memory (a check for ac being < 16, or a dynamic allocation based upon ac would be definitely recommended!)


Jan 03 2011 | 9:56 pm

thanks andrea,
using the pointer definitely helped.))
strangely the one-blow did not work out for me.
(crashes max)

thanks nicolas, but CLASS_ATTR_ATOM_ARRAY produces a defined amount of "undefined" in the inspector window, i don’t want to predefine this amount…

i am still stuck, how to parse my atoms into a string.
with this i could set-back the attribute:

atom_setsym(&x->myattr, "string of all my agrguments");

sprintf(str, "%s", ,atom_getsym(ap))in the for-loop above does not give the expected result.


Jan 03 2011 | 10:32 pm

oups, got it:
post("currentarg %s", atom_getsym(ap)->s_name);

hope to come with a ready code-sniplet soon


Jan 03 2011 | 11:57 pm

this works
thanks a lot

t_atom myattr;
t_atom myatomarray[1024];

CLASS_ATTR_ATOM(c, "myattr", ATTR_FLAGS_NONE, t_myobj, myattr);
CLASS_ATTR_ACCESSORS(c, "myattr", (method)NULL, (method)myobj_getattr_myattr);

t_max_err myobj_getattr_myattr(t_myobj *x, void *attr, long ac, t_atom *av)
	long i;
	t_atom *ap;
	char str[16] = "";
	char allstr[1024] = "";
	for (i = 0, ap = av; i < ac; i++, ap++) {
		x->myatomarray[i] = *ap;
		switch (atom_gettype(ap)) {
            case A_LONG:
				sprintf(str, "%ld ", atom_getlong(ap));
			case A_FLOAT:
				sprintf(str, "%f ", atom_getfloat(ap));
            case A_SYM:
				sprintf(str, "%s ", atom_getsym(ap)->s_name);
				post("%ld: unknown atom type (%ld)", i+1, atom_gettype(ap));
		strcat(allstr, str);
	post("all %s", allstr);
	atom_setsym(&x->myattr, gensym(allstr));
	return MAX_ERR_NONE;
Jan 04 2011 | 8:06 am

Hi Klaus
of course the one-blow solution won’t work… it should be
sysmem_copyptr(args, av, ac * sizeof(t_atom));
– av, not ap! in fact, since you copy all your atoms at once you don’t need the loop or ap anymore!
but if you want to post your atoms one after the other, or strcat them together, you need the loop.

one more observation: in some cases, your loop might not do what you want.
e.g., it won’t take into account multiple spaces into your input string; it won’t behave as expected with floating point numbers (you might find that 0.123 changed into 0.129999 or something like that); it won’t deal with leading 0s in numbers (0123 changed into 123)…
In fact, I suspect that the only solution for having a string with spaces is to quote it, and treat it as a single symbol.


Jan 07 2011 | 3:54 pm

thanks nicolas,
this looks very very good to me!

Viewing 10 posts - 1 through 10 (of 10 total)

Forums > Dev