switch do – Talk

From Bohemia Interactive Community
Jump to navigation Jump to search
(Response)
m (deleting some horrible code)
 
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>
- [[User:DreadedEntity|DreadedEntity]] ([[User talk:DreadedEntity|talk]]) 05:16, 14 December 2014 (CET)
::FYI, missionNamespace variables are just regular global variables. So to pass code you can just use global variables. (_ prefix = local variable, no _ prefix = global variables). See [[Variables]] for more info
::You might also want to update your note to simply say that function names can be used in place of code. --[[User:SilentSpike|SilentSpike]] ([[User talk:SilentSpike|talk]]) 12:41, 14 December 2014 (CET)

Latest revision as of 20:36, 15 December 2014