Count entities?
Moderator: InsideQC Admins
7 posts
• Page 1 of 1
Count entities?
Are there any ways to count entities with same classname or team number in qc?
- Stealth Kill
- Posts: 83
- Joined: Fri Dec 29, 2006 12:34 pm
I think you can probably do that by adding/calling a check in PlaceItem() (in items.qc), something like
if (self.classname == "blablabla") blablablanumber = blablablanumber + 1;
somewhere at the beginning at the routine...
I think it's how I did it when I wanted to count the number of pickable quads in Quad Hunt.
if (self.classname == "blablabla") blablablanumber = blablablanumber + 1;
somewhere at the beginning at the routine...
I think it's how I did it when I wanted to count the number of pickable quads in Quad Hunt.
Neurotic Conversions - New location: Update your bookmarks!
-

CocoT - Posts: 695
- Joined: Tue Dec 14, 2004 5:39 pm
- Location: Belly-Gum
From the top of my head (untested, so is very likely to have some naive bug lying around):
- Code: Select all
float CountEntities (string name) =
{
local float total = 0;
local entity ent;
ent = find (world, classname, name);
while (ent != world) {
total = total + 1;
ent = find (ent, classname, name);
}
return total;
}
I know FrikaC made a cgi-bin version of the quakec interpreter once and wrote part of his website in QuakeC
(LordHavoc)
-

frag.machine - Posts: 2090
- Joined: Sat Nov 25, 2006 1:49 pm
frag.machine wrote:From the top of my head (untested, so is very likely to have some naive bug lying around):
- Code: Select all
float CountEntities (string name) =
{
local float total = 0;
local entity ent;
ent = find (world, classname, name);
while (ent != world) {
total = total + 1;
ent = find (ent, classname, name);
}
return total;
}
That looks pretty clean, except that your declaration of total will make it a constant. QC will initialize variables to an appropriate 0 (0 for floats, '0 0 0' for vectors, world for entities (think of them as "pointer to entity" if this doens't make sense to you), "" for strings; only function pointers need to initialized), so you can just say say local float total;
Alternative methods usually involve counting entities when they spawn or are created, and then removing them from the total when they are removed. This may involve having to make a wrapper for remove() for certain entities, although you can often use their touch or th_die functions to do this. However, it's useful to know the find() method because it has its uses. Also know that you can only look for strings this way without needing certain engine extensions.
-

Lardarse - Posts: 266
- Joined: Sat Nov 05, 2005 1:58 pm
- Location: Bristol, UK
As I said, it was very likely to have something wrong or just silly. That's what happens when you have to deal with lots of different programming languages at same time (QuakeC, C, Java, C#, JavaScript, etc): you start mixing things and adopting the same behaviour in all cases - even when is not necessary. But TBH I don't recall having problems with variables being initialized during declaration in QuakeC before.
I know FrikaC made a cgi-bin version of the quakec interpreter once and wrote part of his website in QuakeC
(LordHavoc)
-

frag.machine - Posts: 2090
- Joined: Sat Nov 25, 2006 1:49 pm
First declare either a regular global float or entity assignable float.
Global float usage:
total_ents = CountEntites("player");
For entity assignable floats: (Example only)
self.total_ents = CountEntities("player");
Global float usage:
total_ents = CountEntites("player");
For entity assignable floats: (Example only)
self.total_ents = CountEntities("player");
-

Dr. Shadowborg - InsideQC Staff
- Posts: 1110
- Joined: Sat Oct 16, 2004 3:34 pm
7 posts
• Page 1 of 1
Who is online
Users browsing this forum: No registered users and 1 guest