question

Patrick Cloutier avatar image
0 Likes"
Patrick Cloutier asked Matthew Gillespie edited

Addressing all members of a group

Not sure if this is a bug or normal.

When I want to address all members of a group. This statement works:

for (int i = 1; i <= Group("Stations").length; i++) 
{
	Group("Stations")[i].output.close();
}

But when I instead use the sampler to get the group name from the tool box. I end up with this statement which doesn't work. It doesn't give any error but doesn't do anything either.

for (int i = 1; i <= Model.find("Tools/Groups/Stations").subnodes.length; i++) 
{
	Model.find("Tools/Groups/Stations").subnodes[i].output.close;
}

Should the 2nd method work or not?

Thanks,

FlexSim 19.0.2
groupsapi
· 1
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Matthew Gillespie avatar image Matthew Gillespie ♦♦ commented ·

If there are other groups as members of the first group you should use the toFlatArray() method to give you a single list of all the objects throughout the hierarchy of the group.

Array members = Group("Stations").toFlatArray();
for (int i = 1; i <= members.length; i++) {
	members[i].as(Object).color = Color.random();
}
2 Likes 2 ·

1 Answer

Matthew Gillespie avatar image
3 Likes"
Matthew Gillespie answered

No, the 2nd method doesn't work and it's not supposed to because you're not doing what you think you're doing.

Objects in a group aren't stored as subnodes of the group, those objects are stored in the model. The subnodes of a group are pointers to the objects in the model.

You'd have to modify the 2nd method like this for it to work

treenode stations = Model.find("Tools/Groups/Stations");
for (int i = 1; i <= stations.subnodes.length; i++) {
	stations.subnodes[i].find("+/~").as(Object).output.close();
}

Which is exactly why we provide a Group API (the 1st method) for you to use so that you don't have to write such nasty code.


groups.png (10.9 KiB)
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.