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 k = read_data( 1 );
g_Kills[k]++;
static kname[32];
get_user_name(k, kname, sizeof kname - 1);
// condition operator
server_print("%s: %d kill%s", kname, g_Kills[k] == 1 ? "" : "s");
// if statement
if(g_Kills[k] == 1)
server_print("%s: %d kill", kname, g_Kills[k]);
else
server_print("%s: %d kills", kname, g_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(id, name, sizeof name - 1);
// condition operator
client_print(0, print_chat,"ADMIN %s: Shows an example",get_pcvar_num(pointer) == 2 ? name : "");
// if statement
if(get_pcvar_num(pointer) == 2)
client_print(0, print_chat,"ADMIN %s: Shows an example",name);
else if(get_pcvar_num(pointer) == 1)
client_print(0, print_chat,"ADMIN : Shows an example");
// switch statement
switch(get_pcvar_num(pointer))
{
case 2: client_print(0, print_chat,"ADMIN %s: Shows an example",name);
case 1: client_print(0, print_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.
__________________