Bug #12743

"_i" used but not declared.

Added by Evil_Echo almost 5 years ago. Updated almost 5 years ago.

Status:Feedback Start date:08/06/2010
Priority:Normal Due date:
Assignee:- % Done:

0%

Category:-
Target version:-
Affected Version:

fnc_Outliers.sqf (1.7 kB) Evil_Echo, 08/06/2010 22:52

fn_swapMagazines.sqf (3.9 kB) dj76, 08/07/2010 02:56

History

Updated by Evil_Echo almost 5 years ago

Sorry, hit the button too fast.

The situation occurs when a variable is invoked as part of a loop.

for "_i" from 0 to count _list do {
....
};

Updated by sbsmac almost 5 years ago

  • Status changed from New to Feedback

I tried

for "_i" from 0 to 2 do {

_b=_i ;
};

but couldn't reproduce. Do you have a code fragment which shows this ?

Updated by Evil_Echo almost 5 years ago

Here you go, fairly simple routine.

Updated by dj76 almost 5 years ago

I have the exact opposite of this bug:

'_i' declared but not used

when clearly I am using "_i" in my code:

    for "_i" from 1 to _max do {
        _unit removeMagazine _oldAmmo;
    };
    // add mags of other type
    for "_i" from 1 to _max do {
        _unit addMagazine _newAmmo;
    };

My private line at the top of the file includes "_i".

Not sure if I should open another issue for this or if it even is one.

Updated by sbsmac almost 5 years ago

Thanks for the code examples. In both cases (fnc_outliers and fn_swapMagazines) squint is correct when telling you that "_i" is declared but unused. This may be a bit surprising so let me explain....

The 'for' statement, when supplied as a string as an argument actually creates a new private variable of that name. You can verify this with this code...

_i=100;

for "_i" from 1 to 2 do {
player sidechat format ["i is %1",_i] ;
};
player sidechat format ["i is %1",_i] ;

You will see result of 1,2, and 100.

In the code files given, you have this construction ....

private [...,"_i",...] ;

...
for "_i" from ... to ...

So the 'outer' _i, ie the one declared in the private statement is never used at all and can be removed.

Having said all that, it would probably be helpful to change the warning in this case to make it clearer that the variable in for doesn't need a declaration.

Updated by Evil_Echo almost 5 years ago

Interesting info about the scope and auto-creation of variables, thanks.

Agree that this requires squint to be a tad more clever in evaluating whether this is an error and what to say to the user. I'd like to see more of my code show up "green". :-)

Updated by shuko almost 5 years ago

What makes this even more interesting is that the auto private only is if you use that version of the For syntax.

If you use the more traditional:

_i = 5;
for [{_i = 0},{_i < 2},{_i = _i + 1}] do {
  player sidechat format ["_i in loop: %1",_i];
};
player sidechat format ["_i after: %1",_i];

It will actually update the outer scope _i. So the print out will be:
loop: 0
loop: 1
after: 2

Updated by sbsmac almost 5 years ago

Yes, that's expected. The parameters in the array are just treated as standard code-blocks so are executed in the calling context. It would be very difficult for the arma engine to figure out which variables were being used in those codeblocks and to automatically make them private (contents of code-blocks can be arbitrarily complex). In contrast, the alternative syntax of

for "_a" ...

makes it very easy for the engine to deduce that you want to create a 'magic' variable called _a.

Also available in: Atom PDF