0

So I've seen a couple methods of this but they don't seem to work for what I'm looking do to...

I'm using this push for another section of code and I don't see why it would not also work for this one in question.

var npc = new Array();

npc.push([  
[5,25,10],
]);

npc.push([  
[18,28,38],
]);

npc.push([  
[1,2,3],
]);

I want to set this up so I can call something like...

LoadNpc(2);

I want it to check the 2nd npc array and set the 3 numbers to variable..

hp = 18;
atk = 28;
def = 38;
id = 2;  //current loaded npc's id

I'm sure I've been over thinking how to do this and I can't get anything to work correctly. If anyone has a simple javascript to this properly I would be very thankful.

2 Answers 2

3

You should use array of objects not array of arrays.

npc[2] = {hp :18, atk: 28, def :38} 

You "pull" the values like this:

alert(npc[2].atk);
alert(npc[2].hp);
...
Sign up to request clarification or add additional context in comments.

2 Comments

Why is that? I would normally using an array of objects. But, whats wrong with the Array of Arrays?
@limoragni, array is good for multiple entities of one value, object is for multiple values for one "entity".
0

This looks pretty straightforward:

var npc = [];
npc.push([5,25,10]);
npc.push([18,28,38]);
npc.push([1,2,3]);

var hp, atk, def, id;

function loadNpc(index){
    id = index;
    hp = npc[index-1][0];
    atk = npc[index-1][1];
    def = npc[index-1][2];
}

1 Comment

This is exactly what I was attempting. Thank you!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.