umenu – symbol value

Nov 13, 2012 at 3:34pm

umenu – symbol value

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

#65199
Nov 13, 2012 at 4:23pm

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

#234972
Nov 13, 2012 at 8:58pm

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);

}

#234973
Nov 13, 2012 at 10:51pm

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);
	}
}
#234974
Nov 13, 2012 at 11:04pm

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

#234975
Nov 14, 2012 at 3:42pm

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

#234976
Nov 14, 2012 at 3:46pm

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

#234977
Nov 14, 2012 at 4:35pm

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.

#234978
Nov 15, 2012 at 2:00pm

…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

#234979
Nov 15, 2012 at 6:53pm

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.

#234980
Nov 16, 2012 at 7:39pm

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

#234981

You must be logged in to reply to this topic.