OFPEC Forum
Editors Depot - Mission Editing and Scripting => ArmA - Editing/Scripting General => Topic started by: alef on 21 Apr 2009, 20:40:15
-
I see a lot of:
_v = _u getVariable "var"; if (format["%1", _v == "<null>") then { _v = 0; };
around. What are your opinions about:
#define GET_VAR_DEF( U, V, D) if (isNil {U getVariable #V}) then {D} else {U getVariable #V}
_v = GET_VAR_DEF( _u, var, 0 );
?
Thanks,
alef
-
I'd rather write a wrapper for create{Unit,Vehicle,Whatever} or run some init code (for editor placed stuff) to make sure the variable is set instead of checking it a thousand times during runtime (
where the check isn't necessary the last 999 times edit: I assumed that the variable is set after the first check which is not the case here).
-
In _u getVariable "var", you get
- Nothing (http://community.bistudio.com/wiki/Nothing), when "var" is not set
- Anything (http://community.bistudio.com/wiki/Nothing), when _u doesn't exists
isNil { _u getVariable "var" } catches both.
-
Interesting. So when I get back the default value I know something is wrong. But what would be my next step then? Create the object? No, it might exist.
Read/set the variable? Uhm, no, the object might not exist. I fail to see what to do with this information. Please enlighten me.
-
I fail to see what to do with this information.
Good point.
One should add additional checks that render things more complicated.// return _def is _var is not set in _obj. nil on errors: _def can't be nil.
get_var_def = { // doesn't throw
private["_obj", "_var", "_def", "_ret"];
_obj = _this select 0; _var = _this select 1; _def = _this select 2;
if (isNil {_obj} or isNil {_def}) exitWith {nil};
if (isNull _obj) exitWith {nil};
if ("STRING" != typeName _var) exitWith {nil};
_ret = _obj getVariable _var;
if (isNil {_ret}) exitWith {_def};
_ret
};