Forum

Count entities?

Discuss programming in the QuakeC language.

Moderator: InsideQC Admins

Count entities?

Postby Stealth Kill » Wed Aug 20, 2008 5:55 pm

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

Postby CocoT » Wed Aug 20, 2008 7:33 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.
Neurotic Conversions - New location: Update your bookmarks!
User avatar
CocoT
 
Posts: 695
Joined: Tue Dec 14, 2004 5:39 pm
Location: Belly-Gum

Postby frag.machine » Thu Aug 21, 2008 1:49 am

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)
User avatar
frag.machine
 
Posts: 2090
Joined: Sat Nov 25, 2006 1:49 pm

Postby Lardarse » Thu Aug 21, 2008 8:31 am

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.
User avatar
Lardarse
 
Posts: 266
Joined: Sat Nov 05, 2005 1:58 pm
Location: Bristol, UK

Postby frag.machine » Fri Aug 22, 2008 2:29 am

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)
User avatar
frag.machine
 
Posts: 2090
Joined: Sat Nov 25, 2006 1:49 pm

Postby Quake123 » Sun Aug 31, 2008 10:49 am

How can i use this?

float CountEntities ("player"); ??
Quake123
 
Posts: 3
Joined: Fri Aug 29, 2008 10:13 pm

Postby Dr. Shadowborg » Sun Aug 31, 2008 4:05 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");
User avatar
Dr. Shadowborg
InsideQC Staff
 
Posts: 1110
Joined: Sat Oct 16, 2004 3:34 pm


Return to QuakeC Programming

Who is online

Users browsing this forum: No registered users and 1 guest