I have a collision between two objects. When those two objects collide I want to delete the from object. I am using CollisionHanlderEvent and can’t figure out how to get the collision function to call the delete function of the object’s class. I can get the nodepath and collisionsphere but those don’t allow me to access the functions of the class that the object is from. Does anyone know of a way to do this?
Yes, this is a commonly asked question. One way to achieve it is by setting the class as python tag:
object = MyObjectClass() object.nodePath.setPythonTag("obj", object)
Later, when you have just a NodePath, you can retrieve the class instance by doing:
object = collidedNodePath.getNetPythonTag("obj")
Sometimes i feel like i am always doing things the hard way.
Disregard this stuffs
I havent found a clever (built in) way of doing this yet; but i use the nodepath/tag info you can set up in panda (info can be grabbed off of the CollisionEntry)
Simple way would be to set a tag on all of your objects that you would need access to(from the collision event or entry), perhaps a unique integer or string or whatever. I have a class that keeps a dictionary whos keys are these unique identifiers, and the corresponding value is a reference to that class.
#in setup self.myId = someUniqueInteger self.nodepath = loader.loadModel("blah.egg") self.nodepath.setTag("collisionId", self.myId) collisionDictionary[self.myId] = self #where self here is the class that has this node object ##later in collision function fromNode = collisionEntry.getFromNodePath() id =fromNode.getNetTag("collisionId") fromClass = collisionDictionary[id] fromClass.yourDeleteFunction()]
Yeah, that works great pro-rsoft. I hadn’t even considered that you could pass that much information with the tag system. I was already passing tags for handling stuff but I didn’t know you could pass the whole object as a tag. Don’t feel bad rwhughst I liked your way of doing it better, sadly it is more code intensive and would require me to handle a bunch of diffferent exceptions. The fact that I am notoriously apt to use the simplest(and least work intesive) method strikes again.
Hah im going to go change my code to use the python tag now ha