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).
-
How are you calling the array thats inside?Farax– Farax2014年06月19日 22:32:30 +00:00Commented Jun 19, 2014 at 22:32
-
can you show how you are calling the array?BeNdErR– BeNdErR2014年06月19日 22:33:16 +00:00Commented Jun 19, 2014 at 22:33
-
Cant you access the inner array like this => skill[9][5]? Would return [2, "Bull's Eye", 10, 2, 2, null]Farax– Farax2014年06月19日 22:35:32 +00:00Commented Jun 19, 2014 at 22:35
-
it's in there, see at number 11 in the end i call skill[8] to get all the value of the array number 8 to the number 11Nikato– Nikato2014年06月19日 22:36:08 +00:00Commented Jun 19, 2014 at 22:36
-
4He's trying to access the array while defining it, it's not going to work.dave– dave2014年06月19日 22:36:09 +00:00Commented Jun 19, 2014 at 22:36
3 Answers 3
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.
3 Comments
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);
Comments
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)
Comments
Explore related questions
See similar questions with these tags.