AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Variable already declared? (https://forums.alliedmods.net/showthread.php?t=245893)

PreDominance 08-08-2014 10:16

Variable already declared?
 
Greetings,
I have an odd case where I have a chain of else-if's, and a variable declared in one elif is in the same scope as another. Am I not fully understanding scope, or is it just not a good idea to program at 8am?

Code:
....     else if (strfind(name, "numberoflevels") != -1) {         if (consistent) {             for (i = 0; i < RACE_MAXSKILLS; i++) RACE_SKILL_LEVELS[num][i] = str_to_num(value);         } else {             new arrIds[RACE_MAXSKILLS][3];             util_explodeString(arrIds, RACE_MAXSKILLS, 2, value, '|');             for (i = 0; i < RACE_MAXSKILLS; i++) RACE_SKILL_LEVELS[num][i] = str_to_num(arrIds[i]);         }     } else if (strfind(name, "skillnames") != -1) {         new arrIds[RACE_MAXSKILLS][20]; //Is fine????         util_explodeString(arrIds, RACE_MAXSKILLS, 19, value, '|');         for (i = 0; i < RACE_MAXSKILLS; i++) RACE_SKILL_NAMES[num][i] = arrIds[i];     } else if (strfind(name, "skilldesc") != -1) {         new arrIds[RACE_MAXSKILLS][50]; // LINE 79         util_explodeString(arrIds, RACE_MAXSKILLS, 49, value, '|');         for (i = 0; i < RACE_MAXSKILLS; i++) RACE_SKILL_INFO[num][i] = arrIds[i];     } else if (strfind(name, "skillreqlevel") != -1) {         new arrIds[RACE_MAXSKILLS][3]; //LINE 83         util_explodeString(arrIds, RACE_MAXSKILLS, 2, value, '|');         for (i = 0; i < RACE_MAXSKILLS; i++) RACE_SKILL_REQ_LEVEL[num][i] = str_to_num(arrIds[i]);     } ...
http://puu.sh/aJTzO/9066db09d8.png

Backstabnoob 08-08-2014 11:57

Re: Variable already declared?
 
You have it defined somewhere else prior to the else-if block. Show the full function.

As a general 'lesson', whenever you're unsure whether there's a bug in the compiler or not, it's good to run a simple code through it:
PHP Code:

public x()
{
    new 
yz
    
if(z) { new var; }
    else if(
y) { new var; }


Compiles fine.

PreDominance 08-08-2014 12:02

Re: Variable already declared?
 
Here's the full function.
PHP Code:

transferToArray(name[], value[], numbool:consistent)
{
    static 
i;
    if (
strfind(name"name") != -1copy(RACE_NAMES[num], 19value);
    else if (
strfind(name"author") != -1)            copy(RACE_AUTHORS[num], 19value);
    else if (
strfind(name"required_level") != -1RACE_REQ_LEVEL[num] = str_to_num(value);
    else if (
strfind(name"maximum_level") != -1)    RACE_MAX_LEVEL[num] = str_to_num(value);
    else if (
strfind(name"teamlimit") != -1)         RACE_TEAM_LIMIT[num] = str_to_num(value);
    else if (
strfind(name"numberofskills") != -1RACE_NUM_SKILLS[num] = str_to_num(value);
    else if (
strfind(name"allow_only") != -1) {
        new 
arrIds[RACE_MAX_PRIVATE_ID][35], i;
        
util_explodeString(arrIdsRACE_MAX_PRIVATE_ID34value'|');
        for (
0RACE_MAX_PRIVATE_IDi++) RACE_ALLOW_ONLY[num][i] = arrIds[i];}
    else if (
strfind(name"numberoflevels") != -1) {
        if (
consistent) {
            for (
0RACE_MAXSKILLSi++) RACE_SKILL_LEVELS[num][i] = str_to_num(value);
        } else {
            new 
arrIds[RACE_MAXSKILLS][3];
            
util_explodeString(arrIdsRACE_MAXSKILLS2value'|');
            for (
0RACE_MAXSKILLSi++) RACE_SKILL_LEVELS[num][i] = str_to_num(arrIds[i]);
        }
    } else if (
strfind(name"skillnames") != -1) {
        new 
arrIds[RACE_MAXSKILLS][20]; //<-- Why is this ok?
        
util_explodeString(arrIdsRACE_MAXSKILLS19value'|');
        for (
0RACE_MAXSKILLSi++) RACE_SKILL_NAMES[num][i] = arrIds[i];
    } else if (
strfind(name"skilldesc") != -1) {
        new 
arrIds[RACE_MAXSKILLS][50]; //<-- Line 79, error.
        
util_explodeString(arrIdsRACE_MAXSKILLS49value'|');
        for (
0RACE_MAXSKILLSi++) RACE_SKILL_INFO[num][i] = arrIds[i];
    } else if (
strfind(name"skillreqlevel") != -1) {
        new 
arrIds[RACE_MAXSKILLS][3];
        
util_explodeString(arrIdsRACE_MAXSKILLS2value'|');
        for (
0RACE_MAXSKILLSi++) RACE_SKILL_REQ_LEVEL[num][i] = str_to_num(arrIds[i]);
    }


My thought process is that if I'd already declared arrIds[] elsewhere in the code, the line right above 79 wouldn't work right.

*Edit
Bad indenting on here. Picture might be easier to read.
http://puu.sh/aK0Lr/868f3be04c.png

Backstabnoob 08-08-2014 12:49

Re: Variable already declared?
 
... Interesting.
PHP Code:

public func(param)
{
    if(
param
    {
        new var[
1][1]
    }
    else
    {
        new var[
1][1]
    } 


Won't compile (symbol already defined). If, however, the array is one dimensional, it works fine:

PHP Code:

public func(param)
{
    if(
param
    {
        new var[
1]
    }
    else
    {
        new var[
1]
    } 


This is definitely a compiler bug. I find it hard to believe that it wasn't discovered yet, though. Did you try compiling it with an earlier build?

PreDominance 08-08-2014 13:05

Re: Variable already declared?
 
Uhh no, I have not. It's not a big deal to use different variable names though. Good to know I'm not crazy ;)

I assume you've reported the bug then, if not I can.

Backstabnoob 08-08-2014 13:06

Re: Variable already declared?
 
I'm a bit busy right now, so it'd be great if you reported the bug, but I can report it later myself.

PreDominance 08-08-2014 13:07

Re: Variable already declared?
 
Taken care of. Thanks for the help.


All times are GMT -4. The time now is 12:58.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.