OFPEC Forum

Editors Depot - Mission Editing and Scripting => ArmA - Editing/Scripting General => Topic started by: alef on 21 Apr 2009, 20:40:15

Title: #macro for getvariable
Post by: alef on 21 Apr 2009, 20:40:15
I see a lot of:
Code: [Select]
_v = _u getVariable "var"; if (format["%1", _v == "<null>") then { _v = 0; };around. What are your opinions about:
Code: [Select]
#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
Title: Re: #macro for getvariable
Post by: Worldeater on 21 Apr 2009, 21:10:40
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).
Title: Re: #macro for getvariable
Post by: alef on 21 Apr 2009, 21:35:16
In _u getVariable "var", you get
isNil { _u getVariable "var" } catches both.
Title: Re: #macro for getvariable
Post by: Worldeater on 21 Apr 2009, 22:16:40
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.
Title: Re: #macro for getvariable
Post by: alef on 22 Apr 2009, 02:15:17
I fail to see what to do with this information.
Good point.
One should add additional checks that render things more complicated.
Code: [Select]
// 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
};