Jump to content
Sign in to follow this  
f2k sel

Trying check getvariable in trigger condition

Recommended Posts

I can check the variable but is there a better way.

currently this works

state = obj getvariable "obj";(isnil ("state"))

I thought this would work but it gives true even when variable isn't created. I could probably just set the variable true or false in an init.sqf but I'm just curious if it can be done this way.

(isnil ("obj getvariable 'obj'"))

Thanks.

Share this post


Link to post
Share on other sites

Use the getVariable array

obj getVariable ["state",false];

It will return false (or whatever you enter after the comma) if the variable doesn't exist

object getVariable [name, defaultValue]

Parameters:

object: Object or Location

name: String - Variable name that was defined in setVariable (Case sensitive)

defaultValue: Any Value - Value to return if variable doesn't exist

Return Value:

Any Value

Returns defaultValue if the variable doesn't exist.

Returns Anything if the object is undefined.

Share this post


Link to post
Share on other sites

Thanks I'd completely forgotten about the default.

In my case it's returning any as the object doesn't exist.

So my next question is how to tell when an object doesn't exits.

!isnull obj will return true if the obj exists.

isnull obj doesn't return true when obj doesn't exist.

this should work according to wiki but it doesn't.

if (isNull obj) then {hint "doesn't exist"} 

http://community.bistudio.com/wiki/isNull

Any Ideas?

Share this post


Link to post
Share on other sites

You'd have to do an isNil check if the object has never existed. If an object has been deleted etc you can do an isNull check, not otherwise. Unless you set obj = objNull in the init.sqf or similar.

if (isNil "obj") then {
hint "i have no idea what obj is.";
};

if (isNull obj) then {
 hint "i know what obj is but it doesn't exist.";
};

Share this post


Link to post
Share on other sites

Thanks that's cleared things up a little for me.

I never quite got the isnull objnull stuff.

Cheers sel.

Share this post


Link to post
Share on other sites

There is a way to check for nil variables that were set using setVariable. The thing to note, under isNil documentation, it will also check to see if an expression returns a value or not. So if you treat your check as an expression, it will properly determine if the variable exists.

This is the proper way of checking for nil variables set using setVariable:

if (isNil {obj getVariable "Something"}) then { //var doesnt exist };

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
Sign in to follow this  

×