YENGswitch
From OpenSimulator
(Difference between revisions)
												
			m (Add into category)  | 
			m (Line number clarification)  | 
			||
| (3 intermediate revisions by one user not shown) | |||
| Line 1: | Line 1: | ||
switch can only work in integer expressions, or strings<br>  | switch can only work in integer expressions, or strings<br>  | ||
break, default and '...' are keywords used with it.<br>  | break, default and '...' are keywords used with it.<br>  | ||
| + |  Make sure to add yoptions; to the second line of the script, usually line number 1 starting from 0.  | ||
| + | Simple string (command) example:  | ||
| − | + | <source lang="lsl">  | |
| + | switch (command)  | ||
| + | {  | ||
| + |     case "turnleft":  | ||
| + |     {  | ||
| + |         TurnLeft ();  | ||
| + |         break;  | ||
| + |     }  | ||
| + |     case "turnright":  | ||
| + |     {  | ||
| + |         TurnRight ();  | ||
| + |         break;  | ||
| + |     }  | ||
| + |     default: llOwnerSay ("Dont know how to handle " + command);  | ||
| + | }  | ||
| + | </source >  | ||
| − | + | A simple integer example  | |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
... can be used to define a range  | ... can be used to define a range  | ||
| − | + | <source lang="lsl">  | |
| − | + | switch (command)  | |
| − | + | {  | |
| − | + |     case 4:  | |
| − | + |     {  | |
| − | + |         llSay(PUBLIC_CHANNEL, "4");  | |
| − | + |         break;  | |
| − | + |     }  | |
| − | + |     case 5 ... 20:  | |
| − | + |     {  | |
| − | + |         // ... defines a range  | |
| − | + |         llSay(PUBLIC_CHANNEL, "r");  | |
| − | + |         break;  | |
| − | + |     }  | |
| − | + |     default: llOwnerSay ("Dont know how to handle " + command);  | |
| − | + | }  | |
| − | + | </source >  | |
| − | + | Other:  | |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | <source lang="lsl">  | |
| − | + | switch (command)  | |
| + | {  | ||
| + |     case 4:  | ||
| + |         llSay(PUBLIC_CHANNEL, "4");  | ||
| + |         break;  | ||
| + |     case 5 ... 20:  | ||
| + |         llSay(PUBLIC_CHANNEL, "5 to 20");  | ||
| + |         break;  | ||
| + |     case 22:  | ||
| + |     case 27:  | ||
| + |         llSay(PUBLIC_CHANNEL, "other");  | ||
| + |         break;  | ||
| + |     default: llOwnerSay ("dont know how to handle " + command);  | ||
| + | }  | ||
| + | </source >  | ||
The different case ranges can not overlap.  | The different case ranges can not overlap.  | ||
[[Category:Scripts]]  | [[Category:Scripts]]  | ||
Latest revision as of 14:02, 16 March 2021
switch can only work in integer expressions, or strings
break, default and '...' are keywords used with it.
Make sure to add yoptions; to the second line of the script, usually line number 1 starting from 0.
Simple string (command) example:
switch (command) { case "turnleft": { TurnLeft (); break; } case "turnright": { TurnRight (); break; } default: llOwnerSay ("Dont know how to handle " + command); }
A simple integer example ... can be used to define a range
switch (command) { case 4: { llSay(PUBLIC_CHANNEL, "4"); break; } case 5 ... 20: { // ... defines a range llSay(PUBLIC_CHANNEL, "r"); break; } default: llOwnerSay ("Dont know how to handle " + command); }
Other:
switch (command) { case 4: llSay(PUBLIC_CHANNEL, "4"); break; case 5 ... 20: llSay(PUBLIC_CHANNEL, "5 to 20"); break; case 22: case 27: llSay(PUBLIC_CHANNEL, "other"); break; default: llOwnerSay ("dont know how to handle " + command); }
The different case ranges can not overlap.