Entityclass

Blitz3D Forums/Blitz3D Programming/Entityclass

"just make your own delete entity function that would set the handle to zero"

That doesn't help in a lot of cases because there is no way to get at all the handles to a given entity. For example, imagine an RTS game and you want when you select a unit and have it target another unit, the handle of the targetted unit is stored as a type field with the selected unit. Now if the targetted unit is destroyed (not by the selected unit) how do you work backwards to know all the units that had it targetted? You can't, so you can't have a delete function which sets all handles to that entity to 0. Doh!


"The problem with checking if an entity no longer exists is that the data area for the entity which the handle value points to no longer exists. And if you keep the data area for a deleted entity so that its existance state can be checked then you end up with a huge memory leak."

Why does it matter if a data area for the entity exists? That is necessary to tell you if the entity EVER existed, but not necessary if you don't care about the past and only about the present (ie. does the entity currently exist?) Just look at the data in the memory address passed to the function; if it is not a valid entity return Null or 0.


ADDITION: Oh, and I am doing this right now with a bit of roundabout code involving types. While attempting to access an entity that no longer exists gives you a MAV, attempting to access a type instance that no longer exists just returns Null. Thus, create a type with the handle of an entity as a field. Whenever you create a new entity, first create a new type instance and then set the handle of the new entity to the field of the type instance. Now use Handle() to get the handle of the type instance and use that handle with Object() to access the type instance directly (instead of the entity directly.) Now when you free the entity delete the type instance; any time you refer to that deleted type instance, you'll get Null.

For example of what I mean, you wouldn't pass the handle of an entity to a function, you pass the handle of a type instance. Then in the function check if the type instance exists and if so get the entity handle stored in the type instance. Now do whatever you need to do with the entity.