Proper way to remove a node

Would it be:



Node.removeFromParent();



or



Node.getParent().getChildren().remove(Node);



or do these have the same effect?



Both seem to work… but I think I may be experiencing some aftifacting that prevents you from using the same Node name again. The node with the old name is successfully added to the scene, but interacting with it seems to not work the same. This is probably my error, but still wanted to ask about properly removing a node. I’ll try renaming the node prior to adding it again and see if this has any effect–will report the outcome (of course) because I am neurotic.



Thanks!

They do the same. The parent information etc is updated for both Spatials in all cases. You can even straight attach a spatial to another node and it will be properly detached from the current.

Thanks :slight_smile:

See my update.

Oh… nice. That will be very useful!

Ok… this is strange. I’ll try and keep the explanation as brief as possible.



ObjectX is serializable. It is created by server and given a unique identifier.

ObjectX is sent to all clients and client greates appropriate geomety,

Geometry is added to a Node that is named the ID of ObjectX

Client adds Node to scene.

Player picks up ObjectX (ObjectX is removed from scene and from server).

Player drops ObjectX (sending serialized object to server for adding).

Server recreates object and notifies all clients that a new object has been added to the zone.

Now…



The client recreates the object (adding geom to a Node named the ID of the object)



If the ID is the same as previous, the item is added, but you can not pick it back up (i.e. Node was named the same as previously removed Node).

If the server generates a new ID for the object before sending the update to the clients, the object is added to the scene and you can manipulate it.



For the life of me, I can not figure out why the Node being named the same as a Node that was removed from the scene graph would have any effect on anything.



Thoughts?

Hm, do you use getChild() somewhere? Its the only thing inside Node using names…

JME does not use the ID for anything. It’s just informational. And there’s no magic in remove. If you remove the node from its parent then it is removed.



So if you are seeing some inconsistent related to IDs then it is entirely somewhere in your code… and therefore impossible for us to debug remotely.



In your last paragraph you say “looks this up by the id of the node selected”… what does that mean? How are you doing that? And timing-wise, how does that play into the receipt of the new object with the same ID. My suspicion is that your problem centers around that somewhere… something is not updating when you expected it to. But we don’t know enough about your code to speculate further.



If it were me debugging my own code, I’d add a bunch of logs or System.out.println()s dumping information to the console to validate that the right objects are where they are supposed to be. And since I think the normal node toString() won’t tell you the difference between two nodes with the same ID, you could add System.identityHashcode(myNode) to the println().



Example:

System.out.println( “New node created:” + myNode + “@” + System.identityHashCode(myNode) );





System.out.println( “Picked node:” + pickedNode + “@” + System.identityHashCode(pickedNode) );



and so on.



My guess… the node you look up by ID from picking or whatever is the old node that is no longer in the scene graph. But I can only guess. And I don’t know even how you are looking it up.

After going through the code, I wasn’t able to find a getChild() call. I also checked to see if perhaps I was naming the Geomtry the same as the Node… notta on that one as well. It’s not a show stopper by any stretch (updating the id before propagation is easy enough), but I am sure I must being handling something wrong, which means that I could potentially be eating away at memory the more NPCs or items that are spawned and removed.



Is there a way to dump a fully traversed list of all items in a scene graph? Easy to write if not, but if it exists already, all the better. I can at least find out I am leaving bits and pieces lying about.

The subtle point is that Node, Geometry, etc. don’t use their names for anything. You can give every node the same name and it won’t matter to JME.



…so if you aren’t trying to look up nodes/geometries by their name then the problem must be elsewhere in your code… and/or have nothing to do with node names.

pspeed said:
...so if you aren't trying to look up nodes/geometries by their name then the problem must be elsewhere in your code... and/or have nothing to do with node names.


Not following... sorry. I am using names to id Nodes/Geometry etc. Are you saying this is a problem? or not? I'm trying to determine if I am removing them properly... not whether JME has an issue. And if I am doing this properly, why adding a node with the same name as a node that was previously removed is causing this problem, but if I give it a new unique name... the problem goes away.

Quick structure breakdown:

Node (named Id) - contains:
Geometry 1
Geometry 2
Geometry 3
Etc.

If I use Node.removeFromParent(); This destroys the Node and all contained Geometries/Nodes/etc... correct? Visually, all geometries seem to be removed. I check against the Node Id... and it returns null. So, I am just confused as can be on this.

I am sure I am grabbing the node that is named the id, because the code handling retrieving the item, looks this up by the the id (name) of the node selected, and this always (in all other circumstances aside from the above stated), returns the proper serialized object that represents the spacial.