[HOWTO] Finding Entites

For scripting examples for learning how to use AMXX.
User avatar
Archive-Poster
Posts: 1267
Gender:
United States of America

[HOWTO] Finding Entites

Post by Archive-Poster »

Introduction

It is sometimes necessary to find an entity in the world for some reason. For example, you may need to find a button in the world, and then remove it (this example will be covered later). This article will explain how to do so.

Functions

There are a number of functions built into AMX Mod X / AMXX Modules that allow you to find entities.

find_ent_by_class
find_ent_by_model
find_ent_by_owner
find_ent_by_target
find_ent_by_tname
find_ent_in_sphere
find_sphere_class

find_ent_by_class

<Function is included in Engine>
This function allows you to find an entity based on it's classname. All entities have classnames. For example, a player's classname is "player", while a button is either "func_button" or "func_rot_button".
To find an entity using this, one would simply use the following:

Code: Select all

find_ent_by_class(-1,"classname_to_find");
find_ent_by_model

<Function is included in Engine>
This function can be used to find an entity based on its model. Some entities do not have models, meaning they cannot be found using this method. Also, it is, most of the time, better to use another type of ent finding, as this can be highly inaccurate and be broken by something as simple as a model changing plugin. This function also takes into account the class name, and is generally used more for isolating a certain model from a set of classnames (ex you have 3 grenades, with different models, and want to find 1 of them)
To find an entity using this, one would use the following:

Code: Select all

find_ent_by_model(-1,"classname_to_find","model_to_find")
find_ent_by_owner

<Function is included in Engine>
This function can be used to find an entity based on an owner. For example, if there is a player in the world, and you want to find if he has thrown a grenade out, you would use this and check for "grenade" entities that's owner is the player. Note: planted C4 (in CS) also have "grenade" classname.
To find an entity using this, one would use the following:

Code: Select all

find_ent_by_owner(-1,"classname_to_find",owner_id,0);
Notice the last parameter. This is a mode to find in.

find_ent_by_target

<Function is included in Engine>
This function allows one to find an entity based on its target. This function also checks for classname.
To find an entity using this, one would use the following:

Code: Select all

find_ent_by_target(-1,"classname_to_find");
find_ent_by_tname

<Function is included in Engine> This essentially functions the same as find_ent_by_target.


find_ent_in_sphere

<Function is included in Engine>
This function can be used to find an entity within a certain distance of another entity. This can be quite handy to, for example, see if a player is around another player, and then deal damage or slap that player. This is effectively a shortcut to using find_ent_by_class and get_distance(_f) together.
To find an entity using this, one would use the following:

Code: Select all

find_ent_in_sphere(-1,location_of_ent_vector,radius_float);
find_sphere_class

This function is probably the most complex of all the find_ent functions. This allows you to find an entity inside a certain radius of another entity, with a certian classname. This is effectively a shortcut to using find_ent_by_class & get_distance & entity_get_string(ent,EV_SZ_classname... or find_ent_in_sphere & entity_get_string(ent,EV_SZ_classname...
To find an entity using this, one would use the following:

Code: Select all

new entlist[513];
find_sphere_class(0,"classname_to_find",radius_float,entlist,512,origin_vector);
Also, note that origin_vector and the first parameter (_aroundent) should not both be specified. If _aroundent is specified as 0, origin_vector is used. If origin_vector is not specified, or is 0, then _aroundent is used.

Looping

If, for example, one wants to loop one of these, it is quite simple to do so.
Example:

Code: Select all

while((ent = find_ent_by_class(ent,"classname_to_find") != 0)
{
    // do whatever
}
This will find every entity with classname "classname_to_find" and then execute the "// do whatever" code every time it finds one.
User avatar
Archive-Poster
Posts: 1267
Gender:
United States of America

Re: [HOWTO] Finding Entites

Post by Archive-Poster »

there is no way to scan with aroundent 0 and origin 0, unless you want to search from origin 0,0,0, which I can't imagine you'd want to do

find_sphere_class is probably not the function you want, you probably want find_ent_by_class.
User avatar
Archive-Poster
Posts: 1267
Gender:
United States of America

Re: [HOWTO] Finding Entites

Post by Archive-Poster »

Origin is a vector. That means that it's a coordinate on the map somewhere.

A vector is represented through 3 cells, usually as floats. When dealing with coordinates, it basically looks like this:

Code: Select all

new Float:Vector[3]
Vector[0] // this is the x value (left/right)
Vector[1] // this is the y value (forward/back)
Vector[2] // this is the z value (up/down)
It isn't exactly like that per se (since if you're looking in different directions they're not really left/right and forward/back) but that's as close a representation I can give with text.

Here's how you represent a static vector:

Code: Select all

Float:{100.0,-300.0,400.0}
which means x is 100, y is -300, z is 400, etc.

This param in the function find_sphere_class is used when you want to search around a location where there is no ent, such as the point a player is aiming at. I've only used this function like twice before, and once was to make an explosion effect around a location that a player was looking (using origin and not _aroundent), and another was finding players around a player when they used a command (using _aroundent and not origin).
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest