Raised This Month: $ Target: $400
 0% 

A type of global variable


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
gregws
Member
Join Date: Aug 2013
Old 08-25-2013 , 01:09   A type of global variable
Reply With Quote #1

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);
}
gregws is offline
wickedd
Veteran Member
Join Date: Nov 2009
Old 08-25-2013 , 02:29   Re: A type of global variable
Reply With Quote #2

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

__________________
Just buy the fucking game!!!!
I hate No-Steamers and lazy ass people.
wickedd is offline
gregws
Member
Join Date: Aug 2013
Old 08-25-2013 , 03:16   Re: A type of global variable
Reply With Quote #3

The issue isnt counting the players but sharing the output with all other functions

Last edited by gregws; 08-25-2013 at 03:29.
gregws is offline
Blizzard_87
Veteran Member
Join Date: Oct 2012
Old 08-25-2013 , 03:34   Re: A type of global variable
Reply With Quote #4

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( ) );
__________________
Blizzard_87 is offline
gregws
Member
Join Date: Aug 2013
Old 08-25-2013 , 04:07   Re: A type of global variable
Reply With Quote #5

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.
gregws is offline
akcaliberg
Senior Member
Join Date: Nov 2011
Location: Istanbul
Old 08-25-2013 , 09:20   Re: A type of global variable
Reply With Quote #6

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)

akcaliberg is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 08-25-2013 , 19:54   Re: A type of global variable
Reply With Quote #7

Quote:
Originally Posted by gregws View Post
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.
__________________
fysiks is offline
YamiKaitou
Has a lovely bunch of coconuts
Join Date: Apr 2006
Location: Texas
Old 08-26-2013 , 10:22   Re: A type of global variable
Reply With Quote #8

Is that code for SourceMod?
__________________
ProjectYami Laboratories

I do not browse the forums regularly anymore. If you need me for anything (asking questions or anything else), then PM me (be descriptive in your PM, message containing only a link to a thread will be ignored).
YamiKaitou is offline
President
Member
Join Date: May 2013
Old 08-27-2013 , 12:42   Re: A type of global variable
Reply With Quote #9

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


Last edited by President; 08-27-2013 at 12:44.
President 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 19:10.


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