It is important to understand, what is the "scope" thing. Simply - it is a script level. Generally code in {} brackets is a sub-scope - scope of more "inner" level (in rare situations, eg when
spawn {some code}; is used, contents of {} (some code) will be completely separate scope, same as other, independent script - see below how to proceed then). Variables with "_" (local) are recognized only in scope, where was defined and in all more inner sub-scopes. There are also some other things with this concept, but enough of this for now. So, if you prepare such code:
PHP Code:
if (fog > 0.5) then {_message = "noGo"};
player sidechat _message;
Message will not appear, because _message is definded in sub-scope (so recognized only in this and more inner scopes, if some exists), and next used in main script scope.
This however will work (message will appear regardless of fog, but kind of message will be dependant of fog level):
PHP Code:
_message = "On the way!";
if (fog > 0.5) then {_message = "noGo"};
player sidechat _message;
because _message was defined in same, main scope, as that, in which _message is used at end of the code. It is also proper changed from "On the way!" to "NoGo", because changes maded after definig are recognized everywhere, where variable itself is recognized, unless
private command is used...
This will work also, message will be dispalyed, when fog will be heavy enough
PHP Code:
if (fog > 0.5) then {_message = "noGo";player sidechat _message};
and this too:
PHP Code:
_message = "noGo";
if (fog > 0.5) then {player sidechat _message};
BTW about this:
PHP Code:
start = _this select 0;
end = _this select 1;
Such code is used to introduce, here as two global variables (usually there will be local variables) of names "start" and "end" some values from entirely other scope (eg other script) into this script. _this represents an array of such values,
select 0 represents first value from _this array,
select 1 - second.
For example let's say, that you prepared "first.sqf" script:
PHP Code:
_FirstVar = 0;
_SecondVar = "teddybear";
[] execVM "second.sqf";
There are two variables defined - a number and a string. And you want to use this values in executed from here "second.sqf" script, that looks like:
PHP Code:
hint (str _FirstVar);
sleep 5;
hint _SecondVar;
But this is not working, because local variables was defined in other scope (other script). How to make this work? Two methods for example...
First: to make these variables global (delete "_"). Now, after defining, will be recognized by any other script/scope:
PHP Code:
FirstVar = 0;
SecondVar = "teddybear";
[] execVM "second.sqf";
and:
PHP Code:
hint (str FirstVar);
sleep 5;
hint SecondVar;
but if you change value of this global variables in "second.sqf", then for "first.sqf", and for any other script/scope too, their values also will be changed, so usually better way for "second.sqf" may be:
PHP Code:
_first = FirstVar;
_second = SecondVar;//now you operate on local variables with same value, as these global, so there is no risk to change global variable value for all scripts/scopes
hint (str _first);
sleep 5;
hint _second;
Second way, probably best, is to use
_this array:
"first.sqf"
PHP Code:
_FirstVar = 0;
_SecondVar = "teddybear";
[_FirstVar, _SecondVar] execVM "second.sqf";//all inside [] brackets will pass into "second.sqf" as _this array
"second.sqf"
PHP Code:
_first = _this select 0;//(means _first = _FirstVar)
_second = _this select 1;//(means _second = _SecondVar)
hint (str _first);
sleep 5;
hint _second;
Now "second.sqf" will work.