Jump to content
alleycat

Check if variable exists before using getvariable?

Recommended Posts

I want to check for a variable of an object that was set by setvariable. How would I check if the variable exists at all to prevent an error?

Share this post


Link to post
Share on other sites

_var = missionNameSpace getVariable ["myVar",-1];

 

if (_var != -1) then {do stuff} else {not defined, so do other stuff}

Share this post


Link to post
Share on other sites

You can also use the syntax of getvariable to set a default value. That will also prevent errors when used correct:

private _var = _someObj getvariable["variable_of_doom",objNull];
if(!isNull _var) then {
   //Do something with var
} else {
   diag_log "Error: Variable_of_doom was not set.";
};

Edit: Moerderhoschi was faster.... D'Oh!

Share this post


Link to post
Share on other sites

Those two methods are ok, but what if a possible value is -1 or null object? They are used to avoid some errors, not to really check if a variable is nil.

 

The first one is the best.

Share this post


Link to post
Share on other sites

That thing ckecks Nil:

if (isNil {_your_object getVariable "objects_variable"}) then
{
 hint "it is Nil";
}else{
 hint "it is something";
};

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

×