Uses for SceneGraphVisitorAdapter to load different levels

I’m implementing different levels in my game and am trying to implement something to remove the current objects from the current level before loading a new level.



I discovered the SceneGraphVisitor function and am wondering if the code below is the appropriate way to remove a level before adding the next level. I’m worried about leaving unattached nodes/geometries hanging around (ie. memory leaks). Could someone let me know if this would be the right way? I would appreciate any comments.



[java]

// curLevel is a class that has a Node member (retrieved with getScene()) that already has child nodes and geometries attached

// and is attached to rootNode to make the level visible



public void loadLevel(int levelNum) {

if (curLevel != null) { // curLevel not null means a level is currently active



SceneGraphVisitorAdapter v = new SceneGraphVisitorAdapter() {

@Override

public void visit(Node node) {

logger.log(Level.INFO, “Visited curLevel Node: {0}”, node.getName());

for (int i=0; i<node.getNumControls(); i++) {

Control control = node.getControl(i);

node.removeControl(control);

logger.log(Level.INFO, “Visited curLevel Node: {0} had a Control: {1}”,

new Object[]{node.getName(), control.getClass()});

control = null;

}

node.removeFromParent();

}

@Override

public void visit(Geometry geometry) {

logger.log(Level.INFO, “Visited curLevel Geometry: {0}”, geometry.getName());

for (int i=0; i<geometry.getNumControls(); i++) {

Control control = geometry.getControl(i);

geometry.removeControl(control);

if (control instanceof RigidBodyControl) {

physicsSpace.remove(control);

}

control = null;

}

geometry.removeFromParent();

curLevel.getGeometries().remove(geometry);

geometry = null;

logger.log(Level.INFO, “curLevel numGeometries: {0}”, curLevel.getGeometries().size());

}

};

}



curLevel.getScene().breadthFirstTraversal(v);

curLevel = null;



curLevel = levelManager.loadLevel(levelNum);

nodeScene.attachChild(curLevel.getScene()); // nodeScene is already attached to rootNode

}



[/java]

I guess that looks okay. Geometry is mostly visible so you should see leftovers… However you do not detach Nodes, most models do contain Nodes to group the geometry. Maybe you rather want to keep lists of models that you add and remove them by those lists. This would also allow iterating through those lists to e.g. check for attack ranges or something else.

@normen

Thanks for the comments. I previously had a seperate list of geometries and of rigidbodycontrols within the curLevel class and was removing them based on the lists. The nodes and geometries were created from loadModel based on a j3o file created from a blender file.



I didn’t have a list of nodes and was wondering if not removing the nodes would create any issues when I changed levels. Do I not need to worry about removing Nodes from rootNode when I change levels?



Previously, I was doing the following but I was worried that not removing the nodes would cause memory leaks:



[java]

if (curLevel != null) { // curLevel not null means a level is currently active

ArrayList<Geometry> geometries = curLevel.getGeometries();

for (Geometry geometry: geometries) {

for (int i=0; i<geometry.getNumControls(); i++) {

Control control = geometry.getControl(i);

geometry.removeControl(control);

if (control instanceof RigidBodyControl) {

physicsSpace.remove(control);

curLevel.getPhysicsControls().remove((RigidBodyControl)control);

}

control = null;

}

geometry.removeFromParent();

geometry = null;

}

}



curLevel = levelManager.loadLevel(levelNum);

nodeScene.attachChild(curLevel.getScene()); // nodeScene is already attached to rootNode



[/java]

Well the nodes do not take lots of memory… but they are there, yeah. look at the SceneExplorer when you open a model with it, then it will become more apparent.

That’s exactly where I realized that I wasn’t removing the nodes. I’m going to use both methods, I think. I’ll keep the seperate lists so I can compare objects quickly like you said earlier and then use the vistor to remove the level objects. If I remove them from the seperate lists as well as from the scene, I should have everything dealt with to keep the level swapping clean. Thanks for your replies.