Raised This Month: $99 Target: $400
 24% 

[TUT/INFO] Condition operator aka ? and :


Post New Thread Reply   
 
Thread Tools Display Modes
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
mut2nt
Senior Member
Join Date: Oct 2006
Location: in HELL!
Old 10-25-2008 , 13:42   Re: [TUT/INFO] ? and : function
Reply With Quote #2

nice ..i have learn something new , 10x anakin :*!
+karma
__________________
Anyone who helps me +KARMA
mut2nt is offline
Send a message via Yahoo to mut2nt Send a message via Skype™ to mut2nt
danielkza
AMX Mod X Plugin Approver
Join Date: May 2007
Location: São Paulo - Brasil
Old 10-25-2008 , 13:44   Re: [TUT/INFO] ? and : function
Reply With Quote #3

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.
__________________

Community / No support through PM
danielkza is offline
AntiBots
Veteran Member
Join Date: May 2008
Location: La Plata, Bs As, Argenti
Old 10-25-2008 , 14:19   Re: [TUT/INFO] ? and : function
Reply With Quote #4

Exelent tuto +Karma
__________________
AntiBots is offline
Send a message via ICQ to AntiBots Send a message via MSN to AntiBots Send a message via Skype™ to AntiBots
anakin_cstrike
Veteran Member
Join Date: Nov 2007
Location: Romania
Old 10-25-2008 , 14:24   Re: [TUT/INFO] ? and : function
Reply With Quote #5

You can put only 2 lines in this function: one at first expresion at one at last expresion....otherwise you'll get an error.
__________________

anakin_cstrike is offline
AntiBots
Veteran Member
Join Date: May 2008
Location: La Plata, Bs As, Argenti
Old 10-25-2008 , 14:26   Re: [TUT/INFO] ? and : function
Reply With Quote #6

I can use a task like this or how i cant?
PHP Code:
set_task(2.0"elejirotorsplayers"id == idplr idplr2 idplr 
__________________
AntiBots is offline
Send a message via ICQ to AntiBots Send a message via MSN to AntiBots Send a message via Skype™ to AntiBots
anakin_cstrike
Veteran Member
Join Date: Nov 2007
Location: Romania
Old 10-25-2008 , 14:37   Re: [TUT/INFO] ? and : function
Reply With Quote #7

I don't understand what do you want to do...
__________________

anakin_cstrike is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 10-25-2008 , 14:58   Re: [TUT/INFO] ? and : function
Reply With Quote #8

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");
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
anakin_cstrike
Veteran Member
Join Date: Nov 2007
Location: Romania
Old 10-25-2008 , 15:11   Re: [TUT/INFO] ? and : function
Reply With Quote #9

Yea...you can use it several times on one line..is codeless
__________________

anakin_cstrike is offline
atomen
Veteran Member
Join Date: Oct 2006
Location: Stockholm, Sweden
Old 10-25-2008 , 17:29   Re: [TUT/INFO] ? and : function
Reply With Quote #10

aka "condition operator"...
__________________
atomen is offline
Send a message via MSN to atomen
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 01:23.


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