AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Solved Getting global data from non-id publics (https://forums.alliedmods.net/showthread.php?t=333829)

Crackhead69 08-08-2021 15:24

Getting global data from non-id publics
 
Hello! I am wondering how could i retrieve global id saved variable to a public whom doesn't have the typical id index.
Example :
PHP Code:

#include <amxmodx>
#include <hamsandwich>

new g_Example[33]

public 
plugin_init()
{
    
RegisterHam(Ham_Spawn"player""player_spawn"1)
    
    
register_logevent("ev_round_end"2"1=Round_End")
}

public 
player_spawn(id)
{
    
g_Example[id] += 1
}

public 
ev_round_end()    // no  id  index
{
    if(
g_Example[id] >= 2)
    {
        
client_print(idprint_chat"Retrieved player's variable")
    }


It has nothing specific in the example that i want, i am just curious.
I have seen some plugins which used get_players for retrieving gobal data like that but it seemed pretty much situational, since in this example i don't see logic on how get_players will work.

Although i guess i could set a variable to hold the player's Id i guess.
Like so
PHP Code:

#include <amxmodx>
#include <hamsandwich>

new g_Example[33]
new 
temp_id

public plugin_init()
{
    
RegisterHam(Ham_Spawn"player""player_spawn"1)
    
    
register_logevent("ev_round_end"2"1=Round_End")
}

public 
player_spawn(id)
{
    
temp_id id
    g_Example
[temp_id] += 1
}

public 
ev_round_end()
{
    if(
g_Example[temp_id] >= 2)
    {
        
client_print(temp_idprint_chat"Retrieved player's variable")
    }


But is there any other more modern-like way of doing this?

Bugsy 08-08-2021 16:01

Re: Getting global data from non-id publics
 
I would avoid the temp_id method since it will not be reliable. There's nothing wrong with checking all players at round end using get_players(). What are you confused about? Round end is not a player-specific event so you cannot get a single player id for it. You need to loop through all players and react on them based on conditions.
PHP Code:


#include <amxmodx>
#include <hamsandwich>

new g_ExampleMAX_PLAYERS ];

public 
plugin_init()
{
    
RegisterHam(Ham_Spawn"player""player_spawn"1)
    
    
register_logevent("ev_round_end"2"1=Round_End")
}

public 
player_spawn(id)
{
    if ( 
is_user_aliveid ) )
    {
        
g_Exampleid ] += 1;
    }
}

public 
ev_round_end()
{
    new 
iPlayers32 ] , iNum iPlayer;
    
    
get_playersiPlayers iNum );
    
    for ( new 
iNum i++ )
    {
        
iPlayer iPlayers];
        
        if ( 
g_ExampleiPlayer ] >= )
        {
            
client_printiPlayer print_chat"Retrieved player's variable")
        }
    } 



Crackhead69 08-09-2021 04:02

Re: Getting global data from non-id publics
 
I see, but won't that trigger the client print to everybody if just One player has the value?

HamletEagle 08-09-2021 05:19

Re: Getting global data from non-id publics
 
Quote:

Originally Posted by Crackhead69 (Post 2754836)
I see, but won't that trigger the client print to everybody if just One player has the value?

Code:

client_print( iPlayer , print_chat, "Retrieved player's variable")
No. If you used 0 instead of iPlayer, then yes.

Maybe this will help clear your confusion. The code below is functionally equivalent to what Bugsy did, but now you have an explicit function, player_round_end(id), that takes the player index as an argument. Hope it will help you see why this works just fine. The loop is over all players, but you still execute the code over one player at a time.
(I'm not saying you should use the code below, I just provided it for intuition)
PHP Code:

#include <amxmodx>
#include <hamsandwich>

new g_ExampleMAX_PLAYERS ];

public 
plugin_init()
{
    
RegisterHam(Ham_Spawn"player""player_spawn"1)
    
    
register_logevent("ev_round_end"2"1=Round_End")
}

public 
player_spawn(id)
{
    if ( 
is_user_aliveid ) )
    {
        
g_Exampleid ] += 1;
    }
}

public 
ev_round_end()
{
    new 
iPlayers32 ] , iNum iPlayer;
    
    
get_playersiPlayers iNum );
    
    for ( new 
iNum i++ )
    {
        
player_round_end(iPlayers[i]);
    } 


player_round_end(id)
{
       if ( 
g_Example[id] >= )
       {
           
client_print(idprint_chat"Retrieved player's variable")
       }



Natsheh 08-09-2021 06:43

Re: Getting global data from non-id publics
 
Basically get_players is the same as looping players indexes from 1 to 32 except its done in a module which is much faster

PHP Code:

for(new id 1<= g_iMaxplayersid++)
{
     if(
is_user_connected(id))
     {
  
      }



HamletEagle 08-09-2021 07:07

Re: Getting global data from non-id publics
 
Quote:

Originally Posted by Natsheh (Post 2754843)
Basically get_players is the same as looping players indexes from 1 to 32 except its done in a module which is much faster

PHP Code:

for(new id 1<= g_iMaxplayersid++)
{
     if(
is_user_connected(id))
     {
  
      }



Speed is not the reason get_players is the recommended way. get_players should be used because it returns directly a list of valid player indexes. Moreover, it allows filtering players based on multiple conditions, which can come in handy.

The speed with a loop that runs at most 32 times is irrelevant. Even if running the loop in a module may be very slightly faster, as a whole get_players is likely slower than just looping from 1 to max players and checking. With get_players there is a loop inside the module, then there is the pawn c++ interop time and then you loop again in plugin over the results from get_players(which may be less than 32 because of filtering/the server is not full). But this is not an issue, because as I said previously, speed is not why get_players is recommended.

Crackhead69 08-09-2021 09:46

Re: Getting global data from non-id publics
 
Aah, i get it!
Thank you lots. :fox:

DJEarthQuake 08-09-2021 22:55

Re: Getting global data from non-id publics
 
Quote:

Originally Posted by Crackhead69 (Post 2754786)
It has nothing specific in the example that i want, i am just curious.

Get_loguser_index() is what I use.

Grenade Suicide example.
Spoiler
Code:
stock get_loguser_index() { new loguser[80], name[32] read_logargv(0, loguser, 79) parse_loguser(loguser, name, 31) return get_user_index(name); }

Quote:

But is there any other more modern-like way of doing this?
Code:
stock get_loguser_index() {     new log_user[MAX_RESOURCE_PATH_LENGTH + MAX_IP_LENGTH], name[MAX_PLAYERS];     read_logargv(0, log_user, charsmax(log_user));     parse_loguser(log_user, name, charsmax(name));     return get_user_index(name); }


Later is identical just more prudent. Back to getting index within a function without it. They have to do something first and it be logged. Then the stock can extract the index for use.


All times are GMT -4. The time now is 08:32.

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