AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   How to loop all entities? (https://forums.alliedmods.net/showthread.php?t=162438)

byetovice 07-19-2011 04:12

How to loop all entities?
 
There was an idea like : "loop all entities. if movetype is follow and aiment is your player, its attached!"

It's good an idea! But i don't know how to loop all entities :D And does it slow down the server ?

Hunter-Digital 07-19-2011 04:46

Re: How to loop all entities?
 
It doesn't slow it that much but it would be faster to store attached entities on creation.... unless your plugin doesn't make them, in that case, this searches for entities near players:

Code:

new g_iMaxPlayers

// plugin_init()
g_iMaxPlayers = get_maxplayers()

// your function
new const Float:fRadius = 8.0 // I really dunno how much to set here =) if it doesn't detect squat, increase
new Float:fOrigin[3]
new ent

for(new id = 1; id <= g_iMaxPlayers; id++)
{
    if(is_user_alive(id))
    {
        entity_get_vector(id, EV_VEC_origin, fOrigin)

        ent = g_iMaxPlayers // skip players from the search

        while((ent = find_ent_in_sphere(ent, fOrigin, fRadius))
        {
            if(entity_get_int(ent, EV_ENT_aiment) == id && entity_get_int(ent, EV_INT_movetype) == MOVETYPE_FOLLOW)
            {
                // ent is attached to id
            }
        }
    }
}


Tirant 07-19-2011 06:03

Re: How to loop all entities?
 
I think this might do the trick. I'm not sure if you create entities or not, so put the entNum before the loops or make it a global variable and initialize at plugin_init(). If you want to reset all entities on round change, I also provided that. I honestly don't think this will slow down the server to any noticeable extent. I mean, in my base builder mod, I reset all entities origin, angles, mins, maxs and owners every round end, and I don't really feel anything (and that's usually ~200 objects). But this will slow down your server, any loop this big would.

Code:

#include <amxmodx>
#include <engine>

#define SetAttachedEntity(%1,%2)    (entity_set_int(%1, EV_INT_iuser1, %2))

new entNum = entity_count();

public getAttachedEntities(id) {
    for (new i = get_maxplayers()+1; i < entNum; i++) {
        if (!is_valid_ent(i))
            continue;
       
        if (entity_get_int(i, EV_ENT_aiment) == id && entity_get_int(i, EV_INT_movetype) == MOVETYPE_FOLLOW) {
            SetAttachedEntity(i,id);
        }
    }
}

public resetAttachedEntities() {
    for (new i = get_maxplayers()+1; i < entNum; i++) {
        if (!is_valid_ent(i))
            continue;

        SetAttachedEntity(i,0);
    }
}


Exolent[jNr] 07-19-2011 09:13

Re: How to loop all entities?
 
entity_count() gives the number of entities in the server, not max entities.
Instead, you should use global_get(glb_maxEntities) and loop like you would for max players, except checking if entities are valid.

Tirant 07-19-2011 11:44

Re: How to loop all entities?
 
Quote:

Originally Posted by Exolent[jNr] (Post 1513775)
entity_count() gives the number of entities in the server, not max entities.
Instead, you should use global_get(glb_maxEntities) and loop like you would for max players, except checking if entities are valid.

But doesn't the number of entities reflect indexes also? I don't think you can have an entity id above entity_count()

Exolent[jNr] 07-19-2011 11:48

Re: How to loop all entities?
 
Quote:

Originally Posted by Tirant (Post 1513877)
But doesn't the number of entities reflect indexes also? I don't think you can have an entity id above entity_count()

I don't think entity_count() has anything to do with entity indexes.

Let's say you create 300 entities and delete the first 200.
That leaves 100 entities with indexes 201-300.
Therefore, the number of entities is less than the max entity index.

byetovice 07-19-2011 13:06

Re: How to loop all entities?
 
Hunter-digital's code is working :D
Thanks. Now i can make a player completely invisible (admin mark,admin cloak are invisible now)

abdul-rehman 07-19-2011 15:36

Re: How to loop all entities?
 
Quote:

Originally Posted by Exolent[jNr] (Post 1513880)
I don't think entity_count() has anything to do with entity indexes.

Let's say you create 300 entities and delete the first 200.
That leaves 100 entities with indexes 201-300.
Therefore, the number of entities is less than the max entity index.

Then global_get(glb_maxEntities) will give us the total number of entities which were created and destroyed on the map ?

Exolent[jNr] 07-19-2011 15:37

Re: How to loop all entities?
 
Quote:

Originally Posted by abdul-rehman (Post 1514051)
Then global_get(glb_maxEntities) will give us the total number of entities which were created and destroyed on the map ?

No, it gives the max entities allowed in the server.

abdul-rehman 07-19-2011 15:40

Re: How to loop all entities?
 
Quote:

Originally Posted by Exolent[jNr] (Post 1514052)
No, it gives the max entities allowed in the server.

Oh, now i get it..!


All times are GMT -4. The time now is 01:11.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.