Entityclass

Blitz3D Forums/Blitz3D Programming/Entityclass

Damien Sturdy(Posted 2004) [#1]
Does anyone else agree entityclass should return "None" or something if you entityclass a nonexistent entity?

soo many times have i freed something leaving a variable with a non zero value, which causes havoc with my generic functions.
if entityclass(player)="NONE" then player=createplayer. etc etc etc...


argh

Or is there already a way to do this?


_PJ_(Posted 2004) [#2]
Entityclass???


Matty(Posted 2004) [#3]
Whilst I do not know what "entityclass" is - can't you just set the value of the variable to zero immediately after freeing the entity.


TomToad(Posted 2004) [#4]
Funny, Blitz3D IDE recognizes the command EntityClass() (it's blue and the first letters automatically capitalized), but I cannot find any info on the command in the help files. I wonder if there are other Blitz3D commands I don't know about :-)

ETA: found this on the Blitz forum http://www.blitzbasic.com/b3ddocs/command.php?name=EntityClass&ref=3d_cat#comments

ETA: can't remember how to put in a link :/ Well, just cut and paste :)

ETA once more: Oh, I just type in the link without any tags.


Damien Sturdy(Posted 2004) [#5]
Usually yes, but in a large project with loads of freeentities and loads of children meshes, its a problem

Alot of children meshes get deleted (or at least, seem to) when i freeentity the parent. For future projects its possible to write a routine to do it, but if Entityclass returns "none", it once again saves many people alot of time...


EntityClass(entity): Returns the class of an entity, (if its a light, mesh, sound, etc etc etc)


Odds On(Posted 2004) [#6]
You could just use:

If player = 0
EndIf


Damien Sturdy(Posted 2004) [#7]
Yes, which is what im currently doing, but thats the point, player=0 does not always reflect an entity not existing, because when blitz frees an entity, it doesnt set the variable to 0 after. Entityclass()="None" would save much checking time in the future.

I do suppose however, that the "MAV" error is only caused with bad entities at the moment because it tries to access memory that does not belong to Blitz (to read the entities info). if this is the case then i think itd also be very hard to implement!!!


i still think itd be a brilliant addition.

Maybe this is just a personal thing then? heh...


At current, ive found a couple of my problems being down to storing Entities in Floats (which causes random errors in blitz. Interesting....)


_PJ_(Posted 2004) [#8]

Alot of children meshes get deleted (or at least, seem to) when i freeentity the parent.



Hmm dunno if this is a good thing or a bad thing.

What does EntityClass() return for null entities currently? '0' or "" or an error?

--------------------

I'd recommend what Falcon suggested, but changed slightly, maybe

If player>0
EndIf


Damien Sturdy(Posted 2004) [#9]
An error, which is the only reason i have a problem

The children being deleted is in my current project is a bad thing because i havent kept track of all the children... (entirely my fault)


Usually, children being deleted are a good thing, otherwise there would prolly be memory and entity leaks when deleting AnimMeshes.

Argh.. my head ¬.¬

Well, this is down to my fault, but you have to admit, itd be useful


_PJ_(Posted 2004) [#10]
Yeah I understand ya fully now...

I guess you just have to add some more lines in to track your entities better, and probably add a bit ensuring the handle is reset on freeing them too. I cant think around it any other way right now.


Rook Zimbabwe(Posted 2004) [#11]
How did you find EntityClass()???


Damien Sturdy(Posted 2004) [#12]
hmm, Indeed, i have started writing...

Rook, i dont know actually... i saw it in use somewhere and wrote a demo of it to see what it did. i think i saw it in a particle system...!

And i also have changed my blitz documents to link to the help files here (as an option), so i would have found this in the end:

(same as link from above: http://www.blitzbasic.com/b3ddocs/command.php?name=EntityClass&ref=3d_cat )

Does nobody recognise it then????


Bot Builder(Posted 2004) [#13]
Rook - versions.txt in your blitz directory tells about new commands not added to the help yet.

Also, there are quite a few undocumented commands, most not very useful - http://translate.google.com/translate?u=http%3A%2F%2Fwww.blitzbase.de&langpair=de%7Cen&hl=en&ie=UTF-8&ie=UTF-8&oe=UTF-8&prev=%2Flanguage_tools

I think things with blue stars are undocumented. Notable are Object and HAndle. Handle gives you an integer referring to the type, and object allows you to retrieve the type.

Entity class returning none or "" when there is no entity would mostly be useful for writing libraries. Sure, if you're writing a game it shouldn't be to hard to keep track of your entities, but it'd be nice to allow the users of your library to screw up and instead of crashing inform them in the debuglog.


N(Posted 2004) [#14]
EntityClass returning integer enumerators would be a lot nicer, in my opinion. String comparisons are sometimes a bit off for me for some reason (I wrote a string comparison function that compares the ASCII-to-byte characters in a string because of it). Anyhow, I agree that EntityClass should return something other than a MAV when encountering a nonexistant entity. All Blitz has to do is cycle through all of its entities and check to see if any of them have the memory address of the one you're looking for- if not, return "None", "Nil", "Null", "", etc..


slenkar(Posted 2004) [#15]
you have to take into account that children entities are freed automatically when the parent is freed, so you have to delete their types when the parent is freed.


Rook Zimbabwe(Posted 2004) [#16]
Bot Builder I have been waiting for the ability to do those things... my game could have been written with types and would have been SOOOOOOOOO much easier to program the check routines!!! Oh well... it works OK!
:)
-RZ


Zethrax(Posted 2004) [#17]
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.


Michael Reitzenstein(Posted 2004) [#18]
String comparisons are sometimes a bit off for me for some reason

A bit off!?

All Blitz has to do is cycle through all of its entities and check to see if any of them have the memory address of the one you're looking for- if not, return "None", "Nil", "Null", "", etc..

If it were that easy, it would have been done already. Mark explained why that wouldn't work in all cases in the last EntityExists( ) thread.


Damien Sturdy(Posted 2004) [#19]
@Axeman : I thought that be the case.. oh well!..


ronbravo(Posted 2004) [#20]
Cygnus, Why don't you just make your own delete entity function that would set the handle to zero

example:




Function DeleteEntity( EntityHandle )

If EntityHandle <> 0
FreeEntity EntityHandle
EntityHandle = 0
Endif

EndFunction





Now go into your code and replace all the blitz "FreeEntity" commands with your own "DeleteEntity" Function. Hope that helps.


DrakeX(Posted 2004) [#21]
mojokool - might work, if blitz supported passing parameters byref ;) right now that function would not affect the variable being passed in. which means you'd have to do something like

e=DeleteEntity(e)

and have "DeleteEntity" return a 0. which is ugly.


Damien Sturdy(Posted 2004) [#22]
That idea i have tried and DrakeX is correct.. this isnt a case of something i cant do without, its a case of something thatd speed things up in the future. Right now, my problem is almost solved in fact!


N(Posted 2004) [#23]
If it were that easy, it would have been done already. Mark explained why that wouldn't work in all cases in the last EntityExists( ) thread.


Uh huh. What kind of EntityExists are you referring to here? I'm talking about the kind that tells you whether or not an entity occupies the memory address you provide it, not the kind that magically knows what entity that should be there.

Edit: And just to note, the kind I'm talking about I currently have implemented in my own engine, and it does what I want it to: tells me that an entity is occupying a particular space.


jhocking(Posted 2004) [#24]
"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.


Bot Builder(Posted 2004) [#25]
What mark should do is have a list of handles for existing entities and then on an entityclass call go through the list and see if there is an entry. simple enough. Might be a bit slow once you got a really large amount of entities but ohwell. This would also allow you to loop through all the entities in existance.


_PJ_(Posted 2004) [#26]
Ahh can't test this while Im at work, bnut I just thought...

COuld you have your own EntityClass function?

based on EntityName$ perhaps and set this to something relevant at the creation/loading every mesh?


Damien Sturdy(Posted 2004) [#27]
Maybe thats an idea but im not too sure i understand what you mean.. Also, im using EntityNames to seperate surfaces of a mesh into "explodable" parts of meshes... (so the ships explode into many parts)

I could create new load/create routines, but as of last night, I managed to iron out my problem.

I intend on writing a "lib" type routine where you "addentity" to a list, and and to remove it, use the seperate "removeentity" or "deleteentity" commands, which checks for the entity and if it still exists. if not, i can do what i require from there. The basis for this is in a script engine im currently working on.

there are quite a few interesting ideas to go through actually.