check for existence of entity

Blitz3D Forums/Blitz3D Programming/check for existence of entity

jhocking(Posted 2004) [#1]
Is there any way to check for the existence of an entity before executing a command? Specifically, what's happening is that I am storing handles for entities in type fields. I am getting an intermittent error where occasionally my code attempts to use the Animate command with a stored handle BUT the entity which that handle referred to was already deleted, resulting in a Memory Access Error. I've tried and tried to figure out why my code is doing this, but because the error happens so rarely I can't track it down. In lieu of that, I would like to put some sort of check in my code so that before attempting to call Animate it will check if there actually is an entity at the stored handle. In other words, something like

If GetEntity(handle)<>0 Animate handle


jfk EO-11110(Posted 2004) [#2]
right after deleting something you should set its variable to zero, so you can check first if it's not zero. I you use multiple handles then you might use the new commands which tell if an entity is a mesh, pivot or whatever (nothing if not present afaik)


jhocking(Posted 2004) [#3]
And what commands would those be? I am using multiple handles, one which is definitely getting eliminated (since I delete the type instance on the next line after freeing the entity) but also in another place which I thought was getting zeroed but apparently isn't. More specifically, the other handle is getting zeroed most of the time but apparently every now and then something goes wrong; as I said, this bug is very intermittent. In order to even get the error showing up frequently enough for me to deal with it (ie. sooner than an hour in) I had to accelerate the execution of the code.

Actually, I'm also having another error in my code which may or may not be related to this. It's certainly happening in the same function. I'll start another thread for the other problem.


Tom(Posted 2004) [#4]
Hi Joe,

I had the same oversight, the FreeEntity command doesn't clear the old pointer to the entity,

Example:

Graphics3D 800,600

sphere=CreateSphere()
Print sphere

FreeEntity sphere
Print sphere ; points to non-existant entity


Just make sure whenever you use FreeEntity, the next line is theentity = 0

Cya
Tom


jhocking(Posted 2004) [#5]
Well thanks for the advice but I'm already doing that for the primary handle. Specifically, I'm deleting the type instance. However, the handle is also being stored in other places, places I can't access directly. All right, here's the long story of what I'm doing:

I have a community of insects. Each insect has a type instance associated with it. That type instance stores the handle to that insect; simple enough. But the type instance is ALSO storing the handle for that insect's mate IF the insect is attempting to mate. There's the sticky part: there's no way for me to know what other type instances are storing a handle to a given entity when I delete that entity. I'm already using Object() and Handle() to work backwards from the mate of the insect being deleted but apparently that isn't good enough because every now and then I'm getting an error anyway. Basically, there should only ever be one other object storing the entity's handle (since the insect's mate one at a time) but apparently that isn't happening and that's resulting in the intermittent error.

Here's the code:
www.3darteest.com/HoG_04.bb
The error happens in the AutoMate function. It won't run as is because a) you're missing the models and textures and b) I'm using sswift's shadow system. I'll try to isolate the problem to something simpler and which will run as-is.

And as if this wasn't frustrating enough, there is ANOTHER error happening in the same function (or at least the debugger is stopping on a line within the same function when the other error happens.) I don't want to ask for help on that other problem however until I've figured this one out. I have reason to believe solving one will clear up both.


sswift(Posted 2004) [#6]
"There's the sticky part: there's no way for me to know what other type instances are storing a handle to a given entity when I delete that entity."

Well, there's one thing you can do, but I hesitate to mention it because I consider it shoddy programming and when you go back to look at the code later you're sure to forget that this is happening, leading to confusion.

But here goes.

Let's say you have a type, A. And you have a type B.

If type B contains a pointer to an instance of type A, then if you delete type A, the pointer in type B to type A will automatically be set to null.

In other words, if you make a type Ant, and for each ant go ThisAnt.Ant = New Ant, and then you make another type which contains pointers to specific ants, when you Delete ThisAnt, then any pointer in any other type that points to ThisAnt will be set to null.

Not exactly what you were asking for, but it might be of help.


"Basically, there should only ever be one other object storing the entity's handle (since the insect's mate one at a time) but apparently that isn't happening and that's resulting in the intermittent error."

Well then that's a bug that you need to fix, not find a workaround for.


sswift(Posted 2004) [#7]
In fact, now that I think of it, it might be that very behavior by Blitz which is causing your bug.

Let's say you delete the ant's type. Well, if you then go through and try to find other instances of that ant's type pointer in other types, you don't find them, because that pointer will have been set to null.

Now, let's say the other type looks liek this:

Type Bug
Field AntPointer.Ant
Field X#, Y#, Z#
end type

Well since you deleted the ant itself you now have another type lying around that you're gonan iterate through, and when you do this:

PositionEntity Bug\AntPointer\AntEntity, Bug\X#, Bug\Y#, Bug\Z#

Then you'll get an entity does not exist error or an illegal memeory access error because you deleted Ant, and Bug\AntPointer now points to Null.


Maybe that's your issue, I dunno.


sswift(Posted 2004) [#8]
Oh and one last thing. There is NO way to determine if an entity exists, nor will there ever be. It's completely up to you to make sure you don't delete an entity and fail to update all pointers to it.

This is unfortunate, but Mark has said that's the way it's gotta be. I had a problem with this myself in my particle system and shadow systems because I didn't want to have to make the user tell the system when they'd deleted an entity. But it needed to know if they had or it would crash.

Of course the "proper" way to handle that would then be to override the FreeEntity command with my own FreeEntity command that frees the entity and removes it from my shaodwe system as well, but that is not possible in Blitz.


jhocking(Posted 2004) [#9]
True, and in my original post I mention that I've been trying. For DAYS. And I may have finally figured it out; I've been running the latest build for over an hour straight now with no error (whereas before I'd get the error on average within 10 minutes.) Still, since I cannot be 100% it's fixed (it was very infrequent in the frist place after all,) I'd really like to have error checking as a failsafe.

As for your suggestion, ooh I really don't want to do something that dangerous. But thanks for the tip anyway; that may come in useful some day.

EDIT: Whoah, cross-post. I only read your first post before responding.


jfk EO-11110(Posted 2004) [#10]
my mistake - I thought c$=entityclass$(entity) would return an empty string when the entity does not exist. but in fact it produces an "Entity does not exist" Runtime Error.


Ross C(Posted 2004) [#11]
Joe, have you considered using arrays? Where you can store the index value of the entity that is the mate.

dim insect(insect_no,attributes)

;attributes 0=entity
;           1=entitys mate
;           2=x
;           3=y
;           4=z


Could make finding things easier :) Code gets slightly unreadable, but i decided i would use this for my up and coming project. going quite well :)