Forums > Dev

umenu – symbol value

November 13, 2012 | 3:34 pm

Hey,

I’d like to get the value of a umenu as a symbol.
I can get the value index with object_getvalueof() but didn’t manage to get the symbol value.
I tried several things with object_attr… which didn’t work.

Greetings,
Alex


November 13, 2012 | 4:23 pm

You can look for the items attribute and extra the symbol from there.


November 13, 2012 | 8:58 pm

Thanks, but somehow I can’t get it working.

I tried:

void notify(t_iterator *x, t_symbol *s, t_symbol *msg, void *sender, void *data)
{
long a1 = 1; t_atom* rv = NULL;
object_getvalueof(sender, &a1, &rv);
long value = atom_getlong(rv);
post("%i value", value); //index value is correct

//I’d like to get this working for arrays I don’t know the size of before
//This only produces a,b,c,d …etc. analog to the indices 0,1,2,3 … for any values in the umenu

long a2 = 1; t_symbol* item_list = NULL;
object_attr_getsym_array(sender, gensym("items"), a2, &item_list);
t_symbol* name = &item_list[atom_getlong(rv)];

post("%s", name->s_name);

}


November 13, 2012 | 10:51 pm

You need to ask the current value of the items attribute so that should be something more or less like this (forum coding):

t_object *attr = object_attr_get(apointertotheumenuobject, gensym("items");
if (attr) {
	long ac = 0;
 	t_atom *av = NULL;
	object_attr_getvalueof(apointertotheumenuobject, attr, &ac, &av);
	if (ac && av) {
		... do something with it;
		sysmem_freeptr(av);
	}
}

November 13, 2012 | 11:04 pm

I coded this from the top of my head without testing it so use it with caution.
(‘sender’ should be a pointer to the umenu object)

void notify(t_iterator *x, t_symbol *s, t_symbol *msg, void *sender, void *data)
{
    long ac = 0;
    t_atom *av = NULL;
    long index;

    object_getvalueof(sender, &ac, &av);
    if (av) {
        index = atom_getlong(av);
        post("%i", index); //index value is correct
        sysmem_freeptr(av);

        ac = 0;
        av = NULL;
        object_attr_getvalueof(sender, gensym("items"), &ac, &av);
        if (av) {
            name = atom_getsym(av + index);
            post("%s", name->s_name);
            sysmem_freeptr(av);
        }
    }

    //I'd like to get this working for arrays I don't know the size of before
    //This only produces a,b,c,d ...etc. analog to the indices 0,1,2,3 ... for any values in the umenu
}

Let me know if it works…

- Luigi


November 14, 2012 | 3:42 pm

Thanks for the given ideas!…but it still does not work.

This time I tried:

void notify(t_iterator *x, t_symbol *s, t_symbol *msg, void *sender, void *data)
{
long ac = 0;
t_atom *av = NULL;
t_symbol* name = NULL;
long index;

object_getvalueof(sender, &ac, &av);
if (av)
{
index = atom_getlong(av);
post("%i", index); //index value is correct

ac = 0;
av = NULL;
object_attr_getvalueof(sender, gensym("items"), &ac, &av);
if (av)
{
// t_symbol* name = atom_getsym(av + index); //only gets the value at index 0 correctly
name = atom_getsym(&av[index]); //only gets the value at index 0 correctly
post("%s umenu value", name->s_name);
}
}
sysmem_freeptr(av);
sysmem_freeptr(name);
}

As mentioned in the comments like this I can get the first value correctly but no ones greater than 0.

Greetings,
Alex


November 14, 2012 | 3:46 pm

BTW: Is it a good habit to free ALL max local variables like this at the end of a function?


November 14, 2012 | 4:35 pm

It’s a good idea to free the memory that have been allocated ;-)

It looks like your problem is that you don’t get to ask the values on the correct object/attribute.


November 15, 2012 | 2:00 pm

…I started learning C just recently, doing mainly Java before. So, I’ll do that, thanks, :D

But I think if it would the wrong object/attr I would not always get the correct values for index 0.
So if I put "max is great" at index 0 the post function [post("%s umenu value", name->s_name)] shows exactly that string in the max console.
Anyway it’s not too bad for me to only work with the indices instead of strings. I’ll stick to that.

Greetings,
Alex


November 15, 2012 | 6:53 pm

I made a quick test with iterator2 from the SDK. By throwing this in the callback function:

if (object_classname(obj) == gensym("umenu")) {
	t_object *attr = NULL;
	object_post((t_object *)x, "umenu");

	attr = object_attr_get(obj, gensym("items"));
	if (attr) {
		long ac = 0;
		t_atom *av = NULL;
		object_attr_getvalueof(obj, gensym("items"), &ac, &av);
		if (ac && av) {
			long i;
			post("items attribute has %d elements", ac);
			for (i = 0; i < ac; i++)
				postatom(&av[i]);

			sysmem_freeptr(av);
		}
	}
}

And it worked perfectly, so it looks like you're not getting the pointer on the object/attr.


November 16, 2012 | 7:39 pm

Works! Ok, you were right about the pointer. Thanks a lot for helping!


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