Raised This Month: $32 Target: $400
 8% 

How to loop all entities?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
byetovice
Senior Member
Join Date: Nov 2009
Location: Turkey
Old 07-19-2011 , 04:12   How to loop all entities?
Reply With Quote #1

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 And does it slow down the server ?
__________________
Working on mods..
byetovice is offline
Hunter-Digital
Veteran Member
Join Date: Aug 2006
Location: In the Game [ro]
Old 07-19-2011 , 04:46   Re: How to loop all entities?
Reply With Quote #2

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
            }
        }
    }
}
__________________
Hunter-Digital is offline
Tirant
Veteran Member
Join Date: Jul 2008
Location: Los Angeles, California
Old 07-19-2011 , 06:03   Re: How to loop all entities?
Reply With Quote #3

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);
    }
}
__________________

PM me if you're interested in buying the Credits addition for Base Builder
Battlefield Rebirth [66% done]
Call of Duty: MW2 [100% done]
Base Builder [100% done]

Last edited by Tirant; 07-19-2011 at 06:07.
Tirant is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 07-19-2011 , 09:13   Re: How to loop all entities?
Reply With Quote #4

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.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Tirant
Veteran Member
Join Date: Jul 2008
Location: Los Angeles, California
Old 07-19-2011 , 11:44   Re: How to loop all entities?
Reply With Quote #5

Quote:
Originally Posted by Exolent[jNr] View Post
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()
__________________

PM me if you're interested in buying the Credits addition for Base Builder
Battlefield Rebirth [66% done]
Call of Duty: MW2 [100% done]
Base Builder [100% done]
Tirant is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 07-19-2011 , 11:48   Re: How to loop all entities?
Reply With Quote #6

Quote:
Originally Posted by Tirant View Post
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.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
byetovice
Senior Member
Join Date: Nov 2009
Location: Turkey
Old 07-19-2011 , 13:06   Re: How to loop all entities?
Reply With Quote #7

Hunter-digital's code is working
Thanks. Now i can make a player completely invisible (admin mark,admin cloak are invisible now)
__________________
Working on mods..
byetovice is offline
abdul-rehman
Veteran Member
Join Date: Jan 2010
Location: Khi, Pakistan
Old 07-19-2011 , 15:36   Re: How to loop all entities?
Reply With Quote #8

Quote:
Originally Posted by Exolent[jNr] View Post
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 ?
__________________

My Plugins For ZP

Inactive due to College and Studies
abdul-rehman is offline
Send a message via Yahoo to abdul-rehman Send a message via Skype™ to abdul-rehman
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 07-19-2011 , 15:37   Re: How to loop all entities?
Reply With Quote #9

Quote:
Originally Posted by abdul-rehman View Post
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.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
abdul-rehman
Veteran Member
Join Date: Jan 2010
Location: Khi, Pakistan
Old 07-19-2011 , 15:40   Re: How to loop all entities?
Reply With Quote #10

Quote:
Originally Posted by Exolent[jNr] View Post
No, it gives the max entities allowed in the server.
Oh, now i get it..!
__________________

My Plugins For ZP

Inactive due to College and Studies
abdul-rehman is offline
Send a message via Yahoo to abdul-rehman Send a message via Skype™ to abdul-rehman
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 22:31.


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