Switch: Difference between revisions

From COD Modding & Mapping Wiki
Jump to navigation Jump to search
(New page: == Switch == Image:Nutshell.png Switch is often used when there are multiple limited outcomes for a single variable. The Switch function can often be replaced by lots of "If" and...)
 
No edit summary
Line 1: Line 1:
== [[Switch]] ==
== [[Switch]] ==


[[Image:Nutshell.png]] Switch is often used when there are multiple limited outcomes for a single variable.
[[Image:Nutshell.png]] Switch is often used instead of multiple if statements, as it is more efficient and doesn't require you to type as much.
 
The Switch function can often be replaced by lots of "If" and "Else If" statements, but Switch is more efficient when the variable being checked has a limited amount of outcomes.


''How to use "Switch".''
''How to use "Switch".''
Line 10: Line 8:


Here is an example of how it is used. This is taken from the menu scripts, which handles the ingame menus, I have cut the code and also added the commented lines myself.
Here is an example of how it is used. This is taken from the menu scripts, which handles the ingame menus, I have cut the code and also added the commented lines myself.
 
<pre>
  self waittill("menuresponse", menu, response);
  self waittill("menuresponse", menu, response);


  switch(response)
  switch(response)
  {
  {
  case "changeweapon":
  case "changeweapon":
   self closeMenu();
   self closeMenu();
Line 22: Line 21:
   else if(self.pers["team"] == "axis")
   else if(self.pers["team"] == "axis")
       self openMenu(game["menu_weapon_axis"]);
       self openMenu(game["menu_weapon_axis"]);
  break;   
break;   
 


  case "changeteam":
  case "changeteam":
Line 28: Line 28:
   self closeInGameMenu();
   self closeInGameMenu();
   self openMenu(game["menu_team"]);
   self openMenu(game["menu_team"]);
  break;
break;
 


  case "muteplayer":
  case "muteplayer":
Line 35: Line 36:
   self openMenu(game["menu_muteplayer"]);
   self openMenu(game["menu_muteplayer"]);
   break;
   break;


  case "callvote":
  case "callvote":
Line 42: Line 44:
   break;
   break;
    
    
  //default:
 
   // Add default here
  default:
   // break;    
   //add default action here
   break;
  }
  }
 
</pre>
The first part of the code is where the variable is defined. The game waits until a menu has been activated. The variables recieved are "menu" (the name of the menu activated) and "response" (what option was chosen from the menu). "Response" is the key variable for the 'Switch'.
The first part of the code is where the variable is defined. The game waits until a menu has been activated. The variables recieved are "menu" (the name of the menu activated) and "response" (what option was chosen from the menu). "Response" is the key variable for the 'Switch'.


After the variables have been defined, the Switch function is called. It then checks every "Case" (case is the term used for the possible outcomes of the variable) to find a match, if no match is found, the "Default" case is used. (If you do not have a Default case, the script will continue passed the 'Switch' function).
After the variables have been defined, the Switch function is called. It then checks every "Case" (case is the term used for the possible outcomes of the variable) to find a match, if no match is found, the "Default" case is used. (if you do not have a default case, the script will crash - you can just add an empty one.)


If a match is found, then the function will do ALL the events from that point onwards, which is why you MUST add "break;" at the end of every case, if the break is not existent, then all other case functions will run also.
If a match is found, then the function will do ALL the events from that point onwards, which is why you MUST add "break;" at the end of every case, if the break is not existent, then all other case functions will run also.
Line 60: Line 63:


Once the match is found, everything after the case will happen:
Once the match is found, everything after the case will happen:
* self closeMenu(); (Current menu closes)
* self closeMenu(); //current menu closes
* self closeInGameMenu(); (Close any other ingame menus)
* self closeInGameMenu(); //close any other ingame menus
* self openMenu(game["menu_team"]); (Will open the team menu).
* self openMenu(game["menu_team"]); //will open the team menu
* break; (The rest of the switch statement is ended and the code continues.
* break; //the rest of the switch statement is ended and the code continues
 
You can also make the script perform the same actions in multiple cases. For example:
<pre>
self waittill("menuresponse", menu, response);
 
switch(response)
{
 
case "changeweapon":
case "changeteam":
case "muteplayer":
case "callvote":
  function();
  break;
 
 
default:
  //add default action here
  break;
}
</pre>
This means that if the response variable is: 'changeweapon', 'changeteam', 'muteplayer' or 'callvote', function(); will be called.

Revision as of 02:39, 24 October 2008

Switch

Switch is often used instead of multiple if statements, as it is more efficient and doesn't require you to type as much.

How to use "Switch".

Switch can be quite hard to understand at first glance, but after using it, it will become easier.

Here is an example of how it is used. This is taken from the menu scripts, which handles the ingame menus, I have cut the code and also added the commented lines myself.

 self waittill("menuresponse", menu, response);

 switch(response)
 {

 case "changeweapon":
   self closeMenu();
   self closeInGameMenu();
   if(self.pers["team"] == "allies")
      self openMenu(game["menu_weapon_allies"]);
   else if(self.pers["team"] == "axis")
      self openMenu(game["menu_weapon_axis"]);
 break;   


 case "changeteam":
   self closeMenu();
   self closeInGameMenu();
   self openMenu(game["menu_team"]);
 break;


 case "muteplayer":
   self closeMenu();
   self closeInGameMenu();
   self openMenu(game["menu_muteplayer"]);
   break;


 case "callvote":
   self closeMenu();
   self closeInGameMenu();
   self openMenu(game["menu_callvote"]);
   break;
   

 default:
   //add default action here
   break;
 }

The first part of the code is where the variable is defined. The game waits until a menu has been activated. The variables recieved are "menu" (the name of the menu activated) and "response" (what option was chosen from the menu). "Response" is the key variable for the 'Switch'.

After the variables have been defined, the Switch function is called. It then checks every "Case" (case is the term used for the possible outcomes of the variable) to find a match, if no match is found, the "Default" case is used. (if you do not have a default case, the script will crash - you can just add an empty one.)

If a match is found, then the function will do ALL the events from that point onwards, which is why you MUST add "break;" at the end of every case, if the break is not existent, then all other case functions will run also.

To use the above example, I shall input my own values to show how the example works...

When I open the menu ingame, I choose the option "changeteam". The code kicks in and the variable "response" becomes equal to "changeteam". The switch statement will now look at every case for a positive match.

Once the match is found, everything after the case will happen:

  • self closeMenu(); //current menu closes
  • self closeInGameMenu(); //close any other ingame menus
  • self openMenu(game["menu_team"]); //will open the team menu
  • break; //the rest of the switch statement is ended and the code continues

You can also make the script perform the same actions in multiple cases. For example:

 self waittill("menuresponse", menu, response);

 switch(response)
 {

 case "changeweapon":
 case "changeteam":
 case "muteplayer":
 case "callvote":
   function();
   break;
   

 default:
   //add default action here
   break;
 }

This means that if the response variable is: 'changeweapon', 'changeteam', 'muteplayer' or 'callvote', function(); will be called.