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

Solved Getting global data from non-id publics


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Crackhead69
Member
Join Date: Feb 2021
Old 08-08-2021 , 15:24   Getting global data from non-id publics
Reply With Quote #1

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?

Last edited by Crackhead69; 08-09-2021 at 13:40.
Crackhead69 is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 08-08-2021 , 16:01   Re: Getting global data from non-id publics
Reply With Quote #2

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")
        }
    } 

__________________

Last edited by Bugsy; 08-08-2021 at 16:03.
Bugsy is offline
Crackhead69
Member
Join Date: Feb 2021
Old 08-09-2021 , 04:02   Re: Getting global data from non-id publics
Reply With Quote #3

I see, but won't that trigger the client print to everybody if just One player has the value?
Crackhead69 is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 08-09-2021 , 05:19   Re: Getting global data from non-id publics
Reply With Quote #4

Quote:
Originally Posted by Crackhead69 View Post
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")
       }

__________________

Last edited by HamletEagle; 08-09-2021 at 05:23.
HamletEagle is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 08-09-2021 , 06:43   Re: Getting global data from non-id publics
Reply With Quote #5

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))
     {
  
      }

__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !


Last edited by Natsheh; 08-09-2021 at 06:44.
Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 08-09-2021 , 07:07   Re: Getting global data from non-id publics
Reply With Quote #6

Quote:
Originally Posted by Natsheh View Post
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.
__________________
HamletEagle is offline
Crackhead69
Member
Join Date: Feb 2021
Old 08-09-2021 , 09:46   Re: Getting global data from non-id publics
Reply With Quote #7

Aah, i get it!
Thank you lots.
Crackhead69 is offline
DJEarthQuake
Veteran Member
Join Date: Jan 2014
Location: Astral planes
Old 08-09-2021 , 22:55   Re: Getting global data from non-id publics
Reply With Quote #8

Quote:
Originally Posted by Crackhead69 View Post
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.
__________________
DJEarthQuake is offline
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 05:21.


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