EntityExists()?

Blitz3D Forums/Blitz3D Programming/EntityExists()?

A couple of solutions spring to mind (well, OK, I had to think about it for a bit :) ...

1) Use multiple indirection: When Blitz creates an entity, create an internal pointer to that entity and return the address of this pointer as the entity's handle (i.e. the handle will be a-pointer-to-a-pointer-to-an-entity). Then, when an entity is freed, set the internal pointer to NULL.

An EntityExists(handle) function would then only have to check to see if the handle points to a NULL pointer in order to see if the entity exists or not.

This mechanism would work even if your prog had multiple handles to the same entity.

Downsides: the internal pointer (4 bytes) would have to remain in mem for the duration of the prog. Accessing entities through their handles would have a slight overhead due to the multiple indirection. Then again, I think Blitz already does this: I'm pretty sure Blitz entity handles aren't direct pointers to the actual entity's data. I could be wrong.


2) Store unique entity IDs in a binary tree (or some-such suitable structure): So, each time an entity is created, we get a pointer to it which, when converted to an integer, represents a unique entity ID. Next, add this ID to the tree and in it's node have the pointer to the entity. Again, return the address of this pointer as the entity handle.

An EntityExists(handle) function would then have to dereference the handle (to get the pointer to the actual entity), convert it to an ID integer, and then search the tree for it. If it's in the tree then the entity exists, otherwise it doesn't. This should be quicker than it sounds!

When an entity is freed, it's tree node can be deleted which will also mean the 4 bytes used as the entity's internal pointer is freed also, unlike the first method, above.

Hope some of that made sense! :)

[EDIT] I guess I should have made it clear that the solutions I offer above are not for implementation in Blitz code, but rather how I figure it would be possible for Mark to implement a native EntityExists() function. Not that I know jack about the internal workings of B3D, of course. :P