AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   switch() & case (https://forums.alliedmods.net/showthread.php?t=26018)

capndurk 03-25-2006 16:47

switch() & case
 
I have two questions:

1) Can I switch a string? For example:

Code:
switch(string) {     case "whatever": //code     case "whatever 2": //code }

and

2) Is it possible to combine cases with an & or |? For example:

Code:
case "whatever": // same code as "whatever 2" case "whatever 2": // same code as "whatever" case "whatever" | "whatever 2": // code

Any help is greatly appreciated.

Werewolf 03-25-2006 16:54

Should work. Example:
Code:
if(case 1: || case 2:) { //function }
I don't know if it works but it is worth to try... If not someone says "It won't work"

capndurk 03-25-2006 17:00

Quote:

Originally Posted by Werewolf
Should work. Example:
Code:
if(case 1: || case 2:) { //function }
I don't know if it works but it is worth to try... If not someone says "It won't work"

I know, but i just wanted to avoid doing if statements because they can get kind of ugly with 4 difference cases, and then a default case.

Twilight Suzuka 03-25-2006 17:31

Or, you could just replicate the code twice.

capndurk 03-25-2006 17:39

Quote:

Originally Posted by Twilight Suzuka
Or, you could just replicate the code twice.

Or four times.. :(


Thanks anyway guys.

v3x 03-25-2006 20:06

Or you can check a single character in a string like I sometimes do:
Code:
switch(str[0]) {   case '+': start_cmd(id);   case '-': stop_cmd(id); }
Here's an example of how I use it:

Code:
#include <amxmodx> public plugin_init() {   register_clcmd("+gaben" , "cmd_gaben");   register_clcmd("-gaben" , "cmd_gaben");   // noticed they are both hooked to the same function } public cmd_gaben(id) {   new arg[8];   read_argv(0 , arg , 7);   switch(str[0])   {     case '+': start_cmd(id);     case '-': stop_cmd(id);   }   return PLUGIN_HANDLED; } public start_cmd(id) {   // } public stop_cmd(id) {   // }

capndurk 03-25-2006 21:24

Thanks a lot v3x, I'll try that.

Xanimos 03-25-2006 22:28

to have multiple cases use this
Code:
switch(Var) {     case 1 , 2 , 3 , 4:  DoThis();     case 5 , 6, 7, 8: DoThat(); }

capndurk 03-26-2006 02:56

Thanks for the information guys.

Greenberet 03-26-2006 03:12

here is an example how to use a case for a range:

Code:
switch( bla ) {         case 0 .. 20: client_print( 0, print_chat, "Bla is  between 0 and 20" );         case 21 .. 50: client_print( 0, print_chat, "Bla is  between 21 and 50" ); }


All times are GMT -4. The time now is 16:43.

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