Benargee/Sandbox – User

From Bohemia Interactive Community
Jump to navigation Jump to search
No edit summary
m (temp)
Line 1: Line 1:
{{Command|= Comments
____________________________________________________________________________________________


| arma |= Game name


=[[:category:Introduced with Arma 3 Development Branch]]=
|1.00|= Game version
<DynamicPageList>
____________________________________________________________________________________________
category = Introduced with Arma 3 Development Branch
</DynamicPageList>


==[[:category:Introduced with Arma 3 Development Branch version 1.37]]==
| 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>
<DynamicPageList>
<br>
category = Introduced with Arma 3 Development Branch version 1.37
If a case has no code block, the code of the next case will automatically be executed. This makes it possible to formulate a logical '''"or"''' for cases which otherwise would contain the exact same code. (See example 4 below)<br>
</DynamicPageList>
The [[default]] block will only be executed if no case matches, no matter at which position inside the switch it is.<br>
<br>
''switch'' returns whatever the return value of the case block is. |= Description
____________________________________________________________________________________________


==[[:category:Introduced with Arma 3 Development Branch version 1.39]]==
| switch '''do''' block |= Syntax
<DynamicPageList>
category = Introduced with Arma 3 Development Branch version 1.39
</DynamicPageList>


==[[:category:Introduced with Arma 3 Development Branch version 1.41]]==
|p1= switch: [[Switch Type]] |= Parameter 1
<DynamicPageList>
category = Introduced with Arma 3 Development Branch version 1.41
</DynamicPageList>


==[[:category:Introduced with Arma 3 Development Branch version 1.43]]==
|p2= block: [[Code]] |= Parameter 2
<DynamicPageList>
category = Introduced with Arma 3 Development Branch version 1.43
</DynamicPageList>


==[[:category:Introduced with Arma 3 Development Branch version 1.45]]==
| [[Anything]] |= Return value
<DynamicPageList>
____________________________________________________________________________________________
category = Introduced with Arma 3 Development Branch version 1.45
 
</DynamicPageList>
|x1= <code>[[switch]] (_a) [[do]] { [[case]] 1 : { /*...code...*/ }; [[case]] 2 : { /*...code...*/ }; [[default]] { /*...code...*/ }; };</code>
<br>
<code>[[switch]] (_condition) [[do]] {
    [[case]] 1: { [[hint]] "1" };
    [[case]] 2: { [[hint]] "2" };
    [[default]] { [[hint]] "default" };
};</code>
<br>
<code>[[switch]] (_condition) [[do]] {
    [[case]] "string1";
    [[case]] "string2": { [[hint]] "string1 or string2" };
    [[case]] "string3";
    [[case]] "string4": { [[hint]] "string3 or string4" };
    [[default]] { [[hint]] "default" };
};</code> |= Example 1
 
|x2= <code>_color = [[switch]] ([[side]] [[player]]) [[do]] {
    [[case]] [[west]]: { "ColorGreen" };
    [[case]] [[east]]: { "ColorRed" };
};</code>
 
|x3= <code>_fn_moveForward = { /*...code...*/ };
_fn_moveBackward = { /*...code...*/ };
_fn_invalidKey = { /*...code...*/ };
[[switch]] [[true]] [[do]] {
    [[case]] (_dikCode [[in]] [[actionKeys]] "MoveForward"): _fn_moveForward;
    [[case]] (_dikCode [[in]] [[actionKeys]] "MoveBackward"): _fn_moveBackward;
    [[default]] _fn_invalidKey;
};</code> |= Example 3
 
|x4= <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] };
};</code>
<pre>_var = "0"; //-> ["3", "0"]
_var = "1"; //-> ["2", "1"]
_var = "2"; //-> ["2", "2"]
_var = "3"; //-> ["3", "3"]
_var = "4"; //-> ["2", "4"]
_var = "5"; //-> ["default", "5"]</pre> |= Example 4
 
| [[Control Structures]], [[a:b]], [[case]], [[default]] |= See also
 
}}
 
<h3 style="display:none">Notes</h3>
<dl class="command_description">
<!-- Note Section BEGIN -->
 
<dd class="notedate">Posted on 07 Aug, 2008
<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>
 
 
<dd class="notedate">Posted on 11 Aug, 2008
<dt class="note">'''[[User:Dr_Eyeball|Dr_Eyeball]]'''<dd class="note">
Using [[switch]] with strings is case-sensitive, (unlike string comparisons).
 
 
<dd class="notedate">Posted on 12 Aug, 2008
<dt class="note">'''[[User:General_Barron|General Barron]]'''<dd class="note">
To be safe about the case sensitivity issue, use the [[toLower]] or [[toUpper]] command to force all strings to a certain case.
 
 
<dd class="notedate">Posted on 06 Oct, 2009
<dt class="note">'''[[User:Iva|Iva]]'''<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>
 
 
<!-- Note Section END -->
</dl>
 
[[Category:Scripting Commands|{{uc:{{PAGENAME}}}}]]
[[Category:Scripting Commands ArmA|{{uc:{{PAGENAME}}}}]]
[[Category:ArmA: Control Structures|{{uc:{{PAGENAME}}}}]]
[[Category:Command Group: Program Flow|{{uc:{{PAGENAME}}}}]]
 
<!-- CONTINUE Notes -->
<dl class="command_description">
<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>
<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"];
};
//etc
};
</code>
</dd>
</dl>
<!-- DISCONTINUE Notes -->
 
<!-- CONTINUE Notes -->
<dl class="command_description">
<dd class="notedate">Posted on December 13, 2014 - 22:41 (UTC)</dd>
<dt class="note">[[User:Commy2|Commy2]]</dt>
<dd class="note">
 
As of Arma 3 v1.36, switch returns true (BOOL) if the condition doesn't match any case and no default block is defined.<br>
<br>
switch (0) do {case (1): {"one"};}<br>
-> true
 
</dd>
</dl>
<!-- DISCONTINUE Notes -->
 
<!-- CONTINUE Notes -->
<dl class="command_description">
<dd class="notedate">Posted on December 14, 2014 - 04:18 (UTC)</dd>
<dt class="note">[[User:DreadedEntity|DreadedEntity]]</dt>
<dd class="note">
Function names can be used in place of code, as shown in Example 3.
</dd>
</dl>
<!-- DISCONTINUE Notes -->

Revision as of 22:05, 14 December 2014

-wrong parameter ("Arma") defined!-1.00
Hover & click on the images for description

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. This makes it possible to formulate a logical "or" for cases which otherwise would contain the exact same code. (See example 4 below)
The default block will only be executed if no case matches, no matter at which position inside the switch it is.

switch returns whatever the return value of the case block is.
Groups:
Uncategorised

Syntax

Syntax:
switch do block
Parameters:
switch: Switch Type
block: Code
Return Value:
Anything

Examples

Example 1:
switch (_a) do { case 1 : { /*...code...*/ }; case 2 : { /*...code...*/ }; default { /*...code...*/ }; };
switch (_condition) do { case 1: { hint "1" }; case 2: { hint "2" }; default { hint "default" }; };
switch (_condition) do { case "string1"; case "string2": { hint "string1 or string2" }; case "string3"; case "string4": { hint "string3 or string4" }; default { hint "default" }; };
Example 2:
_color = switch (side player) do { case west: { "ColorGreen" }; case east: { "ColorRed" }; };
Example 3:
_fn_moveForward = { /*...code...*/ }; _fn_moveBackward = { /*...code...*/ }; _fn_invalidKey = { /*...code...*/ }; switch true do { case (_dikCode in actionKeys "MoveForward"): _fn_moveForward; case (_dikCode in actionKeys "MoveBackward"): _fn_moveBackward; default _fn_invalidKey; };
Example 4:
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

Notes

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 11 Aug, 2008
Dr_Eyeball
Using switch with strings is case-sensitive, (unlike string comparisons).
Posted on 12 Aug, 2008
General Barron
To be safe about the case sensitivity issue, use the toLower or toUpper command to force all strings to a certain case.
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 December 13, 2014 - 22:41 (UTC)
Commy2
As of Arma 3 v1.36, switch returns true (BOOL) if the condition doesn't match any case and no default block is defined.

switch (0) do {case (1): {"one"};}
-> true
Posted on December 14, 2014 - 04:18 (UTC)
DreadedEntity
Function names can be used in place of code, as shown in Example 3.