Forum

Detecting removed entities

Discuss programming in the QuakeC language.

Moderator: InsideQC Admins

Detecting removed entities

Postby frag.machine » Sun Feb 21, 2010 12:10 pm

Is there a quick, fail-safe way to do this ? I have entities that keeps references to other entities which may be removed between execution cycles (touched items, killed monsters, etc). At this moment I am resorting to intercept all calls to remove() and assigning string_null to classname before actually calling the built-in function (so my entity can check if it's reference stills valid), but this is awkward.

EDIT: Actually, if there's no way in standard QuakeC to verify if an entity stills valid, it would be a worthy addition to QSB 1.0 wish list.
I know FrikaC made a cgi-bin version of the quakec interpreter once and wrote part of his website in QuakeC :) (LordHavoc)
User avatar
frag.machine
 
Posts: 2090
Joined: Sat Nov 25, 2006 1:49 pm

Postby Spike » Sun Feb 21, 2010 1:00 pm

csqc specifies a builtin that can do it :)

you shouldn't really be poking an entity that has been freed, you ought to have removed all links to it before freeing.

that said, an entity will not be reused during the same qc invocation, and it can be useful to see if a called function removed it (like a touch, hence why the builtin in csqc).
The standard remove builtin does clear out some fields, including:
model
takedamage
modelindex
colormap
skin
frame
origin
angles
nextthink
solid

Actually, when I say clear, I partly lie. nextthink is not cleared. Interestingly, it is set to -1. Spawn then clears the entire entity to 0, resetting nextthink to 0 also.
So you can test nextthink==-1 to see if an entity is freed.
Spike
 
Posts: 2892
Joined: Fri Nov 05, 2004 3:12 am
Location: UK

Postby frag.machine » Sun Feb 21, 2010 2:40 pm

Well, since my code is part of server-side AI, CSQC won't help me on that :)

Ah well, guess I'll keep my classname clearing code, then :roll: . Thanks for the info, Spike.
I know FrikaC made a cgi-bin version of the quakec interpreter once and wrote part of his website in QuakeC :) (LordHavoc)
User avatar
frag.machine
 
Posts: 2090
Joined: Sat Nov 25, 2006 1:49 pm

Postby Sajt » Sun Feb 21, 2010 7:40 pm

This classname clearing idea is clever but I don't know if it's portable. A "good" VM (DarkPlaces might do this?) wouldn't let you access entities marked for removal at all. Have you tested your code with DarkPlaces?
F. A. Špork, an enlightened nobleman and a great patron of art, had a stately Baroque spa complex built on the banks of the River Labe.
Sajt
 
Posts: 1215
Joined: Sat Oct 16, 2004 3:39 am

Re: Detecting removed entities

Postby Teiman » Sun Feb 21, 2010 9:39 pm

frag.machine wrote:Is there a quick, fail-safe way to do this ? I have entities that keeps references to other entities which may be removed between execution cycles (touched items, killed monsters, etc). At this moment I am resorting to intercept all calls to remove() and assigning string_null to classname before actually calling the built-in function (so my entity can check if it's reference stills valid), but this is awkward.

EDIT: Actually, if there's no way in standard QuakeC to verify if an entity stills valid, it would be a worthy addition to QSB 1.0 wish list.


you can have your own remove() func that set .isRemoved = 1;


pseudocode follows:

void( entity ent) remove =
{

ent.isRemoved = 1;

buitin_remove( ent );

}
Teiman
 
Posts: 309
Joined: Sun Jun 03, 2007 9:39 am

Postby frag.machine » Sun Feb 21, 2010 11:41 pm

Sajt wrote:This classname clearing idea is clever but I don't know if it's portable. A "good" VM (DarkPlaces might do this?) wouldn't let you access entities marked for removal at all. Have you tested your code with DarkPlaces?


Nope, tested only in FitzQuake 0.85, seems to work OK.
But I agree, a "good" VM shouldn't allow me to refer discarded entities. OTOH, a "good" VM should also supply me some secure way to test if an entity reference stills valid (for example, a builtin). That's why I mentioned adding this to QSB 1.0 wish list (it's simple to implement and really useful for QC coders).

@Teiman: what I did was almost like what you suggested, only without the extra field. I assumed that any active entity should have at least the .classname set:
Code: Select all
void(entity e) _remove                          = #15;
void(entity e) remove =
{
    e.classname = string_null;
    _remove (e);
};
I know FrikaC made a cgi-bin version of the quakec interpreter once and wrote part of his website in QuakeC :) (LordHavoc)
User avatar
frag.machine
 
Posts: 2090
Joined: Sat Nov 25, 2006 1:49 pm

Postby Spike » Sun Feb 21, 2010 11:44 pm

too many mods reference dead entities one way or another. a generic engine could never do it.
I had a warning about it in FTE a while back, but it was just too spammy.
Its not a problem so long as its the same frame in which they were removed.
Spike
 
Posts: 2892
Joined: Fri Nov 05, 2004 3:12 am
Location: UK

Postby Sajt » Mon Feb 22, 2010 7:20 am

Ah, I forgot about that most fun part of Quake engine coding. (Badly coded mods from 1997.)

Anyway, the "good" VM (no longer talking about Quake) would clear all your references to null automatically, because a good VM should never let you get garbage...
F. A. Špork, an enlightened nobleman and a great patron of art, had a stately Baroque spa complex built on the banks of the River Labe.
Sajt
 
Posts: 1215
Joined: Sat Oct 16, 2004 3:39 am


Return to QuakeC Programming

Who is online

Users browsing this forum: No registered users and 1 guest