0

need some help with a script completely on array that I'm doing

    skill = [
        //[ID, "NAME", TMLEVEL, Learn, Mastery, Prerequisite, PrerequisiteLvl],
        //Schoolgirl, Fighter
        [0, "Steel Punch", 0, 0, null, null],
        [1, "Shockwave", 1, 1, 2, null],
        [2, "Bull's Eye", 10, 2, 2, null],
        [3, "Burning Rave", 20, 2, 2, null],
        [4, "Shockvibe", 20, 1, 2, null],
        [5, "Sense Breaker", 20, 1, 2, null],
        [6, "Luck Breaker", 20, 1, 2, null],
        [7, "Pumping Heart", 25, 3, 3, skill[3], 1],
        [8, "Armor Breaker", 30, 2, 2, skill[1], 10],
        [9, "Upper Smash", 40, 2, 2, skill[2], 10],
        [10, "Hyper Beat", 45, 4, 3, [skill[2],skill[3]], [10,10]],
        [11, "Tornado Bomb", 50, 3, 3, skill[8], 1]
    ];

I need that inside the array, in certain points, to call th array again to put the array value in there, like i have here. In theory this works fine, without any error, but when i call the array inside it,it says that it's "undefined".

Any one knows how can i do this without rewrite everything on it? (because i use this in +- 300 code lines).

Nikato
  • 111
  • 1
  • 10

3 Answers3

1

You'll have to either rethink your whole approach here (recommended), or set those items to null at first, then rerun the declaration:

skill = [
    //[ID, "NAME", TMLEVEL, Learn, Mastery, Prerequisite, PrerequisiteLvl],
    //Schoolgirl, Fighter
    [0, "Steel Punch", 0, 0, null, null],
    [1, "Shockwave", 1, 1, 2, null],
    [2, "Bull's Eye", 10, 2, 2, null],
    [3, "Burning Rave", 20, 2, 2, null],
    [4, "Shockvibe", 20, 1, 2, null],
    [5, "Sense Breaker", 20, 1, 2, null],
    [6, "Luck Breaker", 20, 1, 2, null],
    [7, "Pumping Heart", 25, 3, 3, null, 1],
    [8, "Armor Breaker", 30, 2, 2, null, 10],
    [9, "Upper Smash", 40, 2, 2, null, 10],
    [10, "Hyper Beat", 45, 4, 3, null, null],
    [11, "Tornado Bomb", 50, 3, 3, null, 1]
];

skill = [
    //[ID, "NAME", TMLEVEL, Learn, Mastery, Prerequisite, PrerequisiteLvl],
    //Schoolgirl, Fighter
    [0, "Steel Punch", 0, 0, null, null],
    [1, "Shockwave", 1, 1, 2, null],
    [2, "Bull's Eye", 10, 2, 2, null],
    [3, "Burning Rave", 20, 2, 2, null],
    [4, "Shockvibe", 20, 1, 2, null],
    [5, "Sense Breaker", 20, 1, 2, null],
    [6, "Luck Breaker", 20, 1, 2, null],
    [7, "Pumping Heart", 25, 3, 3, skill[3], 1],
    [8, "Armor Breaker", 30, 2, 2, skill[1], 10],
    [9, "Upper Smash", 40, 2, 2, skill[2], 10],
    [10, "Hyper Beat", 45, 4, 3, [skill[2],skill[3]], [10,10]],
    [11, "Tornado Bomb", 50, 3, 3, skill[8], 1]
];

That way, the array elements you are trying to access already exist, now you are just overwriting them.

dave
  • 62,300
  • 5
  • 72
  • 93
  • but with this instead of 300 code lines i use 600 :-/ it may become slow on slower computers no? – Nikato Jun 19 '14 at 22:39
  • 1
    @Nikato if you want to make it right, you should rethink your whole approach. Here you are copying skills in prerequisites, which is unnecessary, you should just use their ID, that would be muuuuch simpler and it would work. The code Dave gave us here will not work if for example skill #1 is a prerequisite of skill #2 and skill #2 is a prerequisite for skill #3. So you should really avoid that. – blex Jun 19 '14 at 22:54
  • what do you suggest then? i'm doing like Dave said but instead of 2 times i'm overwriting 3 times the code, and works fine – Nikato Jun 19 '14 at 23:01
1

After a long process, I have come up with a solution that will replace all prerequisites, even if they are several levels deep (e.g. skill_3 requires skill_2 which requires skill_1...).

This will require your skill variable to be correctly declared (in your question, not all of the skills had 7 variables).

Here is an example of what the variable will look like:

var skill = [
    //[ID, "NAME", TMLEVEL, Learn, Mastery, Prerequisite, PrerequisiteLvl],
    [0, "Steel Punch",  0,  0,  0,  null,   null],
    [1, "Shockwave",    1,  1,  2,  null,   null],
    [2, "Bull's Eye",   10, 2,  2,  7,      null],
    [3, "Burning Rave", 20, 2,  2,  null,   null],
    [4, "Shockvibe",    20, 1,  2,  null,   null],
    [5, "Sense Breaker",20, 1,  2,  null,   null],
    [6, "Luck Breaker", 20, 1,  2,  null,   null],
    [7, "Pumping Heart",25, 3,  3,  3,      1],
    [8, "Armor Breaker",30, 2,  2,  7,      10],
    [9, "Upper Smash",  40, 2,  2,  2,      10],
    [10,"Hyper Beat",   45, 4,  3,  [2,3],  [10,10]],
    [11,"Tornado Bomb", 50, 3,  3,  8,      1]
];

Now, I thought of a function setPrerequisites() that will, for 1 skill, recursively set it's prerequisites:

Array.prototype.setPrerequisites = function(){
    if (typeof this[5] === "number")
    {
        this[5]=skill[getPosOfSkill(this[5])];
        this[5].setPrerequisites();
    }
    else if (this[5] instanceof Array)
    {
        if (this[5].isSkill()) this[5].setPrerequisites();
        else
        {
            for(var i = 0; i < this[5].length; i++)
            {
                this[5][i] = skill[getPosOfSkill(this[5][i])];
                this[5][i].setPrerequisites();
            }
        }
    }
}

This function uses isSkill() to determine whether an array is a skill, or an array of skill IDs:

Array.prototype.isSkill = function(){
    return this.length==7 && typeof this[1]==="string";
}

It also uses getPosOfSkill(id) to look for the right skill in case your skills were listed in no particular order, or if ID's are missing:

function getPosOfSkill(id){
    for(var i=0; i<skill.length; i++) if (skill[i][0]==id) return i;
    return false;
}

All you have to do is declare your skill variable, and then fill it:

for (var i = 0; i < skill.length; i++) skill[i].setPrerequisites();

// if you want to see the results
console.log(skill);

Here is a jsFiddle Demo

blex
  • 24,941
  • 5
  • 39
  • 72
0

I think i found a easy way, but still has the problem if it's defined after, but since i only call the values defined before there is no problem for now.

But if anyone knows some way better let me know please.

Here is what i do still using only arrays:

    var skill = [];
    skill[0] = [0, "Steel Punch", 0, 0, null, null];
    skill[1] = [1, "Shockwave", 1, 1, 2, null];
    skill[2] = [2, "Bull's Eye", 10, 2, 2, null];
    skill[3] = [3, "Burning Rave", 20, 2, 2, null];
    skill[4] = [4, "Shockvibe", 20, 1, 2, null];
    skill[5] = [5, "Sense Breaker", 20, 1, 2, null];
    skill[6] = [6, "Luck Breaker", 20, 1, 2, null];
    skill[7] = [7, "Pumping Heart", 25, 3, 3, skill[3], 1];
    skill[8] = [8, "Armor Breaker", 30, 2, 2, skill[1], 10];
    skill[9] = [9, "Upper Smash", 40, 2, 2, skill[2], 10];
    skill[10] = [10, "Hyper Beat", 45, 4, 3, [skill[2],skill[3]], [10,10]];
    skill[11] = [11, "Tornado Bomb", 50, 3, 3, skill[8], 1];

This way i define them and i'm still using the arrays has i wanted (1 night of sleep makes me think much better :P)

Nikato
  • 111
  • 1
  • 10