Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   New Scripting Engine (GS2) (https://forums.graalonline.com/forums/forumdisplay.php?f=153)
-   -   Switch and case? (https://forums.graalonline.com/forums/showthread.php?t=134262269)

Astram 03-01-2011 02:12 AM

Switch and case?
 
What does this mean, and how do I use it. Its in the end of my staff boots script?
PHP Code:

function onTimeOut()

{

 if (
this.staff_boots == "on")

  {

   for (
a=0a<4a++)

    {

     if (
keydown(a))

      {

       switch(
a)

        {

          case 
"0":

           
player.y-=this.speed;

           break;

          case 
"1":

           
player.x-=this.speed;

           break;

          case 
"2":

           
player.y+=this.speed;

           break;

          case 
"3":

           
player.x+=this.speed;

           break;

        }

      }

    }

  }

 
setTimer(0.05);




WhiteDragon 03-01-2011 02:26 AM

It's just a different way to write an if-else when you're working with a single variable (a in this case).

That switch statement in your code translates to

PHP Code:

  if (== "0") { 
    
player.-= this.speed
  } else if (
== "1") {
    
player.-= this.speed
  } else if (
== "2") {
    
player.+= this.speed
  } else if (
== "3") {
    
player.+= this.speed
  } 

You can do slightly more complicated logic with switch statements, but I personally think those more complex uses of switch just makes things more confusing and bug-prone.

MrOmega 03-01-2011 07:24 AM

Try using the forum's search function, may save you some time. Search for 'Spoils of the Switch'. It helped me a lot when I first read it.

scriptless 04-29-2011 08:51 PM

For staff boots you will never need to know this.. instead use something more along the lines of:

PHP Code:

for ( i=0i<4i++ ) {
  if ( 
keydown(i) ) {
    
player.+= vecx(i) * this.speed;
    
player.+= vecy(i) * this.speed;
  }


Vec X/Y would give you -1,0,1 depending on direction. I am not completly sure how to explain it to you but I understand how it works. If im not mistaken its for finding vectors?

This greatly reduces your script length and makes it easier to modify.

WhiteDragon 04-29-2011 10:30 PM

I'm not a particularly big fan of that transformation. I find the if statement much clearer.

MrOmega 04-29-2011 11:05 PM

Nice job bringing up another old thread! :p

Anywho, vex/vecy will return the direction in which the operation points. So, let's say your xVal is 32 and you want it to be 30. So vecx( 32-30) would give you -1 since you are to move left. Up would -1 of vecy and so forth, if you aren't moving it will return 0. By graal magic it works with direction values too, 0-3. So if your face south your vecx would be 0 and vecy would be 1.

Deas_Voice 04-30-2011 12:05 AM

Hi Switch, who's Case?

gj @ bumping an old thread for no reason

WhiteDragon 04-30-2011 12:15 AM

Is providing a better solution a bad or useless thing to do?

Tigairius 05-13-2011 10:14 PM

Quote:

Originally Posted by scriptless (Post 1646442)
Vec X/Y would give you -1,0,1 depending on direction. I am not completly sure how to explain it to you but I understand how it works. If im not mistaken its for finding vectors?

Common question, posted a thread about it: http://forums.graalonline.com/forums...47#post1649547

scriptless 05-14-2011 07:20 AM

Thanks for reference Tigairius, will come in hand I'm sure. :)


All times are GMT +2. The time now is 07:25 PM.

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