Using the LiveAPI js object to iterate through children of a Live object

Apr 21, 2012 at 3:05pm

Using the LiveAPI js object to iterate through children of a Live object

I’m trying to use the LiveAPI js object to iterate through children of a Live object, e.g. all tracks in a live_set, or all clip_slots in a track. I’m having some problems doing this and I’m wondering whether I’m going about it the wrong way. For example, in my code below, api.children returns an array, one of which is called ‘tracks’, but api.children["tracks"] returns .

Does anyone have an example of how to do this correctly? Thanks.

inlets = 1;
outlets = 1;

post("global post");
post();

function bang()
{
	post("bang");
	post();

	var api = new LiveAPI("live_set");

	if (!api)
	{
		post("no live_set");
		post();
		return;
	}

	post("new api: ", api);
	post();

	post("info: ", api.info);
	post();

	post("children: ", api.children);
	post();

	post("children["tracks"]: ", api.children["tracks"]);
	post();

	var track;

	for(track in api.tracks)
	{
		post("track: ", track);
		post();
	}
}
#63127
Apr 21, 2012 at 4:37pm

Think I’m getting there slowly…

for(var i = 0; i < api.getcount("tracks"); i++)
	{
		var track = new LiveAPI("live_set tracks " + i);
		post("track " + i + ": " + track.get("name"));
		post();
	}
#227812
Apr 21, 2012 at 5:39pm

That looks about right. You’ll want to avoid instantiating new instances of the api object inside the loop however:

function test()
{
var api = new LiveAPI(this.patcher, “live_set”);
var trackCount = api.getcount(“tracks”);

for (var index = 0; index < trackCount; index++) {
api.path = “live_set tracks ” + index;
post(api.get(“name”), ‘n’);
}
}

#227813
Apr 21, 2012 at 6:22pm

Thanks willyc – but why should I re-use the same instance of the LiveAPI object. Is it because the object takes up a lot of memory? It’s just that I was planning on having very many LiveAPI objects at once, all observing individual paths simultaneously. It seems to be the only way to achieve another goal of mine, described here: http://cycling74.com/forums/topic.php?id=39445

#227814

You must be logged in to reply to this topic.