How works getChildren?

i use the following method do attach or detach some boxes at my node:



public void updateBoxesDrawing(CP_Controller controller) {
      if (drawBoxes == true) {
         for (int i = 0; i < childList.size(); i++) {
            boxNode.attachChild(childList.get(i));
         }
      boxNode.updateRenderState();
      controller.updateRootNode();
      }
      else if (drawBoxes == false) {
         childList = boxNode.getChildren();
         boxNode.detachAllChildren();
         boxNode.updateRenderState();
         controller.updateRootNode();
      }
   }



childlist is a private ArrayList which is created as class variable. if i first call the method with false then it works. but if i call the method after that with true then childList is always empty. why that?

Well Node.getChildren() returns a reference to the Nodes list of children.

After Node.detachAllChildren() the list will of course be emtpy.

Maybe you want to actually copy the content of the children list before detaching.



Or do you mean something different?

yes but i think thats already what im doing. first i call



childList = boxNode.getChildern;



then in the childList array it is safed.



then i detachAllChildern, but the spatial should be saved in the childList or is there only a reference?

ok i tried to solve the problem this way:



public void updateBoxesDrawing(CP_Controller controller) {
      if (drawBoxes == true) {
         for (int i = 0; i < getChilds().getQuantity(); i++) {
            boxNode.attachChild(getChilds().getChild(i));
         }
         controller.updateRootNode();
      }
      
      else if (drawBoxes == false) {
         storeChilds();
         boxNode.detachAllChildren();
         controller.updateRootNode();
      }
   }
   
   public void storeChilds() {
      childList = boxNode.getChildren();
      for (int i = 0; i < childList.size(); i++) {
         bufferNode.attachChild(childList.get(i));
      }
   }
   
   public Node getChilds() {
      return bufferNode;
   }



i works but something strange happens. only every second node is contained in the bufferNode. why that?

I had the same problem getting only half of the objects with “getChildren()” in here http://www.jmonkeyengine.com/jmeforum/index.php?topic=9377



So in your case the bold one is the faulty part:

public void storeChilds() {

childList = boxNode.getChildren();

for (int i = 0; i < childList.size(); i++) {

bufferNode.attachChild(childList.get(i));

}

}



You must do something like that to get all children:

ArrayList<Spatial> childList = new ArrayList<Spatial>();
childList.addAll(boxNode.getChildren());

for(Spatial s:childList)
     bufferNode.attachChild(s);

SCM said:


childList = boxNode.getChildern;
then in the childList array it is safed.
then i detachAllChildern, but the spatial should be saved in the childList or is there only a reference?


nothing is saved or copied here,
childList is still a reference to the childList in the node.

ah ok. thx for the hints now i works.



but i have a second question. how can i really destroy an object like a box? if i just detach it from the node its not drawn but it is still there, how do i really delete a object? (is it clearBatches?)

thats the job of the java garbage collector.

if an object lies around without a reference to anything it will get destroyed.

so, if you detach a Box from the scene, chances are good that now the Box will get destroyed.



If this Box is still referenced by a class member or inside a ArrayList it will not get deleted of course. (unless its a weak reference  :))

owaye said:

I had the same problem getting only half of the objects with "getChildren()" in here http://www.jmonkeyengine.com/jmeforum/index.php?topic=9377
[...]
You must do something like that to get *all* children:

ArrayList<Spatial> childList = new ArrayList<Spatial>();
childList.addAll(boxNode.getChildren());



I just couldn't leave that uncommented because it may lead others to false assumptions.
owaye, you profoundly misunderstood what your problem was in the thread you linked.
I won't explain this any further here for it's off topic for this thread, so if you want to know more, please use the other thread to ask.
The important thing to note is:
Node.getChildren() returns a REFERENCE to the node's children list, which contains ALL of the Node's children.

ok that i understand now. but why is there only exactly the half of the childern in the list??

SCM said:

why is there only exactly the half of the childern in the list??


I would like to know that too

Answered over in the other thread:

http://www.jmonkeyengine.com/jmeforum/index.php?topic=9377.msg73859#msg73859