Home   Help Search Login Register  

Author Topic: #macro for getvariable  (Read 2214 times)

0 Members and 1 Guest are viewing this topic.

Offline alef

  • Members
  • *
#macro for getvariable
« 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

Offline Worldeater

  • Former Staff
  • ****
  • Suum cuique
Re: #macro for getvariable
« Reply #1 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).
« Last Edit: 21 Apr 2009, 21:19:29 by Worldeater »
try { return true; } finally { return false; }

Offline alef

  • Members
  • *
Re: #macro for getvariable
« Reply #2 on: 21 Apr 2009, 21:35:16 »
In _u getVariable "var", you get
isNil { _u getVariable "var" } catches both.

Offline Worldeater

  • Former Staff
  • ****
  • Suum cuique
Re: #macro for getvariable
« Reply #3 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.
« Last Edit: 21 Apr 2009, 22:20:43 by Worldeater »
try { return true; } finally { return false; }

Offline alef

  • Members
  • *
Re: #macro for getvariable
« Reply #4 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
};
« Last Edit: 22 Apr 2009, 03:08:13 by alef »