Inside3D!
     

Count entities?

 
Post new topic   Reply to topic    Inside3d Forums Forum Index -> QuakeC Programming
View previous topic :: View next topic  
Author Message
Stealth Kill



Joined: 29 Dec 2006
Posts: 83

PostPosted: Wed Aug 20, 2008 5:55 pm    Post subject: Count entities? Reply with quote

Are there any ways to count entities with same classname or team number in qc?
Back to top
View user's profile Send private message
CocoT



Joined: 14 Dec 2004
Posts: 599
Location: Belly-Gum

PostPosted: Wed Aug 20, 2008 7:33 pm    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail Visit poster's website
frag.machine



Joined: 25 Nov 2006
Posts: 728

PostPosted: Thu Aug 21, 2008 1:49 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
Lardarse



Joined: 05 Nov 2005
Posts: 243
Location: Bristol, UK

PostPosted: Thu Aug 21, 2008 8:31 am    Post subject: Reply with quote

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
View user's profile Send private message
frag.machine



Joined: 25 Nov 2006
Posts: 728

PostPosted: Fri Aug 22, 2008 2:29 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
Quake123



Joined: 29 Aug 2008
Posts: 3

PostPosted: Sun Aug 31, 2008 10:49 am    Post subject: Reply with quote

How can i use this?

float CountEntities ("player"); ??
Back to top
View user's profile Send private message
Dr. Shadowborg
Inside3D Staff


Joined: 16 Oct 2004
Posts: 726

PostPosted: Sun Aug 31, 2008 4:05 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Inside3d Forums Forum Index -> QuakeC Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2004 phpBB Group