I’m working on a game project and when I call detachNode() when a missile/bullet collides with an object it does indeed detach the node as I am able to pass through the object the bullet hit but the object does not actually disappear from the scene which it is supposed to. The particle effect also occurs afterwards
Here is a code snippet of where the error is coming from:
Well, the immediate thought that comes to mind is that perhaps the node identified by “hitID” isn’t the one that you’re expecting it to be.
This could be for a few reasons. For example, if you have multiple nodes that contain the the same ID in their names, then whichever is found first will be removed–whether or not that’s the one that you actually want to remove. Or perhaps there’s a bug in the logic that extracts the “hitID”.
Ultimately, I’d argue that a search of the scene-graph is likely a somewhat fragile means of identifying the node to be removed: it’s too easy for it to not work as intended.
Instead, if I may, I’d suggest looking for a way to more explicitly connect the node to be detached with its associated collider, such that, when a collider hits something that should result in a node being detached, you can directly access that node.
There are a few ways of doing this, I daresay. For two:
You can assign to the collider a “Python-tag” that contains the node, allowing you to access the node from the collider
This is done via the “setPythonTag” and “getPythonTag” methods, with cleanup being done via the “clearPythonTag” method.
For an example of this approach, let me reference my “beginner’s tutorial”, specifically this lesson (search for “setPythonTag” and go from there), and this lesson
Note that I’m using a class-based approach there. If you’re not, adjust as called for in your code
Just make sure that you clean up the Python-tag when you’re done with it! If you don’t, you may find that the objects aren’t cleaned up properly afterwards, due to forming a cyclic dependency (i.e. node1 holds a reference to node2, and node2 holds a reference to node1).
You can store a dictionary that uses colliders as keys, and their associated nodes as values