Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting > New Scripting Engine (GS2)
FAQ Members List Calendar Today's Posts

 
 
Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 08-16-2007, 02:50 AM
zokemon zokemon is offline
That one guy...
zokemon's Avatar
Join Date: Mar 2001
Location: Sonoma County, California
Posts: 2,925
zokemon is a jewel in the roughzokemon is a jewel in the rough
Send a message via ICQ to zokemon Send a message via AIM to zokemon Send a message via MSN to zokemon Send a message via Yahoo to zokemon
The Spoils of the Switch and Case Statements

This is not a question thread at all but rather a thread to make the scripting community more aware of the power of using switch in your GS2 coding.

I'm also posting this because I don't want to steal the pride of a certain someone after he updated a certain wiki's switch statement page (by updating it my self).


Using the switch to do a case, break, case, break, case, break series is often common. But what people don't know is what you can do outside of such a series. Rather then try to explain it off the bat, I'll show you an example code I made on GK Debug (can't say what its for, sorry):

PHP Code:
LINE 01: switch (this.mystatus) {
LINE 02:   case 1:
LINE 03:   case 3:
LINE 04:   case 5:
LINE 05:     fnGetItem(1"board""boards");
LINE 06:   break;
LINE 07:   case 7:
LINE 08:   case 9:
LINE 09:     fnGetItem(1"iron""iron");
LINE 10:   break;
LINE 11:   default:
LINE 12:     true;
LINE 13:   break;
LINE 14: } 
Basically here is the rule for a switch:
When ever a case is encountered, if that case proves true, all the code following that case statement will be executed (from top to bottom) ignoring all following case statements. A break can be used to "jump out" of the switch to avoid executing unwanted statements of another case.

In a sense, a switch just jumps into the point that the correct case is found and jumps out at the first break found after that (if there is one).

Let's just jump into the code by presenting certain examples:

Let's say this.mystatus is 5:
The code when then start at line 4 and execute both line 5 and then line 6. At line 6 is a break so the switch then ends at that point.

Let's say this.mystatus is 8:
The switch contains no cases for 8 so it starts at the default (line 11). Lines 12 and and 13 are then executed. At line 13 is a break so the switch then ends at that point but keep in mind since this is the last "case" (so to speak) in the switch, the final break is really only there for looks (the switch would end regardless since it is the end).

Another thing to note about default: A switch will start at default if it has found no matching cases before. Thus, if you have any cases after the break in your default, they will always be ignored. For this reason, your default should go at the end of your switch no matter what (putting it at the beginning without a break would be just stupid because you could put that code segment before the switch).

Let's say this.mystatus is 1:
Now the switch starts at line 2 and continues on with the code from there. Following our rule (which says to ignore all following case's), it only executes lines 5 and 6 because lines 3 and 4 are both case's that will be ignored (a case was already found). Just like if this.mystatus was 3 or 5, this code will do the function on line 5 and then end the switch on line 6.

I think the rest is pretty self explanatory, if you have any questions though or are confused, please feel free to ask!

Hope this helps,
- Zero

EDIT:

Here is info on the basics of a switch for those that don't know:

The format for a switch can be read from above but you may not know how to implement it. Look at this:
PHP Code:
switch (this.myvar) { 
This is getting the contents of "this.myvar" and using it to compare to all the switch's case's. The variable (this.myvar) could be a float (number) or a string (series of letters, numbers and/or characters) too! I currently haven't tested if an array works (array members work just fine such as switch (this.myvar[2])) or if an object works. I'll have to get back to you on that.

Now the case's:
The case's should just match the variable type that you used in the switch. If the switch variable is a float, you should do something like I did in the above example. As for strings, here is a good example:

PHP Code:
LINE 01: switch (this.mystring) {
LINE 02:   case "abc":
LINE 03:   case "beans":
LINE 04:   case "ghi":
LINE 05:     fnGetItem(1"board""boards");
LINE 06:   break;
LINE 07:   case "jkl":
LINE 08:   case "I'm a donkey":
LINE 09:     fnGetItem(1"iron""iron");
LINE 10:   break;
LINE 11:   default:
LINE 12:     true;
LINE 13:   break;
LINE 14: } 
Make sense?
__________________
Do it with a DON!

Last edited by zokemon; 08-16-2007 at 12:18 PM..
 


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 +2. The time now is 05:35 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.