View Single Post
Author Message
anakin_cstrike
Veteran Member
Join Date: Nov 2007
Location: Romania
Old 10-25-2008 , 13:36   [TUT/INFO] Condition operator aka ? and :
Reply With Quote #1

Ok. I saw few scripters using this function so I decide to make this little tutorial.

Intro
? and : can be a very usefull function...i'll explain a bit:

Code:
condition ? expresion1 : expresion2
1. if condition is true, the function will asses expresion1
2. if condition is false, the function will asses expresion2

Example:
PHP Code:
// condition operator
is_user_alive(index) ? /* alive */ /* dead */

// if statement
if(is_user_alive(index))
      
/* alive */
else

      
/* dead */ 

How to use this function in code

1. An example for printing a kill/kills:
PHP Code:
new g_Kills[33];

public 
plugin_init() 
    
register_event("DeathMsg","hook_death","a");

public 
hook_death()
{
    new 
read_data);
    
g_Kills[k]++;
    
    static 
kname[32];
    
get_user_name(kknamesizeof kname 1);
    

      
// condition operator
    
server_print("%s: %d kill%s"knameg_Kills[k] == "" "s");

      
// if statement
    
if(g_Kills[k] == 1)
             
server_print("%s: %d kill"knameg_Kills[k]);
    else
             
server_print("%s: %d kills"knameg_Kills[k]);

If it was his first kill will print: "Player: 1 kill", otherwise "Player: 5 kills"

2. Depending on admins activity cvar:
PHP Code:
new pointer;

public 
plugin_init()
{
    
register_concmd("amx_example","example"ADMIN_BAN);
    
pointer get_cvar_pointer("amx_show_activity");
}

public 
example(id)
{
    if(!
access(id,ADMIN_BAN))
        return 
PLUGIN_HANDLED;
    
    new 
name[32];
    
get_user_name(idnamesizeof name 1);
    
    
// condition operator
    
client_print(0print_chat,"ADMIN %s: Shows an example",get_pcvar_num(pointer) == name "");
   
     
// if statement
    
if(get_pcvar_num(pointer) == 2)
              
client_print(0print_chat,"ADMIN %s: Shows an example",name);
    else if(
get_pcvar_num(pointer) == 1)
              
client_print(0print_chat,"ADMIN : Shows an example");

    
// switch statement
    
switch(get_pcvar_num(pointer))
    {
              case 
2client_print(0print_chat,"ADMIN %s: Shows an example",name);
              case 
1client_print(0print_chat,"ADMIN : Shows an example");
    }

    return 
PLUGIN_HANDLED;

"ADMIN Player: Shows an example" or "ADMIN : Shows an example"

3. I saw script posted about a week ago wich won't let a player to use 'say' command:
PHP Code:
new bool:g_Gag[33];

public 
plugin_init()
    
register_clcmd("say","hook_say");
    
public 
check(id)
{
    if(
g_Gag[id])
        return 
PLUGIN_HANDLED;
    return 
PLUGIN_CONTINUE;

This can be write more simple, like this:
PHP Code:
new bool:g_Gag[33];

public 
plugin_init()
    
register_clcmd("say","hook_say");
    
public 
hook_say(id)
        return 
g_Gag[id] ? PLUGIN_HANDLED PLUGIN_CONTINUE
This is a simplistic explenation.
__________________


Last edited by anakin_cstrike; 10-25-2008 at 18:41.
anakin_cstrike is offline