View previous topic :: View next topic |
Author |
Message |
Stealth Kill
Joined: 29 Dec 2006 Posts: 83
|
Posted: Wed Aug 20, 2008 5:55 pm Post subject: Count entities? |
|
|
Are there any ways to count entities with same classname or team number in qc? |
|
Back to top |
|
 |
CocoT

Joined: 14 Dec 2004 Posts: 599 Location: Belly-Gum
|
Posted: Wed Aug 20, 2008 7:33 pm Post subject: |
|
|
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. _________________ http://www.planetcocot.net/ |
|
Back to top |
|
 |
frag.machine

Joined: 25 Nov 2006 Posts: 728
|
Posted: Thu Aug 21, 2008 1:49 am Post subject: |
|
|
From the top of my head (untested, so is very likely to have some naive bug lying around):
Code: |
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;
}
|
_________________ frag.machine - Q2K4 Project
http://fragmachine.quakedev.com/ |
|
Back to top |
|
 |
Lardarse

Joined: 05 Nov 2005 Posts: 243 Location: Bristol, UK
|
Posted: Thu Aug 21, 2008 8:31 am Post subject: |
|
|
frag.machine wrote: | From the top of my head (untested, so is very likely to have some naive bug lying around):
Code: | 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. |
|
Back to top |
|
 |
frag.machine

Joined: 25 Nov 2006 Posts: 728
|
Posted: Fri Aug 22, 2008 2:29 am Post subject: |
|
|
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. _________________ frag.machine - Q2K4 Project
http://fragmachine.quakedev.com/ |
|
Back to top |
|
 |
Quake123
Joined: 29 Aug 2008 Posts: 3
|
Posted: Sun Aug 31, 2008 10:49 am Post subject: |
|
|
How can i use this?
float CountEntities ("player"); ?? |
|
Back to top |
|
 |
Dr. Shadowborg Inside3D Staff

Joined: 16 Oct 2004 Posts: 726
|
Posted: Sun Aug 31, 2008 4:05 pm Post subject: |
|
|
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"); _________________ "Roboto suggests Plasma Bazooka." |
|
Back to top |
|
 |
|