Local Variable shadows a variable at a preceding level
#1

So I would like to clean up my Pawno Compiler box, and I have a few warnings. How would I fix this one?

Here's the Line:
pawn Код:
for(new idx=0; idx<MAX_DOORS; idx++)
Warning:
pawn Код:
local variable "idx" shadows a variable at a preceding level
Reply
#2

You must have that variable defined somewhere else in that area of the script or you have a global variable with that name.
Reply
#3

pawn Код:
new idx=1, File:file;
    if(!IsPlayerLoggedIn(playerid)) return SendClientMessage(playerid, COLOR_GREY, "You need to login first before using any command.");
    for(new idx=0; idx<MAX_DOORS; idx++)
Reply
#4

pawn Код:
new idx=1
This is the problem. You make a new variable titled "idx" but you also create it when making the loop. Either delete the "new" in the loop or remove the variable I showed above.
Reply
#5

Quote:
Originally Posted by Kindred
Посмотреть сообщение
You must have that variable defined somewhere else in that area of the script or you have a global variable with that name.
He's right, but let me explain it a little better so you'll know what he means for sure.

There are a few ways you could get this error, one is this:
pawn Код:
new idx = 0;

ThisCouldBeAnyFunction(functionparameter)
{
    for(new idx = 0; idx < MAX_DOORS; idx++)
        {
        // It will give that warning because "idx" is already defined as global variable ( A variable which isn't created in any function. )
        }
    return 1;
}
And this is the 2nd one:
pawn Код:
ThisCouldBeAnyFunction(functionparameter)
{
    new idx = 0;
    /*
    loads of code, but all in the same function.
    loads of code, but all in the same function.
    loads of code, but all in the same function.
    loads of code, but all in the same function.
    loads of code, but all in the same function.
    loads of code, but all in the same function.
    loads of code, but all in the same function.
    loads of code, but all in the same function.
    loads of code, but all in the same function.
    loads of code, but all in the same function.
    loads of code, but all in the same function.
    loads of code, but all in the same function.
    loads of code, but all in the same function.
    loads of code, but all in the same function.
    loads of code, but all in the same function.
    loads of code, but all in the same function.
    loads of code, but all in the same function.
    loads of code, but all in the same function.
    loads of code, but all in the same function.
    loads of code, but all in the same function.
    loads of code, but all in the same function.
    loads of code, but all in the same function.
    loads of code, but all in the same function.
    loads of code, but all in the same function.
    loads of code, but all in the same function.
    loads of code, but all in the same function.
    loads of code, but all in the same function.
    loads of code, but all in the same function.
    loads of code, but all in the same function.
    loads of code, but all in the same function.
    loads of code, but all in the same function.
    loads of code, but all in the same function.
    loads of code, but all in the same function.
    loads of code, but all in the same function.
    loads of code, but all in the same function.
    loads of code, but all in the same function.
    loads of code, but all in the same function.
    loads of code, but all in the same function.
    loads of code, but all in the same function.
    loads of code, but all in the same function.
    loads of code, but all in the same function.
    loads of code, but all in the same function.
    loads of code, but all in the same function.
    loads of code, but all in the same function.
    loads of code, but all in the same function.
    loads of code, but all in the same function.*/

    for(new idx = 0; idx < MAX_DOORS; idx++)
        {
        // It will give that warning because "idx" is already defined as local variable ( A variable which is created in the same function. )
        }
    return 1;
}
And a little extra information;
This will also give that warning:
pawn Код:
ThisCouldBeAnyFunction(functionparameter)
{
    for(new idx = 0; idx < MAX_DOORS; idx++)
        {
        for(new idx = 0; idx < MAX_DOORS; idx++)
            {
            break;
            }
        }
    return 1;
}
While this won't:
pawn Код:
ThisCouldBeAnyFunction(functionparameter)
{
    for(new idx = 0; idx < MAX_DOORS; idx++)
        {
        break;
        }
    for(new idx = 0; idx < MAX_DOORS; idx++)
        {
        break;
        }
    for(new idx = 0; idx < MAX_DOORS; idx++)
        {
        break;
        }
    for(new idx = 0; idx < MAX_DOORS; idx++)
        {
        break;
        }
    for(new idx = 0; idx < MAX_DOORS; idx++)
        {
        break;
        }
    for(new idx = 0; idx < MAX_DOORS; idx++)
        {
        break;
        }
    for(new idx = 0; idx < MAX_DOORS; idx++)
        {
        break;
        }
    return 1;
}
Simply because in a "for" statement/loop the "new ..." variable is local to that loop, if you use the same variable in a loop/"for" statement as the variable in which the loop/"for" statement is it will also shadow it. But if you have a lot of loops under eachother like in my last example you can use the same variables as the variable is local to the loop so out of that loop the variable isn't created anymore so if you use it in the next loop it will not be shadowed. These parts are quite hard to explain, at least I don't know how to explain it better. I hope you understand haha.

Best regards,
Jesse
Reply
#6

Fixed it, I remove the whole variable, I added it to support my family "HQ" system which failed terribly.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)