AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   A type of global variable (https://forums.alliedmods.net/showthread.php?t=224496)

gregws 08-25-2013 01:09

A type of global variable
 
Hi guys,

I'm wanting to check to see how many people are in the game at the start of each round. But also allow all functions to have access to this variable at any time.
Code:

stock GetRealClientCount( bool:inGameOnly = true ) {
        new clients = 0;
        for( new i = 1; i <= GetMaxClients(); i++ ) {
                if( ( ( inGameOnly ) ? IsClientInGame( i ) : IsClientConnected( i ) ) && !IsFakeClient( i ) ) {
                        clients++;
                }
        }
        return clients;
 }

I know how to call this once at round start, but I'm not sure how to allow all the following functions access to it. Eg.

Code:

roundStart()
{
pCount = GetRealClientCount();
}

anotherFunction()
{
blah blah blah
doSometingWithpCount(pCount);
}


wickedd 08-25-2013 02:29

Re: A type of global variable
 
Use get players.

PHP Code:

public new_round( )
{    
    new 
players32 ], pCount
    get_players
playerspCount"a" )
    {
        if( 
pCount 10 )
        {
            
//your code
        
}
        else if( 
pCount 10 )
        {
            
//your code
        
}
    }



gregws 08-25-2013 03:16

Re: A type of global variable
 
The issue isnt counting the players but sharing the output with all other functions

Blizzard_87 08-25-2013 03:34

Re: A type of global variable
 
Code:
stock GetPlayersCount( ) {     new iPlayers[ 32 ], iNum;     get_players( iPlayers, iNum );     return iNum; }

this will return the number of players connected anytime its called.

example

Code:
client_print( id, print_chat, "Players Connected %i", GetPlayersCount( ) );

gregws 08-25-2013 04:07

Re: A type of global variable
 
Sorry, I don't think I'm explaining myself very well.

I can call how many players are in the game.
But I'm wanting to limit the amount of times the server does the count.
So instead of in each new function scanning to see how many players are in the game I want to see how many players are in the game at the start of the round.
Then have that value in a global variable and use it in other functions.

So scanning for the amount of players isnt the issue, but reducing the amount of times that function is called is.

akcaliberg 08-25-2013 09:20

Re: A type of global variable
 
If you declare the variable as a global variable, you can access it wherever you want.

PHP Code:

new gPlayersNum;
public 
plugin_init() {
    
register_logevent("eRoundStart",2,"1=Round_Start")
}
public 
eRoundStart() {
    new 
players[32];
    
get_players(players,gPlayersNum)
}
public 
function2() {
    
client_print(0,print_chat,"There were %d players when the round started",gPlayersNum)



fysiks 08-25-2013 19:54

Re: A type of global variable
 
Quote:

Originally Posted by gregws (Post 2021138)
Sorry, I don't think I'm explaining myself very well.

I can call how many players are in the game.
But I'm wanting to limit the amount of times the server does the count.
So instead of in each new function scanning to see how many players are in the game I want to see how many players are in the game at the start of the round.
Then have that value in a global variable and use it in other functions.

So scanning for the amount of players isnt the issue, but reducing the amount of times that function is called is.

Use a global variable.

But, remember that you need to consider that race conditions can be created when doing this so you need to make sure that every function that relies on this number is executed after it is updated.

YamiKaitou 08-26-2013 10:22

Re: A type of global variable
 
Is that code for SourceMod?

President 08-27-2013 12:42

Re: A type of global variable
 
PHP Code:

public roundstart()
{
       new 
count;
       
       
// getting the count of all players in variable "count"
       
       
Func(count); // sends the count to other function
}

// stock Function(iCount) // I don't know why stocks continue being used...
Func(iCount// you can call the variable differently than that you send :) example: iCount
{
       
// here you do something with the shared variable, from roundstart




All times are GMT -4. The time now is 19:10.

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