Jump to content
Sign in to follow this  
654wak654

Having Trouble Using Private Variables Outside Scope

Recommended Posts

Allright, before asking my question with the example code, I wanna make my public apologies to the community:

This is the 2nd time I'm asking for a scripting question ever, I always thought there are enough resources if you look enough, and there is a LOT about this subject. I've been trying by my self for couple days but I'm giving up. I'm obviously mission something blatantly obvious and simple... Here is 654wak654' s 2nd question to the ArmA Scripting community, and it's probably even easier than the first one. Sorry I found the worst subject to fail.

[color=#FF8040][color=#191970][b]if[/b][/color] [color=#8B3E2F][b]([/b][/color]CONDITION_1[color=#8B3E2F][b])[/b][/color] [color=#191970][b]then[/b][/color] [color=#8B3E2F][b]{[/b][/color]
   [color=#1874CD]_pvar1[/color] [color=#8B3E2F][b]=[/b][/color] [color=#7A7A7A]"stuff"[/color][color=#8B3E2F][b];[/b][/color]
   [color=#191970][b]if[/b][/color] [color=#8B3E2F][b]([/b][/color][color=#8B3E2F][b]![/b][/color][color=#8B3E2F][b]([/b][/color][color=#1874CD]_pvar1[/color] [color=#191970][b]in[/b][/color] [color=#1874CD]_array[/color][color=#8B3E2F][b])[/b][/color][color=#8B3E2F][b])[/b][/color] [color=#191970][b]then[/b][/color] [color=#8B3E2F][b]{[/b][/color][color=#1874CD]_pvar1[/color] [color=#8B3E2F][b]=[/b][/color] [color=#7A7A7A]"other stuff"[/color][color=#8B3E2F][b]}[/b][/color][color=#8B3E2F][b];[/b][/color]
[color=#8B3E2F][b]}[/b][/color] [color=#191970][b]else[/b][/color] [color=#8B3E2F][b]{[/b][/color]
   [color=#1874CD]_pvar1[/color] [color=#8B3E2F][b]=[/b][/color] [color=#7A7A7A]"other stuff"[/color][color=#8B3E2F][b];[/b][/color]
[color=#8B3E2F][b]}[/b][/color][color=#8B3E2F][b];[/b][/color]
[color=#191970][b]if[/b][/color] [color=#8B3E2F][b]([/b][/color]CONDITION_2[color=#8B3E2F][b])[/b][/color] [color=#191970][b]then[/b][/color] [color=#8B3E2F][b]{[/b][/color]
   [color=#1874CD]_pvar2[/color] [color=#8B3E2F][b]=[/b][/color] [color=#1874CD]_pvar1[/color] [color=#8B3E2F][b]+[/b][/color] [color=#7A7A7A]"is stuff"[/color][color=#8B3E2F][b];[/b][/color]
[color=#8B3E2F][b]}[/b][/color] [color=#191970][b]else[/b][/color] [color=#8B3E2F][b]{[/b][/color]
   [color=#1874CD]_pvar2[/color] [color=#8B3E2F][b]=[/b][/color] [color=#1874CD]_pvar1[/color] [color=#8B3E2F][b]+[/b][/color] [color=#7A7A7A]"is other stuff"[/color][color=#8B3E2F][b];[/b][/color]
[color=#8B3E2F][b]}[/b][/color][color=#8B3E2F][b];[/b][/color]
[color=#1874CD]_pvar2[/color] [color=#191970][b]call[/b][/color] WAK_fnc_someFunction[color=#8B3E2F][b];[/b][/color][/color]

Made with KK's SQF to BBCode Converter

What's happening is obvious: If I run the code, I'm getting errors for undefined variables. I know I can declare them by using the "private" command at the start outside both if scopes, but they're just staying nil outside the scope which makes my function throw an error. I'm pretty damn sure making a string with this code should pretty easy, and that I'm missing something. Also sorry for the English in this post, not in my best condition.

I'm not Seppuku bad, just got the seasonal flu (also tired) and wanted to put some humor in the thread.

Share this post


Link to post
Share on other sites

Private command does not define variables. It only localizes them to be used within the scope of the script. _ array must be defined within the scope of your script before it is used like:

_array = [];

or

_array = [element1,element2];

If the Error is comming form the function then passed variable to the function must be defined in the function itself like:

private "_pvar2";

_pvar2 = _this select 0;

You can totally pass a private variable to another script or function as long as it is again defined in the other script/function. What is the error exactly? Also maybe try to pass the variable to function like this:

[_pvar2] call WAK_fnc_someFunction;

It can be handy to make your variables global ie; without _ for testing in debug console to verify variable values. After testing and verifying that that the variables are valid and that script /function works then change them back to local variables with leading underscore.

Edited by Jigsor

Share this post


Link to post
Share on other sites

You should define _pvar1, _pvar2, and _array before your "If Then" statements because variables remain local to the scope they were defined in. Lower scopes have access to them, but higher scopes do not. If you don't believe me you can run a small check like this:

if (true) then
{
private["_test"];
_test = "This variable will only exist inside this ""if"" statement";
};
hint str _test;

That said:

_pvar1 = "";
_pvar2 = "";
_array = [];
if (CONDITION_1) then
{
_pvar1 = "stuff";
if (!(_pvar1 in _array)) then {_pvar1 = "other stuff"};
} else
{
_pvar1 = "other stuff";
};
if (CONDITION_2) then
{
_pvar2 = _pvar1 + "is stuff";
} else {
_pvar2 = _pvar1 + "is other stuff";
};
_pvar2 call WAK_fnc_someFunction;

It only localizes them to be used within the scope of the script.

Correction: It only localizes them to the scope that the command was run in

Share this post


Link to post
Share on other sites

You already know youre defining them inside the if statement's scope and trying to use them out side of the scope where they were defined. What about putting your strings into the array and then calling the function?

private ["_pvar1","_pvar2"];

_array = [];

if (CONDITION_1) then {
   _pvar1 = "stuff";
   if (!(_pvar1 in _array)) then {
	_pvar1 = "other stuff";
};
} else {
   _pvar1 = "other stuff";
};
if (CONDITION_2) then {
   _pvar2 = _pvar1 + "is stuff";
} else {
   _pvar2 = _pvar1 + "is other stuff";
};

{
_array pushback _x;
} forEach [_pvar1,_pvar2];

_array call WAK_fnc_someFunction;

Edited by Iceman77

Share this post


Link to post
Share on other sites

This is very simple, a variable must be declared or defined in the scope it is used or in any of the parent scopes. See Iceman77 example.

Share this post


Link to post
Share on other sites

Sorry I tried to give the exact code I have, but forgot about the array. Thing is I already have a filled array which I compare to _pvar1, and after the first if & else _pvar1 is not used when calling the function, just _pvar2, the successor of _pvar1 (which should be my final string). You can ignore the function part, I'm getting an undefined variable error anyway.

This is very simple, a variable must be declared or defined in the scope it is used or in any of the parent scopes. See Iceman77 example.

Even if I define it as an empty string at the start, isn't it going to be just an empty string again while calling the function? Like the 4th code snippet here.

Share this post


Link to post
Share on other sites
Even if I define it as an empty string at the start, isn't it going to be just an empty string again while calling the function? Like the 4th code snippet here.

No. The reason the variable didn't change was because of use of the private command. Private allows you to create a new variable, without regard to variables in a parent scope. That means that you can even create variables with exactly the same name, and they won't affect each other. So if you don't use the private command in a lower scope, it'll be fine.

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  

×