joshua.cook Posted January 9, 2015 Share Posted January 9, 2015 So, I have a recursive class function with a map, a vector, and a problem Say the function looks like void foo(MyObject bar) { *do stuff with bar* MyObject childObjectMap[]; bar.getChildren(childObjectMap); // clears childObjectMap then copies all of the child objects into it foreach(MyObject obj; childObjectMap) { foo(obj); } } The expected result is that this will traverse each MyObject, do things with it, then get all it's children objects and repeat recursively. What actually happens is that it only does this with the first child. So, even if each MyObject has five children this will only recurse on the first child of the first child. What I found is happening is that each time the ID of ChildObjectMap remains the same so, in essense, it is static. Everytime I call MyObject::getChildren I'm clobbering it so on the return it has the data from the child. So, the question becomes, how do I create a new map or vector to keep a function from treating a map or vector like a static? Link to comment
ulf.schroeter Posted January 9, 2015 Share Posted January 9, 2015 Maybe same cause as in https://developer.unigine.com/forum/topic/1435-solved-nested-foreachkey-bug/ Link to comment
joshua.cook Posted January 9, 2015 Author Share Posted January 9, 2015 No, that is a different issue. What is happening in my problem is the map/vector acts like a static variable. If you do the recursion and print the ID of the map/vector at the start of the function the ID never changes. EDIT :Rereading the link, applying a bit of logic, and really thinking about it I would bet my problem and the linked issue may be two ways to produce the same bug. When I changed from a foreach to a plain for loop the silly thing worked as intended. Thank you for the help. Link to comment
joshua.cook Posted January 16, 2015 Author Share Posted January 16, 2015 I take it back; this isn't resolved.so, I have a class foo with a class function bar as above.void bar(MyObject obj) { *do cool stuff* MyObject objVec[0]; obj.getChildren(objVec); // clears objVec and copies any MyObjects obj considers children into it. for(int index = 0; index < objVec.size(); index++ { MyObject object = objVec[index]; bar(object); } } If I put in a log.warning statement to print off the id of objVec then it is the same ID no matter how deep I itterate into the recursion. Is there a way to force the script to create a new vector or map? NOTE : This does not happen if I call the same function on different objects. Only if I recurse on the same object. Link to comment
unclebob Posted January 19, 2015 Share Posted January 19, 2015 Hi there, Joshua! That's because local variables in UnigineScript aren't actually local. They're global with local function scope. Consider keeping arrays inside your instances and expose two methods like: int MyObject::getNumChildren(); MyObject MyObject::getChild(int index); That way, you'll be able to iterate recursively though children without any side effects. If you really really want to stick with the arrays then I guess it's better to have a stack of arrays (consider using Unigine::Vector for keeping all the arrays). Link to comment
Recommended Posts