That's bass ackwards. If you can't figure out which handles point to an entity when freeing it, how can you figure out which entity the handle points to for checking if it exists? That is, how can you tell which myentity variable to check?
Imagine a situation like your average simulation game (RTS, whatever.) A potentially limitless number of entities can be created at any time, and any of those enitities can be destroyed at any time. Now imagine coding some AI where multiple entities decide to attack one other entity. When that other entity is destroyed, it is non-trivial to convey that fact back to the attacking entities (so that they stop checking the position of the target, for example) in the open ended situation of an RTS game.
The little trick you described would work well if you had a strictly limited number of entities, all created from the beginning of the game, but then the trick would be useless in that situation.
ADDITION: Wait, re-reading your post I don't think you understand the problem. If there is only one handle to an entity that's easy to deal with. Then it is pretty obvious what handle points to that entity since there is only one handle. Your trick is needlessly obfuscating the code; just make the next line after freeing the entity myentity=0 and don't bother with a special entity free function. The problem is if multiple handles exist to a single entity. This situation is especially problematic if the handles are buried in the fields of multiple type objects. In that situation it is really difficult (impossible basically) to know all the handles which point to a given entity and set them all to 0.
@Ken and edzup: Could you post some code demonstrating what you are talking about? That sounds pretty useful but I'm not exactly understanding what you described.
Wait, I think I get it. Instead of making additional handles pointing to an entity, you make handles to a type object which has, as one of its fields, the handle to the entity. Then when you need to access that entity you do so by accessing the type object first, and then the field of the type object. Thus only one direct handle to the entity exists. Hm, this would work very well actually. Do you understand the trick they suggested, mrtricks? Because this should solve the problem.
Imagine a situation like your average simulation game (RTS, whatever.) A potentially limitless number of entities can be created at any time, and any of those enitities can be destroyed at any time. Now imagine coding some AI where multiple entities decide to attack one other entity. When that other entity is destroyed, it is non-trivial to convey that fact back to the attacking entities (so that they stop checking the position of the target, for example) in the open ended situation of an RTS game.
The little trick you described would work well if you had a strictly limited number of entities, all created from the beginning of the game, but then the trick would be useless in that situation.
ADDITION: Wait, re-reading your post I don't think you understand the problem. If there is only one handle to an entity that's easy to deal with. Then it is pretty obvious what handle points to that entity since there is only one handle. Your trick is needlessly obfuscating the code; just make the next line after freeing the entity myentity=0 and don't bother with a special entity free function. The problem is if multiple handles exist to a single entity. This situation is especially problematic if the handles are buried in the fields of multiple type objects. In that situation it is really difficult (impossible basically) to know all the handles which point to a given entity and set them all to 0.
@Ken and edzup: Could you post some code demonstrating what you are talking about? That sounds pretty useful but I'm not exactly understanding what you described.
Wait, I think I get it. Instead of making additional handles pointing to an entity, you make handles to a type object which has, as one of its fields, the handle to the entity. Then when you need to access that entity you do so by accessing the type object first, and then the field of the type object. Thus only one direct handle to the entity exists. Hm, this would work very well actually. Do you understand the trick they suggested, mrtricks? Because this should solve the problem.