Traversing patcher hierarchy

Georg Hajdu's icon

Hello,

Is there a way to improve this code? I'm trying to traverse a patcher hierarchy in order to get the value of a named object called "id" without knowing the number of parent patcher levels it may be located in. My solution is very lame and works only up to 6 levels, since I don't know how to apply the this.patcher.parentpatcher...getnamed() function recursively.

Thanks, Georg

function bang()
{
    var id = this.patcher.parentpatcher.getnamed("id");
    if (id == null){
        id = this.patcher.parentpatcher.parentpatcher.getnamed("id");
        if (id == null){
            id = this.patcher.parentpatcher.parentpatcher.parentpatcher.getnamed("id");
            if (id == null){
                id = this.patcher.parentpatcher.parentpatcher.parentpatcher.parentpatcher.getnamed("id");
                    if (id == null){
                    id = this.patcher.parentpatcher.parentpatcher.parentpatcher.parentpatcher.parentpatcher.getnamed("id");
                        if (id == null){
                        id = this.patcher.parentpatcher.parentpatcher.parentpatcher.parentpatcher.parentpatcher.parentpatcher.getnamed("id");
                            if (id == null){
                            id = this.patcher.parentpatcher.parentpatcher.parentpatcher.parentpatcher.parentpatcher.parentpatcher.parentpatcher.getnamed("id");
                            }
                            else outlet(0,id.getvalueof())
                        }
                        else outlet(0,id.getvalueof())
                    }
                    else outlet(0,id.getvalueof())
                }
                else outlet(0,id.getvalueof())
            }
            else outlet(0,id.getvalueof())
        }
        else outlet(0,id.getvalueof())
}

Mathieu Chamagne's icon

here is a js that returns the name of root patcher

function bang() {
    var prev = this;
    var owner = this.patcher.box;
    while (owner) {
        prev = owner;
        owner = owner.patcher.box;
    }
    if (prev) {
        outlet(0, prev.patcher.name);
    }
}

it should easily be adapted to match your needs...

Mathieu

Georg Hajdu's icon

Thanks, I'll look into this. The problem I see (this may be unfounded) is that the object in question may not be on root level.

Georg