AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Code Snippets/Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=83)
-   -   [TUT/INFO] Condition operator aka ? and : (https://forums.alliedmods.net/showthread.php?t=79543)

anakin_cstrike 10-25-2008 13:36

[TUT/INFO] Condition operator aka ? and :
 
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.

mut2nt 10-25-2008 13:42

Re: [TUT/INFO] ? and : function
 
nice :D..i have learn something new , 10x anakin :*!
+karma

danielkza 10-25-2008 13:44

Re: [TUT/INFO] ? and : function
 
Don't forget you should be very careful while using this expression. It has strange precedence orders, and can lead to unexpected behavior. To avoid this, enclose it in () where you have more complex expressions.

PS: +karma, good tut.

AntiBots 10-25-2008 14:19

Re: [TUT/INFO] ? and : function
 
Exelent tuto :D +Karma

anakin_cstrike 10-25-2008 14:24

Re: [TUT/INFO] ? and : function
 
You can put only 2 lines in this function: one at first expresion at one at last expresion....otherwise you'll get an error.

AntiBots 10-25-2008 14:26

Re: [TUT/INFO] ? and : function
 
I can use a task like this or how i cant?
PHP Code:

set_task(2.0"elejirotorsplayers"id == idplr idplr2 idplr 


anakin_cstrike 10-25-2008 14:37

Re: [TUT/INFO] ? and : function
 
I don't understand what do you want to do...

Exolent[jNr] 10-25-2008 14:58

Re: [TUT/INFO] ? and : function
 
You can use it anywhere...

I've even used it in menus:
Code:
new item[32]; formatex(item, sizeof(item) - 1, "Item Name: %s%s",\     /* can use item */ ? ( /* is using item */ ? "\y" : "\r" ) : "\d",\     /* is using item */ ? "On" : "Off"     ); menu_additem(menu, item, "num");

anakin_cstrike 10-25-2008 15:11

Re: [TUT/INFO] ? and : function
 
Yea...you can use it several times on one line..is codeless:)

atomen 10-25-2008 17:29

Re: [TUT/INFO] ? and : function
 
aka "condition operator"...


All times are GMT -4. The time now is 12:30.

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