Component children look up – #6 by TURKY_M7MD – C++


Yeah that’s what I was thinking about, but there is another problem which is how to look for nodes that are inside nodes that are inside nodes and so on… something like a file search

I found a way on how to do it after a lot of thinking, which is

// WARNING: Returns nullptr if nothing is found, please check if return pointer is nullptr to avoid problems
// the 'list' argument should be left nullptr, it's only used for the recursive search
inline std::vector<Node*>* findComponentsByName(Node* parent, std::string name, bool containParents = true, std::vector<Node*>* list = nullptr)
        {
            std::vector<Node*>* nodes = list == nullptr ? new std::vector<Node*>() : list;

            if (parent->getChildrenCount() == 0)
                return nullptr;

            bool hasFoundSomething = false;

            for (auto i : parent->getChildren())
            {
                if (i->getChildrenCount() > 0 && containParents)
                {
                    if (i->getComponent(name) != nullptr)
                    {
                        nodes->push_back(i->getComponent(name)->getOwner());
                        hasFoundSomething = true;
                    }
                }
                else if (i->getChildrenCount() == 0 && !containParents)
                {
                    if (i->getComponent(name) != nullptr)
                    {
                        nodes->push_back(i->getComponent(name)->getOwner());
                        hasFoundSomething = true;
                    }
                }

                if (i->getChildrenCount() > 0)
                    findComponentsByName(i, name, containParents, nodes);
            }

            if (list == nullptr)
            {
                if (hasFoundSomething)
                    return nodes;
            }

            return nullptr;
        }

It’s kinda bad and unoptimized but it does the job for me! Thanks for the help <3



Source link

More To Explore

Share on facebook
Share on twitter
Share on linkedin
Share on email