Jump to content
Sign in to follow this  
tomturner

isNull and undefined variable issue

Recommended Posts

This example in the https://community.bistudio.com/wiki/isNull produces an "undefined variable" error if the object has not been created yet.

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

So how would I test for an object named "anchor3" to see if it has been created yet or not?

example:

if (isNull anchor3) exitWith {hint "SealTeam Platform not yet deployed !";};

If I declare it in my init.sqf as:

anchor3 = objNull;

then, it is no longer Null and acts as though it exists.

Share this post


Link to post
Share on other sites

This might be kind of confusing as I'm not the best at explaining. anchor3 is technically a variable which points to an object, to check if a variable points to anything (read: exists) you would use isNil. Once the object has been created then anchor3 now points to the object, so isNull would work if the object were to be deleted as anchor3 would then point to objNull.

That's just how I process it in my mind, I doubt the engine follows the exact same logic.

Edited by SilentSpike

Share this post


Link to post
Share on other sites
if ( isNil "anchor3" || { isNull anchor3 } ) then { hint "anchor3 doesn't exist" };

Share this post


Link to post
Share on other sites
This might be kind of confusing as I'm not the best at explaining. anchor3 is technically a variable which points to an object, to check if a variable points to anything (read: exists) you would use isNil. Once the object has been created then anchor3 now points to the object, so isNull would work if the object were to be deleted as anchor3 would then point to objNull.

That's just how I process it in my mind, I doubt the engine follows the exact same logic.

Then the wiki example is wrong? If I use isNil then the object name would have to be surrounded in quotes, which would indeed make it a variable, albeit, a str variable and not an object. So how does one test if an object has been created yet? btw, using (alive obj) also produces an undefined variable error if the object had not been created yet.

Share this post


Link to post
Share on other sites
This example in the https://community.bistudio.com/wiki/isNull produces an "undefined variable" error if the object has not been created yet.

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

So how would I test for an object named "anchor3" to see if it has been created yet or not?

example:

if (isNull anchor3) exitWith {hint "SealTeam Platform not yet deployed !";};

If I declare it in my init.sqf as:

anchor3 = objNull;

then, it is no longer Null and acts as though it exists.

You are on the right track, just a bit misinformed. It is unrelated whether or not your object has been created yet. I has entirely to do with the Variable itself.

You are getting the undefined variable error, because, simply put, 'anchor3' is undefined. What does this mean?

THE DIFFERENCE BETWEEN NIL AND NULL VALUES

NULL - a null variable, is a valid variable name, that is either empty (like if you did 'anchor3 = objNull') or it was referring to an object that has since been delete (like in 'deleteVehicle anchor3')

NIL - a 'nil' value is a scripting variable name, that has never been created. so until you do either 'anchor3 = objNull;' or 'anchor3 = "classname" createVehicle [param, param, param]' the variable will be nil because the variable has never before been set or initialized. You can check if this is the case with isNil

I hope this helps you understand the differences. If this explanation was unclear or you have more questions, please ask.

Share this post


Link to post
Share on other sites
Then the wiki example is wrong? If I use isNil then the object name would have to be surrounded in quotes, which would indeed make it a variable, albeit, a str variable and not an object. So how does one test if an object has been created yet? btw, using (alive obj) also produces an undefined variable error if the object had not been created yet.

The wiki example is correct. It doesn't make it a string variable. The argument of the command itself is simply a string which defines the variable name you want to check :) I was simply trying to explain that an object name isn't actually the name of an object, but the name of a variable which contains the object, which is why isNil would be used when it hasn't been created yet.

DnA_UK's code above will work perfectly for you.

Edit: somners also put this very clearly. Like I said, I suck at explaining things. :p

Share this post


Link to post
Share on other sites
Then the wiki example is wrong? If I use isNil then the object name would have to be surrounded in quotes, which would indeed make it a variable, albeit, a str variable and not an object. So how does one test if an object has been created yet? btw, using (alive obj) also produces an undefined variable error if the object had not been created yet.

This does not make it a string variable, it is the name of the variable. anchor3 is the variable name. the object it refers to, is an object.

Share this post


Link to post
Share on other sites

This statement returns true even after the object named anchor3 has been successfully created via script ON A DEDICATED SERVER, any idea why?

if ( isNil "anchor3" || { isNull anchor3 } )

Share this post


Link to post
Share on other sites

well is the code shown run on the client or the server?

If that code is run on the client, but the vehicle was created on the server, it will return that, because the client doesn't know about the variable name. you have to update the client.

This is where Multiplayer scripting comes into play, its more complex than single player scripting. You should read the Biki about it to get a better understanding.

Share this post


Link to post
Share on other sites

@somners

So if the server creates the object and all clients see it, the object's variable name is not known by the client until each object's name has been "publicVariable"d ? It doesn't make sense that even after the object has been created :

anchor3 = "Sign_Pointer_F" createVehicle targetpos; 

would return "anchor3" as nil when the object anchor3 is no longer null.

So, it brings me back to my op, (So how would I test for an object named "anchor3" to see if it has been created yet or not? )

Edited by tomturner

Share this post


Link to post
Share on other sites
@somners

So if the server creates the object and all clients see it, the object's variable name is not known by the client until each object's name has been "publicVariable"d ? It doesn't make sense that even after the object has been created :

anchor3 = "Sign_Pointer_F" createVehicle targetpos; 

would return "anchor3" as nil when the object anchor3 is no longer null.

So, it brings me back to my op, (So how would I test for an object named "anchor3" to see if it has been created yet or not? )

what you said is correct, the client will not know the variable name exists on the server unless you use 'publicVariable' to tell it. There is no way to do this without client/server communication.

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  

×