switch do – Talk

From Bohemia Interactive Community
Revision as of 13:41, 14 December 2014 by SilentSpike (talk | contribs) (Response)
Jump to navigation Jump to search

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 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 like this (examples for everything I just wrote): _myVar = "a"; _myString = "foobar"; _myNumber = 0; _myArray = []; missionNamespace setVariable ["switch_case_parameters", [_myString, _myNumber, _myArray]]; //hacky way of creating input 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; }; switch (_myVar) do { case "a": firstCase; //using function as code block case "b": { }; default { }; }; - 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. --SilentSpike (talk) 12:41, 14 December 2014 (CET)