Jump to content

recursion in hierarchy


photo

Recommended Posts

class Node {
string name;
Node child[];

Node(string aName){name=aName;}	
};


void saveNode(Node node) {
log.message("save node [%s] children count: [%i]\n",node.name,node.child.size());
foreach(Node n; node.child) 
	saveNode(n);
}

int init() {
Node root = new Node("zero");
Node one = new Node("one");
Node two = new Node("two");
root.child.append(1,one);
one.child.append(2,two);
root.child.append(3,new Node("three"));

saveNode(root);

return 1;
}

This code crashes application without any error message.

Log:

13:01:15 Loading "worlds/vars.world" 186ms
13:01:15 save node [zero] children count: [2]
13:01:15 save node [one] children count: [1]
13:01:15 save node [two] children count: [0]

The same behavior with vector.

Link to comment

Thank you. Recursion bug is fixed.

 

As temporary solution you can use vector array with for/forloop cycle instead of foreach.

 

save node [zero] children count: [2]
save node [one] children count: [1]
save node [two] children count: [0]
save node [three] children count: [0]

Link to comment
  • 1 month later...
×
×
  • Create New...