switch do – Talk

From Bohemia Interactive Community
Jump to navigation Jump to search
(Created page with "Since functions are really just variables that hold code (in sqf), please be aware that it is entirely possible to use a function in place of a code block for a case. While th...")
 
m (deleting some horrible code)
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
Since functions are really just variables that hold code (in sqf), please be aware that it is entirely possible to use a function in place of a code block for a case. While that may be obvious to most people, it's still worth mentioning. Therefore, I'm also assuming you can use a [[compile|compiled]] string, since it gets turned into code. You should also note that variables are passed to the code during execution. Unfortunately, you may not wish to be forced to use the same variables inside your function, as it can become limiting, but there is also no way (as far as I know) to pass variables to the code. To work around this, you'll have to do a semi-hacky thing by setting the variables to a [[Namespace|namespace]] like this ('''examples for everything I just wrote'''):
 
<code>_myVar = "a";
_myString = "foobar";
_myNumber = 0;
_myArray = [];
<nowiki></nowiki>
[[missionNamespace]] [[setVariable]] ["switch_case_parameters", [_myString, _myNumber, _myArray]]; '''//hacky way of creating input'''
<nowiki></nowiki>
firstCase =
{
[[private]] ["_params","_myString"];
_params = [[missionNamespace]] [[getVariable]] ["switch_case_parameters", [[nil]]]; '''//hacky way of using "input"'''
_myString = "[";
[[for]] "_i" [[from]] 0 [[to]] (([[count]] _params) - 2) [[do]]
{
_myString = _myString + ([[str]] (_params [[select]] _i)) + ", ";
}; '''//not necessary at all but I thought it was cool'''
_myString = _myString + ([[str]] (([[count]] _params) - 1)) + "]";
[[hint]] _myString;
};
<nowiki></nowiki>
[[switch]] (_myVar) [[do]]
{
[[case]] "a": firstCase; '''//using function as code block'''
[[case]] "b":
{
};
[[default]]
{
};
};</code>

Latest revision as of 21:36, 15 December 2014