Jump to content
Sign in to follow this  
ZNorQ

Passing undeclared variable to function

Recommended Posts

I'm creating a mission framework/template (called "NAMF") where I have a set of (public) variables "attached" to an object ("nsf_oJIP"). These variables will always be syncronized to all platforms (ie. clients and server).

In this template, I've also created a function ("nsf_fJIPn") that will return a boolean result; the purpose is to check if a public variable (in the "NSF_oJIP" "namespace") is declared or not. I want this function to handle all sorts of inputs, but it is primarily expecting a nsf_oJIP attached variable. My current code is as follows;

nsf_fJIPn = {
   if     ( ( !isNil "_this" ) and ( typeName _this == "STRING" ) )
   then { isNil { nsf_oJIPns getVariable _this; };}
   else  { true;};
 };

I see 4 scenarios which I want the function to handle;

The parameter...

a) ...is already declared, but wrong format (not a string).

b) ...supplied, but isn't declared at all (isNull true).

c) ...is missing (no parameter input)

d) ...the parameter is correct (string)

I'm not having problems with a), c) and d), but b) is a major pain you-know-where.

Do anyone know if it is possible to check for this scenario?

I have a follow-up question to c);

It seems to me that if you use "call" without any parameters, _this becomes -1; can anyone confirm this? If this is true, if ( !isNil "_this" ) could never be false, and one can not really determine if the call function was supplied with a parameter or not. I'd rather _this = nil (undeclared), hence it would be easy to determine if a parameter was supplied or not.

Example;

hint format["%1", call {_this;}];

.. returns -1

Regards,

Kenneth aka. ZNorQ

Edited by ZNorQ
The function was wrong, corrected it to as-is.

Share this post


Link to post
Share on other sites
nsf_fJIPn = {
   if     ( ( !isNil "_this" ) and ( typeName _this == "STRING" ) )
   then { isNil { nsf_oJIPns getVariable _this; };}
   else  { true;};
 };

i cant help myself... but all what i see is that u try to hide error messages from users.. ?

re u really sure about that?

if i understand it right u re trying to create "bulletproof" function but all what it will do will be that it hide error messages from users... code should be always done in right way...

and also... re u sure that u really need function like this ? isnt isNil enough ?

Share this post


Link to post
Share on other sites
i cant help myself... but all what i see is that u try to hide error messages from users.. ?

re u really sure about that?

if i understand it right u re trying to create "bulletproof" function but all what it will do will be that it hide error messages from users... code should be always done in right way...

and also... re u sure that u really need function like this ? isnt isNil enough ?

No, that wasn't the purpose at all, it is rather the opposite; as I stated above I want to catch incidents where an undeclared variable passed to the call function.

When I do that, the function returns "any", rather than true.

Btw, the function is a "skeleton" version; it works on a), c) and d), but b) doesn't work. Also, it seems that c) is always converted to a "-1" value, hence isNil "_this" will never be false (as far as I can see.)

Last but not least: I'm really not asking about this just for this specific function; just curious about the general functionality of passing parameters/arguments to a "call" function.

ZNorQ

---------- Post added at 14:50 ---------- Previous post was at 14:49 ----------

Try looking into this:

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

You can use it to setup for multi-parameter data pattern options, if it isn't quite what you are looking for then the only option you really have is using a switch statement on _this with typename and value checking.

Thanks, will check it out. :) However, I'm not sure if that will answer my question which was how to detect passing undeclared parameters to a call function. ;)

ZNorQ

Share this post


Link to post
Share on other sites
No, that wasn't the purpose at all, it is rather the opposite; as I stated above I want to catch incidents where an undeclared variable passed to the call function.

thats exactly why i said that u try to create "bulletproof" function...

but qustion is.... how can this happen?

u shouldnt pass wrong parametrs and if u do so u should see error message about that...

and all what your function do is that it hides this error message...........

Share this post


Link to post
Share on other sites
u shouldnt pass wrong parametrs and if u do so u should see error message about that...

Thats the whole point; any isNil-parameters is wrong/an error and shouldn't happen. So, passing an undefined variable as a parameter is also a scenario that may happen and should be taken care of. However, I don't know how to implement code for handling that. If I'm misunderstanding you, LoonyWarrior, I'm sorry.. Oo

Examples;

[b][color=#800080]// a) parameter is already declared, but wrong format (not a string);[/color][/b]
myparam = ["test"]; myparam call nsf_fJIPn; [color=#006400]// returns true, array not allowed.[/color]

[b][color=#800080]// b) parameter supplied, but was never declared/assigned a value.[/color][/b]
[color=#ff0000]// ~ Don't know how to implement this scenario. ~[/color]

[b][color=#800080]// c) parameter is missing (no parameter input)[/color][/b]
call nsf_fJIPn;    [color=#006400]// returns true (In essence, _this becomes -1, (SCALAR), which again isn't an allowed format.)[/color]

[b][color=#800080]// d) parameter is correct (string)[/color][/b]
myparam = "sometext"; myparam call nsf_fJIPn; [color=#006400]// returns false (Param is as expected, everything ok.)[/color]

Thanks for your feedback, LoonyWarrior.

Regards,

ZNorQ

Edited by ZNorQ
Fixed the quote

Share this post


Link to post
Share on other sites
Thats the whole point; any isNil-parameters is wrong/an error and shouldn't happen. So, passing an undefined variable as a parameter is also a scenario that may happen and should be taken care of. However, I don't know how to implement code for handling that. If I'm misunderstanding you, LoonyWarrior, I'm sorry..

my point was... that this shouldnt happen..

u try to "cover" it in the function... but i believe that u should always pass "right" parameters... if u will do so.. u dont have to check if they re right.. and if u will provide wrong parameters u will get error message...

i understand that your question is how to check if _this in called script isNil and all what can i say about that is that it really works as u described..........

---------- Post added at 16:50 ---------- Previous post was at 16:24 ----------

...there re two points that i dont understand

1. the thing about "allowing" wrong parameters

2. the idea of the function itself....

example:

server

if isServer then
{
 TAG_someVariable = false;

 // now something happened

 TAG_someVariable = true;
 publicVariable "TAG_someVariable";
};

on clients connected at time of publishing u handle it with event handler..

for JIP clients all published variables re already declared at start of initialization (init.sqf)

so all what u have to do is...

client

if !isServer then
{
 if (!isNil "TAG_someVariable") then
 {
   // varialble already published do something
   // this should simply execute same function as event handler do for "normal" clients
 };
};

Edited by LoonyWarrior

Share this post


Link to post
Share on other sites

Of course I should always pass the right parameters, but I'm human - I do make mistakes. So, when I do, the function should handle this. However, passing undeclared variables screws everything up, and a function that was meant to return boolean (true or false) all of a sudden returns "any", and makes it harder for me to track bugs.

The whole point isn't really about this particular function, but how to handle undeclared variables as parameters for function calls in general.

It's really a pity (well, have kind of mixed feelings about it) that one don't have to declare variables in advance (like in C/C++, etc. etc), so that one cannot make mistakes. Using undeclared variables in C/C++ reports an error, sqf don't. (I say mixed feelings, because I kinda like the isNil functionality).

So, just forget about this particular function (and the purpose of it); concentrate on item b) - passing undeclared variables as parameters to call functions.

;)

Kenneth aka ZNorQ

PS! I must admit I'm having a bit problems understanding you. Probably me since I'm from Norway... Oo :P Thanks for the feedback anyhow!

Edited by ZNorQ
Spelling

Share this post


Link to post
Share on other sites

myFunction1 =
{
 if ((count _this) > 0) then
 {
   true;
 }
 else
 {
   false;
 };
};

_variable = call myFunction1;

hint str _variable;

this will return false if there is no parametr provided... no errors... but if u will pass the string it will end in error... because of type... it expects array...

Share this post


Link to post
Share on other sites

Now I'm really confused...! Oo I played around with your example, and now I'm received "undefined variable in expression" errors...!

Also, isNil "_this" returns true if no parameters are supplied... I think I need a break...!

I gotta really investigate why the hell I received "Any" in my previous tests...

I know now that there are some problems using call in the A3 debug window... Seems DISPLAY is passed as a parameter if I use

a call without parameters...

Thanks man, I'll need a few to investigate my code more thoroughly.

ZNorQ

Share this post


Link to post
Share on other sites

dont be confused... :cool:

all what that code says is that if u have undeclared parameters _this is empty array....

Share this post


Link to post
Share on other sites
I'm creating a mission framework/template (called "NAMF") where I have a set of (public) variables "attached" to an object ("nsf_oJIP"). These variables will always be syncronized to all platforms (ie. clients and server).

In this template, I've also created a function ("nsf_fJIPn") that will return a boolean result; the purpose is to check if a public variable (in the "NSF_oJIP" "namespace") is declared or not. I want this function to handle all sorts of inputs, but it is primarily expecting a nsf_oJIP attached variable. My current code is as follows;

nsf_fJIPn = {
   if     ( ( !isNil "_this" ) and ( typeName _this == "STRING" ) )
   then { isNil { nsf_oJIPns getVariable _this; };}
   else  { true;};
 };

I see 4 scenarios which I want the function to handle;

The parameter...

a) ...is already declared, but wrong format (not a string).

b) ...supplied, but isn't declared at all (isNull true).

c) ...is missing (no parameter input)

d) ...the parameter is correct (string)

I'm not having problems with a), c) and d), but b) is a major pain you-know-where.

Do anyone know if it is possible to check for this scenario?

I have a follow-up question to c);

It seems to me that if you use "call" without any parameters, _this becomes -1; can anyone confirm this? If this is true, if ( !isNil "_this" ) could never be false, and one can not really determine if the call function was supplied with a parameter or not. I'd rather _this = nil (undeclared), hence it would be easy to determine if a parameter was supplied or not.

Example;

hint format["%1", call {_this;}];

.. returns -1

Regards,

Kenneth aka. ZNorQ

The solution is a lot easier than you think ,

With getVariable you can set a default value to return if the the var is not defined. Then check for that default vaule for your case B.

_varTest = nsf_oJIPns getVariable [_this,"myVarIsNul"];

if (_varTest == "myVarIsNul") then {
  hint "You did not declare the variable!";
};


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

defaultValue: Any Value - Value to return if variable doesn't existReturn 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
The solution is a lot easier than you think ,

With getVariable you can set a default value to return if the the var is not defined. Then check for that default vaule for your case B.

_varTest = nsf_oJIPns getVariable [_this,"myVarIsNul"];

if (_varTest == "myVarIsNul") then {
  hint "You did not declare the variable!";
};


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

I wasn't aware of this feature! Thanks, Riouken.

ZNorQ

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  

×