i guess you are calling local getChildComponentOfType<ComponentClass>() inside your local template itself over and over, no ? it wont go recusively here, unless your ColponentClass is valid pointer, then you can invoke your recursion with it.
or im tripping ?
I’m not super-keen on that. Unlike findParentComponentOfClass, it’s the kind of thing where you’d add it and then people would start wanting more control, e.g. an option to search backwards, or to return an array of all the matches, etc. And it works perfectly well as a free function like this one.
I found this recursive child-finder method quite helpful, so my thanks to @adamski and @jules
I was trying to understand the nested child structure of a PropertyPanel, and wanted a clean printout of the structure, so inspired by the getChildComponentOfClass method here, I came up with this recursive printout method. It prints the level of recursion, indenting appropriately, and then prints the typeof the Component. If the Component name and/or ID are set, it includes them in the printout as well.
static void printAllChildren (Component* parent, int level = 1)
if (level == 1)
DBG ("Recursive Printout of All Child Components");
for (int i = 0; i < parent->getNumChildComponents(); ++i)
auto* childComp = parent->getChildComponent (i);
for (int j = 0; j < level - 1; ++j)
text += " "; // add indents
text += "Level " + String (level) + " ";
String type = typeid(*childComp).name();
text += "(" + type + ") ";
text += "name = " + childComp->getName();
if (childComp->getName().isNotEmpty() && childComp->getComponentID().isNotEmpty())
text += ", ";
text += "id = " + childComp->getComponentID();
printAllChildren (childComp, level + 1);
Not of a given class but by ComponentID, this function searches recursively among all children and through the entire genealogy:
// Generic function to get the pointer to a child component by its ComponentID
Component* getChildComponentByID(const String& ComponentID, Component* childToSearchFor = nullptr, int level = 0)
if (childToSearchFor == nullptr)
childToSearchFor = this;
for (auto cmp : childToSearchFor->getChildren())
if (cmp->getComponentID() == ComponentID)
int next = level + 1;
if (childToSearchFor->getChildren().size() > level)
return getChildComponentByID(ComponentID, childToSearchFor->getChildren()[level], next);
Could be easily modified to search by component type.
I’m confused as to what this code does. The getChildren() function returns an array, not a single Component*, so how can you call getComponentID() on the return value of that function? Should that have been getChildren[level]?
Returns an implementation defined null-terminated character string containing the name of the type. No guarantees are given; in particular, the returned string can be identical for several types and change between invocations of the same program.
The string you get back can be pretty much useless depending on your compiler.
If you are using clang, not on Windows, you can do
auto demangled = abi::__cxa_demangle (typeid (* childComp).name(), nullptr, nullptr, &status))