c++ - Ogre3d: iterating through childnode error -
i have city node houses many building nodes, each of these wish grant new child-node. tells house role , sign have/role. can later used other functions. same .mesh (will later make sign) identify house what. shall randomly assigned. if try run following error. new ogre , adds weird code
int citymanager::assignbuildingrole(ogre::scenenode * _citynode, int _numberofbuildings) { std::stringstream nodename("buildingrolenode"); ogre::scenenode::childnodeiterator cnode = _citynode->getchilditerator(); std::vector <ogre::scenenode*> detachable; while (cnode.hasmoreelements()) { detachable.push_back((ogre::scenenode *)cnode.getnext()); } (int = 0; < detachable.size(); i++) { nodename << childiteration << "_" << parentiteration << "_" << i; switch (rand() % 5) // assign building random proffessions giving them rolenode { case 0: _rolenode = ( ogre::scenenode *)cnode.getnext()->createchild(nodename.str()); _signentity = manager->createentity("barrel.mesh"); _rolenode->attachobject(_signentity); break; case 1: _rolenode = (ogre::scenenode *)cnode.getnext()->createchild(nodename.str()); _signentity = manager->createentity("barrel.mesh"); _rolenode->attachobject(_signentity); break; case 2: _rolenode = (ogre::scenenode *)cnode.getnext()->createchild(nodename.str()); _signentity = manager->createentity("barrel.mesh"); _rolenode->attachobject(_signentity); break; case 3: _rolenode = (ogre::scenenode *)cnode.getnext()->createchild(nodename.str()); _signentity = manager->createentity("barrel.mesh"); _rolenode->attachobject(_signentity); break; case 4: _rolenode = (ogre::scenenode *)cnode.getnext()->createchild(nodename.str()); _signentity = manager->createentity("barrel.mesh"); _rolenode->attachobject(_signentity); break; default: break; } } return role; }
calling getnext on childnodeiterator not retrieve next element advance next element, too. see description in documentation
so iterating on children of _citynode , store every child in detachable vector.
while (cnode.hasmoreelements()) { detachable.push_back((ogre::scenenode *)cnode.getnext()); } after these lines cnode points end of children list (specifically after last element, end() in std::map underlying data type)
if trying iterate further you'll error.
you can either use detachable next iteration (i guess that's why created it)
_rolenode = detachable[i]->createchild(nodename.str()); or child creation in while loop , don't use detachable @ all.
while (cnode.hasmoreelements()) { ogre::scenenode* curr = (ogre::scenenode *)cnode.getnext(); switch (rand() % 5) { case 0: _rolenode = curr->createchild(nodename.str()); _signentity = manager->createentity("barrel.mesh"); _rolenode->attachobject(_signentity); break; /* ... */ default: break; } } 
Comments
Post a Comment