switch: Difference between revisions
Jump to navigation
Jump to search
Lou Montana (talk | contribs) m (Text replacement - "<!-- Note Section [A-Z]+ --> " to "") |
Lou Montana (talk | contribs) (Some wiki formatting + merge from switch do) |
||
Line 1: | Line 1: | ||
{{RV|type=command | {{RV|type=command | ||
| arma1 | |game1= arma1 | ||
|version1= 1.00 | |||
|game2= arma2 | |||
|version2= 1.00 | |||
|game3= arma2oa | |||
|version3= 1.50 | |||
|game4= tkoh | |||
|version4= 1.00 | |||
|game5= tkoh | |||
|version5= 1.00 | |||
|game6= arma3 | |||
|version6= 0.50 | |||
|gr1= Program Flow | |gr1= Program Flow | ||
| | |descr= Checks if the given parameter matches any [[case]]. If so, the code block of that case will be executed. After that the switch ends so no further cases will be checked.<br> | ||
If a case has no code block the code of the next case will automatically be executed, making it possible to formulate a logical "or" for cases which would otherwise contain the exact same code (see [[#Example 4|example 4]]).<br> | |||
The [[default]] block will be executed '''only''' if no case matches, no matter its position inside the code block. It is not a [[case]], and will '''never''' be entered by fallthrough. | |||
| | {{Feature|Informative| | ||
* [[String]] comparison is case-sensitive. Use [[toUpper]], [[toLower]], [[toLowerANSI]] or [[toUpperANSI]] to force all [[String|strings]] to the same case. | |||
* if no [[default]] block is provided and no [[case]] is matched, the switch block returns the default value [[true]], otherwise it returns what the valid case block returns. | |||
}} | |||
| [[switch]] | |s1= [[switch]] value | ||
|p1= | |p1= value: [[Anything]] | ||
| [[Switch Type]] | |r1= [[Switch Type]] | ||
|x1= <code>[[ | |x1= <code>[[switch]] (_a) [[do]] { [[case]] 1 : { /*...code...*/ }; [[case]] 2 : { /*...code...*/ }; [[default]] { /*...code...*/ }; };</code> | ||
|x2= <code>[[switch]] (_condition) [[do]] { | |x2= <code>[[switch]] (_condition) [[do]] | ||
{ | |||
[[case]] 1: { [[hint]] "1" }; | |||
[[case]] 2: { [[hint]] "2" }; | |||
[[default]] { [[hint]] "default" }; | |||
};</code> | };</code> | ||
|x3= | |x3= <code>[[switch]] (_condition) [[do]] | ||
<code>[[switch]] (_condition) [[do]] { | { | ||
[[case]] "string1"; | |||
[[case]] "string2": { [[hint]] "string1 or string2" }; | |||
[[case]] "string3"; | |||
[[case]] "string4": { [[hint]] "string3 or string4" }; | |||
[[default]] { [[hint]] "default" }; | |||
};</code> | }; | ||
</code> | |||
|x4= <code>[[private]] _color = [[switch]] ([[side]] [[player]]) [[do]] | |||
{ | |||
[[case]] [[west]]: { "ColorGreen" }; | |||
[[case]] [[east]]: { "ColorRed" }; | |||
}; | |||
</code> | |||
|x5= <code>[[private]] _fn_moveForward = { {{codecomment|/*...code...*/}} }; | |||
[[private]] _fn_moveBackward = { {{codecomment|/*...code...*/}} }; | |||
[[private]] _fn_invalidKey = { {{codecomment|/*...code...*/}} }; | |||
[[switch]] [[true]] [[do]] | |||
{ | |||
[[case]] (_dikCode [[in]] [[actionKeys]] "MoveForward"): _fn_moveForward; | |||
[[case]] (_dikCode [[in]] [[actionKeys]] "MoveBackward"): _fn_moveBackward; | |||
[[default]] _fn_invalidKey; | |||
}; | |||
</code> | |||
|x6= <code>[[switch]] _var [[do]] | |||
{ | |||
[[case]] "0"; | |||
[[default]] { [[hint]] [[str]] ["default", _var] }; | |||
[[case]] "3": { [[hint]] [[str]] ["3", _var] }; | |||
[[case]] "1"; | |||
[[case]] "4"; | |||
[[case]] "2": { [[hint]] [[str]] ["2", _var] }; | |||
}; | |||
_var = "0"; {{cc|-> ["3", "0"]}} | |||
_var = "1"; {{cc|-> ["2", "1"]}} | |||
_var = "2"; {{cc|-> ["2", "2"]}} | |||
_var = "3"; {{cc|-> ["3", "3"]}} | |||
_var = "4"; {{cc|-> ["2", "4"]}} | |||
_var = "5"; {{cc|-> ["default", "5"]}} | |||
</code> | |||
|seealso= [[Control Structures]] | |seealso= [[Control Structures]], [[a:b]], [[case]], [[default]] | ||
}} | }} | ||
<dl class="command_description"> | <dl class="command_description"> | ||
<dt><dt> | |||
<dd class="notedate">Posted on 07 Aug, 2008</dd> | |||
<dt class="note">[[User:ColonelSandersLite|ColonelSandersLite]]<dd class="note"><br> | |||
Be careful of the parenthesis around the variable you're switching on. If you accidentally use braces instead (ex: switch {_myVar} do{...), it won't error, but will always return default.<br> | |||
<dt><dt> | |||
<dd class="notedate">Posted on 06 Oct, 2009</dd> | |||
<dt class="note">[[User:Iva|Iva]]</dt> | |||
<dd class="note"> | |||
It's possible to use [[Boolean]] value as a switch and [[Code]] as case. One thing to take special care in such case is that code must be in parentheses. Example: | |||
<code>[[switch]] ([[true]]) [[do]] | |||
{ | |||
[[case]] (_boolVar): {someCode}; | |||
[[case]] (unit1 [[distance]] unit2 > 5): {someCode}; | |||
}; | |||
</code> | |||
</dd> | |||
<dt></dt> | |||
<dd class="notedate">Posted on November 6, 2014 - 16:33 (UTC)</dd> | |||
<dt class="note">[[User:Eggbeast|Eggbeast]]</dt> | |||
<dd class="note"> | |||
BEWARE: | |||
<br>Sometimes, and I'm unsure why, numbers are treated differently (A2OA 1.63) with quote-wraps that worked | |||
<br>in earlier code now not working unless quote-wraps are removed, with otherwise identical code. | |||
<br> | |||
<code>_number1 [[=]] ([[floor]] [[random]] 10) | |||
[[switch]] (_number1) [[do]] | |||
{ | |||
[[case]] "0": | |||
{ | |||
_vehicle [[setObjectTexture]] [0,"\mymodpath\textures\num1.paa"]; | |||
}; | |||
//etc | |||
}; | |||
</code> | |||
<br> | |||
this one below works, and the one above stopped working with 1.63<br> | |||
<code>[[switch]] (_number1) [[do]] | |||
{ | |||
[[case]] 0: | |||
{ | |||
_vehicle [[setObjectTexture]] [0,"\mymodpath\textures\num1.paa"]; | |||
}; | |||
{{cc|etc}} | |||
}; | |||
</code> | |||
</dd> | |||
<dt></dt> | |||
<dd class="notedate">Posted on Nobember 1, 2020 - 10:05 (UTC)</dd> | |||
<dt class="note">[[User:Freddo3000|Freddo3000]]</dt> | |||
<dd class="note"> | |||
You are able to call code within the switch statements, such as: | |||
<code>_myFunc [[=]] { | |||
[[systemChat]] "0"; | |||
[[case]] 1: {[[systemChat]] "1"}; | |||
[[systemChat]] "2"; | |||
}; | |||
_myParameter [[=]] 1; | |||
[[switch]] (_myParameter) [[do]] | |||
{ | |||
_myParameter [[call]] _myFunc; | |||
[[case]] 1: {[[systemChat]] "4"}; | |||
[[systemChat]] "3"; | |||
[[default]] {[[systemChat]] "-1";}; | |||
};</code> | |||
However the order in which it is executed is difficult to make sense of, see the code comment. | |||
This is used by [[BIS_fnc_missionConversations]] and [[BIS_fnc_missionTasks]] | |||
</dd> | |||
</dl> |
Revision as of 16:49, 6 June 2021
Description
- Description:
- Checks if the given parameter matches any case. If so, the code block of that case will be executed. After that the switch ends so no further cases will be checked.
If a case has no code block the code of the next case will automatically be executed, making it possible to formulate a logical "or" for cases which would otherwise contain the exact same code (see example 4).
The default block will be executed only if no case matches, no matter its position inside the code block. It is not a case, and will never be entered by fallthrough. - Groups:
- Program Flow
Syntax
- Syntax:
- switch value
- Parameters:
- value: Anything
- Return Value:
- Switch Type
Examples
- Example 1:
switch (_a) do { case 1 : { /*...code...*/ }; case 2 : { /*...code...*/ }; default { /*...code...*/ }; };
- Example 2:
switch (_condition) do { case 1: { hint "1" }; case 2: { hint "2" }; default { hint "default" }; };
- Example 3:
switch (_condition) do { case "string1"; case "string2": { hint "string1 or string2" }; case "string3"; case "string4": { hint "string3 or string4" }; default { hint "default" }; };
- Example 4:
private _color = switch (side player) do { case west: { "ColorGreen" }; case east: { "ColorRed" }; };
- Example 5:
private _fn_moveForward = { /*...code...*/ }; private _fn_moveBackward = { /*...code...*/ }; private _fn_invalidKey = { /*...code...*/ }; switch true do { case (_dikCode in actionKeys "MoveForward"): _fn_moveForward; case (_dikCode in actionKeys "MoveBackward"): _fn_moveBackward; default _fn_invalidKey; };
- Example 6:
switch _var do { case "0"; default { hint str ["default", _var] }; case "3": { hint str ["3", _var] }; case "1"; case "4"; case "2": { hint str ["2", _var] }; }; _var = "0"; // -> ["3", "0"] _var = "1"; // -> ["2", "1"] _var = "2"; // -> ["2", "2"] _var = "3"; // -> ["3", "3"] _var = "4"; // -> ["2", "4"] _var = "5"; // -> ["default", "5"]
Additional Information
- See also:
- Control Structuresa:bcasedefault
Notes
-
Report bugs on the Feedback Tracker and/or discuss them on the Arma Discord or on the Forums.
Only post proven facts here! Add Note
- Posted on 07 Aug, 2008
- ColonelSandersLite
Be careful of the parenthesis around the variable you're switching on. If you accidentally use braces instead (ex: switch {_myVar} do{...), it won't error, but will always return default.
- Posted on 06 Oct, 2009
- Iva
-
It's possible to use Boolean value as a switch and Code as case. One thing to take special care in such case is that code must be in parentheses. Example:
switch (true) do { case (_boolVar): {someCode}; case (unit1 distance unit2 > 5): {someCode}; };
- Posted on November 6, 2014 - 16:33 (UTC)
- Eggbeast
-
BEWARE:
Sometimes, and I'm unsure why, numbers are treated differently (A2OA 1.63) with quote-wraps that worked
in earlier code now not working unless quote-wraps are removed, with otherwise identical code.
_number1 = (floor random 10) switch (_number1) do { case "0": { _vehicle setObjectTexture [0,"\mymodpath\textures\num1.paa"]; }; //etc };
this one below works, and the one above stopped working with 1.63
switch (_number1) do { case 0: { _vehicle setObjectTexture [0,"\mymodpath\textures\num1.paa"]; }; // etc };
- Posted on Nobember 1, 2020 - 10:05 (UTC)
- Freddo3000
-
You are able to call code within the switch statements, such as:
_myFunc = { systemChat "0"; case 1: {systemChat "1"}; systemChat "2"; }; _myParameter = 1; switch (_myParameter) do { _myParameter call _myFunc; case 1: {systemChat "4"}; systemChat "3"; default {systemChat "-1";}; };
However the order in which it is executed is difficult to make sense of, see the code comment. This is used by BIS_fnc_missionConversations and BIS_fnc_missionTasks
Categories:
- Scripting Commands
- Introduced with Armed Assault version 1.00
- ArmA: Armed Assault: New Scripting Commands
- ArmA: Armed Assault: Scripting Commands
- Arma 2: Scripting Commands
- Arma 2: Operation Arrowhead: Scripting Commands
- Take On Helicopters: Scripting Commands
- Arma 3: Scripting Commands
- Command Group: Program Flow