- Home /
How to implement nested arrays correctly?
I'm trying to make a map creation system similiar to a risk strategy game, I want to store the data for each map in a group of nested arrays but I'm really bad with arrays and can't get them to work, here is what I would like to do (but doesn't work):
map['maxPlayers'] = 2;
map['platforms'] = Array;
//Platform 1
map['platforms'][0] = Array;
map['platforms'][0]['ID'] = 1;
map['platforms'][0]['ownerID'] = 1;
map['platforms'][0]['type'] = 1;
map['platforms'][0]['troopCount'] = 1;
map['platforms'][0]['atkBonus'] = 1;
map['platforms'][0]['defBonus'] = 1;
map['platforms'][0]['proBonus'] = 1;
map['platforms'][0]['nodes'] = 1;
//Platform 2
map['platforms'][1] = Array;
map['platforms'][1]['ID'] = 1;
map['platforms'][1]['ownerID'] = 1;
map['platforms'][1]['type'] = 1;
map['platforms'][1]['troopCount'] = 1;
map['platforms'][1]['atkBonus'] = 1;
map['platforms'][1]['defBonus'] = 1;
map['platforms'][1]['proBonus'] = 1;
map['platforms'][1]['nodes'] = 1;
This is what would be ideal for me but I can't get it to work, I's sure it's just some simple sintax problem I'm having but any help would be great thanks!
Answer by Benproductions1 · Jun 11, 2013 at 01:52 AM
Hello,
Ok, there are so many problems with what you are doing:
Fistly, you can't use a string to reference an item in an array. Only indecies work.
Secondly, you have to initialize Arrays, using "new Array()"
Thirdly, you can't add things to an array by setting a value to an I referenced index. Fourthly, you should never use "Array"s they are memory hungry and slow. Use generic lists instead. Fithly, for your purpose, I suggest using "Classes" instead of arrays/lists. Look them up and see their awesomeness!!!
Hope this helps,
Benproductions1
Thankyou very much I will take a look at what you recommended!
Yes was just checking it was right for me :D and yes classes were just what I was looking for and it now works great thanks!
Your answer
Follow this Question
Related Questions
Sorting arrays? 1 Answer
How to delete new'd arrays? 2 Answers
what is the difference between a float[] and a float[,]? 1 Answer
How to set parents of all objects in an array? 1 Answer
Parse Array data to generate html 0 Answers