Graal Forums  

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

Closed Thread
 
Thread Tools Search this Thread Display Modes
  #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..
  #2  
Old 08-16-2007, 03:27 AM
Twinny Twinny is offline
My empire of dirt
Twinny's Avatar
Join Date: Mar 2006
Location: Australia
Posts: 2,422
Twinny is just really niceTwinny is just really nice
Send a message via AIM to Twinny
Nice work.
  #3  
Old 08-16-2007, 12:15 PM
Chompy Chompy is offline
¯\(º_o)/¯
Chompy's Avatar
Join Date: Sep 2006
Location: Norway
Posts: 2,815
Chompy is just really niceChompy is just really niceChompy is just really nice
Send a message via MSN to Chompy
Very nice Zero!
_____________________________
Oh, and for arrays:

This code will not work:

PHP Code:
function onCreated() {
  
temp.= {01};
  
  switch(
temp.a) {
    case {
01}:
      echo(
"foo");
    break;
  }

But, this code will work
PHP Code:
function onCreated() {
  
temp.= {01};
  
  switch(@ 
temp.a) {
    case @{
01}:
      echo(
"foo");
    break;
  }

__________________
  #4  
Old 08-16-2007, 08:27 PM
Inverness Inverness is offline
Incubator
Inverness's Avatar
Join Date: Aug 2004
Location: Houston, Texas
Posts: 3,613
Inverness is a jewel in the roughInverness is a jewel in the rough
Quote:
Originally Posted by Chompy View Post
PHP Code:
function onCreated() {
  
temp.= {01};
  
  switch(@ 
temp.a) {
    case @{
01}:
      echo(
"foo");
    break;
  }

Its converting to string: "0,1"
__________________
  #5  
Old 08-16-2007, 09:04 PM
Chompy Chompy is offline
¯\(º_o)/¯
Chompy's Avatar
Join Date: Sep 2006
Location: Norway
Posts: 2,815
Chompy is just really niceChompy is just really niceChompy is just really nice
Send a message via MSN to Chompy
Quote:
Originally Posted by Inverness View Post
Its converting to string: "0,1"
:o
__________________
  #6  
Old 08-16-2007, 10:29 PM
Crow Crow is offline
ǝɔɐɹq ʎןɹnɔ
Crow's Avatar
Join Date: Dec 2006
Location: Germany
Posts: 5,153
Crow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond repute
Ya, isnt @ converting arrays to strings?
__________________
  #7  
Old 08-17-2007, 01:08 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
@ is only associated with strings yes so it converts any array into a string format (like you would see if it was saved in a text file).

Did you try like:

PHP Code:
function onCreated() {
  
temp.= {01};
  
temp.= {01};
  
  switch(
temp.a) {
    case 
temp.b:
      echo(
"foo");
    break;
  }

Because you can only use {} for arrays in certain cases (like assignments and in "in" [some other things too]). I know you can't do like:

PHP Code:
if (array == {"a""b""c"}) 
__________________
Do it with a DON!
  #8  
Old 08-21-2007, 02:36 AM
Switch Switch is offline
o.o
Switch's Avatar
Join Date: Jan 2007
Location: Philadelphia
Posts: 3,038
Switch has a spectacular aura about
Send a message via MSN to Switch
Oh sweet
I never knew I would be brought up in a thread nor be a script command
__________________
Oh squiggly line in my eye fluid. I see you lurking there on the peripheral of my vision.
But when I try to look at you, you scurry away.
Are you shy, squiggly line?
Why only when I ignore you, do you return to the center of my eye?
Oh, squiggly line, it's alright, you are forgiven.
  #9  
Old 08-21-2007, 04:23 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
Quote:
Originally Posted by Switch View Post
Oh sweet
I never knew I would be brought up in a thread nor be a script command
PHP Code:
zokemon (myvar) {
  
script "bean":
    
stuff();
  break;

__________________
Do it with a DON!
  #10  
Old 08-21-2007, 03:11 PM
Crow Crow is offline
ǝɔɐɹq ʎןɹnɔ
Crow's Avatar
Join Date: Dec 2006
Location: Germany
Posts: 5,153
Crow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond repute
Thats so good about you Switch. Though you are missing the most important function. You cannot be turned off...
__________________
  #11  
Old 08-21-2007, 08:56 PM
coreys coreys is offline
N-Pulse Assistant Manager
coreys's Avatar
Join Date: Mar 2005
Posts: 2,180
coreys has a spectacular aura about
Send a message via AIM to coreys Send a message via MSN to coreys Send a message via Yahoo to coreys
Quote:
Originally Posted by zokemon View Post
PHP Code:
zokemon (myvar) {
  
script "bean":
    
stuff();
  break;

What? 0_0
__________________

Quote:
*SlikRick: so should I even ask about your aim status?
*Xor: well if you want to
*Xor: but i am LARPING
*SlikRick: While on a computer?
*Xor: yes
*Xor: in my living room
*SlikRick: ahh
*Xor: i have a fort setup to hide from beasts
  #12  
Old 08-21-2007, 09:09 PM
Chompy Chompy is offline
¯\(º_o)/¯
Chompy's Avatar
Join Date: Sep 2006
Location: Norway
Posts: 2,815
Chompy is just really niceChompy is just really niceChompy is just really nice
Send a message via MSN to Chompy
Quote:
Originally Posted by coreys View Post
What? 0_0
It was a joke ;o
__________________
  #13  
Old 06-21-2009, 01:38 PM
xXziroXx xXziroXx is offline
Master of Puppets
xXziroXx's Avatar
Join Date: May 2004
Location: Sweden
Posts: 5,288
xXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant future
Send a message via AIM to xXziroXx Send a message via MSN to xXziroXx
Why was this never stickied?
__________________

"A delayed game is eventually good, but a rushed game is forever bad." - Shigeru Miyamoto
  #14  
Old 10-07-2010, 06:35 AM
Cubical Cubical is offline
Banned
Join Date: Feb 2007
Posts: 1,348
Cubical has a brilliant futureCubical has a brilliant futureCubical has a brilliant futureCubical has a brilliant futureCubical has a brilliant futureCubical has a brilliant futureCubical has a brilliant future
Is it possible to do something like
PHP Code:
  switch(blah){
    case 
"blah" "blah"//if either are true then proceed
      
doStuff();
      break;
    default case 
"blah"//if either are true then proceed
      
doStuff();
      break;
  } 
  #15  
Old 10-07-2010, 06:37 AM
fowlplay4 fowlplay4 is offline
team canada
fowlplay4's Avatar
Join Date: Jul 2004
Location: Canada
Posts: 5,200
fowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond repute
Quote:
Originally Posted by Cubical View Post
Is it possible to do something like
PHP Code:
  switch(blah){
    case 
"blah" "blah"//if either are true then proceed
      
doStuff();
      break;
    default case 
"blah"//if either are true then proceed
      
doStuff();
      break;
  } 
You can do that like this..

PHP Code:
  switch(blah){
    case 
"blah": case "blah2":
      
doStuff();
      break;
    case 
"blah3": default:
      
doStuff();
      break;
  } 
Drops read-ability quite a bit imo though.
__________________
Quote:
Closed Thread


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:00 PM.


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