Raised This Month: $51 Target: $400
 12% 

Question about native : get_players


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
101
Member
Join Date: Nov 2023
Old 12-07-2023 , 10:51   Question about native : get_players
Reply With Quote #1

get_players

does this native do calculations and checking every time it is called ? or it throws a pre-prepared data directly?

Last edited by 101; 12-07-2023 at 11:20.
101 is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 12-07-2023 , 11:33   Re: Question about native : get_players
Reply With Quote #2

get_players will check every time, it does not use cached data. Using cached data would not make much sense, as different calls to get_players could specify different flags so the native would still need to iterate over the cache and filter it.
__________________
HamletEagle is offline
101
Member
Join Date: Nov 2023
Old 12-08-2023 , 02:11   Re: Question about native : get_players
Reply With Quote #3

Quote:
Originally Posted by HamletEagle View Post
get_players will check every time, it does not use cached data. Using cached data would not make much sense, as different calls to get_players could specify different flags so the native would still need to iterate over the cache and filter it.
thank u sir .
i think it does only a single check which is the : flags check .
any other data had been pre-prepared and cached before the native call started , it may looks like this :
Instant calculations ,
Code:
get_players( ......)
{
	new x,count;
	while (x=find_player(given flags)) players[count++]=x; 

//....
//....
and the pre-cahed data which had been calculated before ,
Code:
on_player_death(id)
{
	flags[id][0]=false ;  // set the related to "a" bool to false or null 
	flags[id][1]=true; //set b flag bool 
}

on_player_connect(id)
{
	P_index[Hello++]=id;
	flags[id][8]=true; // i flag
	flags[id][is_user_bot?3:2]=true // c,d flags
}

on_client_change_team(id)
{
	...
	...
	...
my question was to ensure if its a good idea to use this native inside a repeated timer or not ,cuze i think native does only 3 short loops instantly :

#1-loop : from 0 to max avaliable client in server (depends on pre-prepared data optimization and integer "Hello")
#2-loop : general falgs loop from "a" to "i"
#3-loop : number of given flag to check - "aec" for example + a very simple string check in case "e" or "f"
the second and third loops might be mixed by bits method : if (flags&a)

as a result : i think it is an ideal Native to call if only if it was a one-time call , because checking a flag bits/bool for a player is too much easier than manual instant checks : (is_user_alive(),is_use_connected.....) , here is the proof :
Code:
static cell AMX_NATIVE_CALL is_user_alive(AMX *amx, cell *params) /* 1 param */ {     int index = params[1];     if (index < 1 || index > gpGlobals->maxClients)     {         return 0;     }         CPlayer* pPlayer = GET_PLAYER_POINTER_I(index);     if (g_bmod_tfc)     {         edict_t *e = pPlayer->pEdict;         if (e->v.flags & FL_SPECTATOR ||             (!e->v.team || !e->v.playerclass))         {             return 0;         }     }    
    return ((pPlayer->ingame && pPlayer->IsAlive()) ? 1 : 0);
}

while instant check of a single flag looks like this
Code:
check_flag(id,x) {
    return flags[id][x];
    //return flags[id]&x;
}
Finally, I chose another method. If you have any advice related to the attached file, I am ready to listen .
thank u again , sir .
Attached Files
File Type: sma Get Plugin or Get Source (Hud_Teams_Info.sma - 31 views - 1.4 KB)
101 is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 12-09-2023 , 00:13   Re: Question about native : get_players
Reply With Quote #4

Why don't you just look at the native to see what it does?
PHP Code:
static cell AMX_NATIVE_CALL get_players(AMX *amxcell *params/* 4 param */
{
    
int iNum 0;
    
int ilen;
    
charsptemp get_amxstring(amxparams[3], 0ilen);
    
int flags UTIL_ReadFlags(sptemp);

    
cell *aPlayers get_amxaddr(amxparams[1]);
    
cell *iMax get_amxaddr(amxparams[2]);

    
int team 0;

    if (
flags 48)
    {
        
sptemp get_amxstring(amxparams[4], 0ilen);

        if (
flags 16)
        {
            if (
flags 64)
                
team g_teamsIds.findTeamId(sptemp);
            else
                
team g_teamsIds.findTeamIdCase(sptemp);
        }
    }

    for (
int i 1<= gpGlobals->maxClients; ++i)
    {
        
CPlayerpPlayer GET_PLAYER_POINTER_I(i);
        if (
pPlayer->ingame || ((flags 256) && pPlayer->initialized))
        {
            if (
pPlayer->IsAlive() ? (flags 2) : (flags 1))
                continue;
            if (
pPlayer->IsBot() ? (flags 4) : (flags 8))
                continue;
            if ((
flags 16) && (pPlayer->teamId != team))
                continue;
            if ((
flags 128) && (pPlayer->pEdict->v.flags FL_PROXY))
                continue;
            if (
flags 32)
            {
                if (
flags 64)
                {
                    if (
stristr(pPlayer->name.chars(), sptemp) == NULL)
                        continue;
                }
                else if (
strstr(pPlayer->name.chars(), sptemp) == NULL)
                    continue;
            }
            
aPlayers[iNum++] = i;
        }
    }

    *
iMax iNum;
    
    return 
1;

__________________
Bugsy is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 12-09-2023 , 00:19   Re: Question about native : get_players
Reply With Quote #5

I've been told is much more efficient to use get_players() in most all cases even if you do need to loop through them. One thing that it does is that it optimizes how many players you need to loop through.

I'm not entirely sure if your method would be any better or worse than having to call multiple calls to get_players() to get the same resulting data. With modern computers it might not be all that significant either way, especially since you're not calling it all that often.

P.S. Whitespace is not your enemy, it is your friend when used correctly so you should use it. Nobody wants to have to decipher your lack of whitespace when trying to help, it's just annoying. With such a small community, it's probably a good idea to make code that you post readable to make it easier to help you.
__________________
fysiks is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 12-09-2023 , 06:11   Re: Question about native : get_players
Reply With Quote #6

I think that caching things like is_user_alive, is_use_connected, etc is pointless most of the time.

1. You are not gaining any tangible speed-up because calling those natives directly is already very cheap.
2. Correctly maintaining the cache may be non-trivial, error-prone, and require a lot of code.
3. It makes the whole plugin harder to understand and maintain.

I would only consider such micro-optimizations only if, in practice, the code is measurably too slow. That means I created it using a straightforward method, and while testing I noticed that it failed some performance metrics (like execution speed) and that the code is spending a lot of time calling natives like is_user_alive. Otherwise, even if the code is too slow, caching such native calls may not lead to any performance gain because they were not the reason for the slowdown.

Another potential pitfall to be aware of when thinking about caching native calls: keeping the cache updated should NOT be more expensive than just simply performing the native call.
__________________

Last edited by HamletEagle; 12-09-2023 at 06:14.
HamletEagle is offline
101
Member
Join Date: Nov 2023
Old 12-09-2023 , 07:27   Re: Question about native : get_players
Reply With Quote #7

Quote:
Originally Posted by Bugsy View Post
Why don't you just look at the native to see what it does?
I don't have any idea where to get native sources , i searched and didn't find them , and nothing f important about natives in here: https://www.amxmodx.org/api/

When i see this native for the first time , i felt that something wrong in it , and i was right .
i think there must be a general amxx plugin instead, that tracks events moment by moment and set players flags BOOLs method as the example above , and this general plugin should create a general native that gives information instantly without checking ( Teams, connections, status :alive...)
and here is a quick example of Only_bool_Flags_Check:
PHP Code:
native wtf(....,char flags_to_check,..)
{
    for (
i=0i<Hello i++)    //int Hello represents the max ingame-players count (Pre-calculated)
    
{
        
j0;
        While (
strlen(flags_to_check)    )
        {
                
f=find_flag_index(flags_to_check[j++]);
                if ( 
flags[p_index[i]][ ] )    count++;
        }
        if (
count==strlen(flags_to_check))        //player got all flags 
        
wtf[x++]=p_index[i];                                // so add him to array 
    
}
    *
iMax x;
}

find_flag_index(String:s_[1])
{
    for (
int i =max_flags ;i++)
    {
        if (
allowed_flags[i]==s_[0])
        return 
i;
    }
    
SetFailure...;

another issue in is_user_alive and other functions that Shouldn't check if is_user_connected , cuze this will duplicate checking inside loops .
anyway , thank you Mr Bugsy , Correct me if i was wrong .

Last edited by 101; 12-09-2023 at 20:51.
101 is offline
101
Member
Join Date: Nov 2023
Old 12-09-2023 , 07:48   Re: Question about native : get_players
Reply With Quote #8

Quote:
Originally Posted by HamletEagle View Post
keeping the cache updated should NOT be more expensive than just simply performing the native call.
this is a high level of analyzing that got me lost X'D

by the way , if natives have to calculate from the beginning , then why should i call it ?
i think natives are workers that should be ready to give a pre-prepared information instanly -as fast as possible -, and they don't have to work from zero , they should be ready,cached and prepared .
101 is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 12-09-2023 , 08:07   Re: Question about native : get_players
Reply With Quote #9

Quote:
Originally Posted by 101 View Post

by the way , if natives have to calculate from the beginning , then why should i call it ?
i think natives are workers that should be ready to give a pre-prepared information instanly -as fast as possible -, and they don't have to work from zero , they should be ready,cached and prepared .
From where did you get this information? It is not correct. Some natives will give you existing data (if they are simple getters), and some natives will do some work before returning a result. This is perfectly normal, expected and there is nothing wrong with it. There is nothing wrong with get_players.

Btw, is_user_alive should absolutely check if the user is connected first, as being connected is a prerequisite for being alive. So, if you ever need to know if a user is alive, you can simply do is_user_alive(id). You don't need to write is_user_connected(id) && is_user_alive(id).
__________________

Last edited by HamletEagle; 12-09-2023 at 08:09.
HamletEagle is offline
WATCH_D0GS UNITED
Senior Member
Join Date: Jan 2023
Old 12-09-2023 , 08:10   Re: Question about native : get_players
Reply With Quote #10

It could be really better to see the source of all natives to see how many stuff they're using to get the results.

After decompiling, you can get something like this:

PHP Code:
xs_vec_add(Float:in1[], Float:in2[], Float:out[])
{
    
out[0] = floatadd(in1[0], in2[0]);
    
out[1] = floatadd(in1[1], in2[1]);
    
out[2] = floatadd(in1[2], in2[2]);
    return 
0;

PHP Code:
xs_vec_mul_scalar(Float:vec[], Float:scalarFloat:out[])
{
    
out[0] = floatmul(vec[0], scalar);
    
out[1] = floatmul(vec[1], scalar);
    
out[2] = floatmul(vec[2], scalar);
    return 
0;

And some bunch of code to make these to work:

PHP Code:
new xs__ITaskId 32;
new 
xs__ITaskParam[1033] =
{
    
3210017625232839647254810911110010110811547991159597119112461091001080109111100101108115479911595103511151034946109100108010911110010110811547991159511599111117116461091001080109111100101108115479911595115103535348461091001080109111100101108115479912295971191124610910010801091111001011081154799122951035111510349461091001080109111100101108115479912295115991111171164610910010801091111001011081154799122951151035353484610910010801636649211895971191120118951035111510349011895115991111171160118951151035353480000000000000000001613625637600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001242483724966207448689921116124013641488161217361860198421082232235624802604272828522976310032243348347235963720384400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
};
new 
xs__TaskFlags[5] =
{
    
32100176252328
};
new 
xs__TaskFunc[48] =
{
    
32100176252328396472548109111100101108115479911595971191124610910010801091111001011081154799115951035111510349461091001080109111
};
new 
xs__TaskId 32;
new 
Float:xs__TaskInterval 32;
new 
xs__TaskParam[1033] =
{
    
3210017625232839647254810911110010110811547991159597119112461091001080109111100101108115479911595103511151034946109100108010911110010110811547991159511599111117116461091001080109111100101108115479911595115103535348461091001080109111100101108115479912295971191124610910010801091111001011081154799122951035111510349461091001080109111100101108115479912295115991111171164610910010801091111001011081154799122951151035353484610910010801636649211895971191120118951035111510349011895115991111171160118951151035353480000000000000000001613625637600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001242483724966207448689921116124013641488161217361860198421082232235624802604272828522976310032243348347235963720384400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
};
new 
xs__TaskRepeat 32;
new 
xs__global_null 32;
new 
xs__internalseed 32;
new 
xs__logtypenames[6][0] =
{
    {
        
109, ...
    },
    {
        
109, ...
    },
    {
        
109, ...
    },
    {
        
109, ...
    },
    {
        
109, ...
    },
    {
        
109, ...
    }
};
new 
xs__maxnum 32;
new 
xs__replace_buf[3072] =
{
    
321001762523283964725481091111001011081154799115959711911246109100108010911110010110811547991159510351115103494610910010801091111001011081154799115951159911111711646109100108010911110010110811547991159511510353534846109100108010911110010110811547991229597119112461091001080109111100101108115479912295103511151034946109100108010911110010110811547991229511599111117116461091001080109111100101108115479912295115103535348461091001080163664921189597119112011895103511151034901189511599111117116011895115103535348000000000000000000161362563760000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000124248372496620744868992111612401364148816121736186019842108223223562480260427282852297631003224334834723596372038440000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001923164405646888129361060118413081432155616801804192820522176230024242548267227962920304431683292341635403664378839124036416042844408453246564780490450285152527654005524564857725896602000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001282523765006247488729961120124413681492161617401864198821122236236024842608273228562980
}; 
As you can see, these natives are using other natives in order to work: floatadd and floatmul.

If you want to use the native only once in the plugin, it will be less CPU usage if you simply use these assignments directly in the code block:

same as xs_vec_add:

PHP Code:
            out[0] = (in[0] + out[0])
            
out[1] = (in[1] + out[1])
            
out[2] = (in[2] + out[2]) 
same as xs_vec_mul_scalar:

PHP Code:
            out[0] = (out[0] * scalar)
            
out[1] = (out[1] * scalar)
            
out[2] = (out[2] * scalar
But the decompiler isn't getting all native codes like these.

And a question we want a response is what these event flags are using for getting the results:

https://www.amxmodx.org/api/amxmodx/register_event

--
--
__________________
💻Know Our New Blog👄
🔗tube2downs.blogspot.com
WATCH_D0GS UNITED is offline
Reply



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 03:58.


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