- Home /
What type of Array should I use?
I need a javascript 2D array to store information about GUI boxes to be rendered, for example.
The "boxTitle" along with a few other columns need to be strings, the xPosition, yPosition etc. Determine the position of each box and therefor need to be ints.
My question, how would I go about declaring this, what type of array should I use, and could you give me an example of how I would then read from the array?
I have spent a whole day trying to wrap my head around something that should be really easy.
Answer by Piflik · Jan 05, 2013 at 02:57 PM
Create a custom class with the needed attributes and create an array of that class.
//Box.js:
public class Box {
public var boxID : int;
public var boxTitle : String;
public var xPosition : int;
public var yPosition: int;
public var xSize : int;
public var ySize : int;
function Box() {
boxID = 0;
boxTitle = null;
xPosition = 0;
yPosition = 0;
xSize = 0;
ySize = 0;
}
function Box(ID : int, title : String, xP : int, yP : int, xS : int, yS : int) {
boxID = ID;
boxTitle = title;
xPosition = xP;
yPosition = yP;
xSize = xS;
ySize = yS;
}
}
Create an array like this:
private var testArr : Box[] = new Box[10]; //use whatever number of entries you need in that array
I think this will get me on my way but I do have another question, I am very new to all this sorry. Could you give me an example of how I would add an entry to the "testArr" array?
You need to create the array with enough entries, because you can't resize it.
You would not add new entries, but replace existing ones, like:
testArr[3] = new Box(1323, "testBox", 10, 40, 15, 45); //values completely random
If you need to iterate through this array, and you are not sure that it is completely filled with valid Boxes, you can test the ID of each entry and only create/display a GUI.Box if the ID is not 0 (since the array is initialized with default Boxes and these have ID = 0).
Example:
for(var i : int = 0; i < testArr.length; i++) {
if(testArr[i].ID != 0) {
//do stuff
}
}
If you need to change the number of entries dynamically, you could use a list ins$$anonymous$$d of the array.
Hmm, i'm getting an error using that line "testArr[3] = new Box(1323, "testBox", 10, 40, 15, 45);" it says: "The type 'Box' does not have a visible constructor that matches the argument list '(int, String, int, int, int, int)'."
Ok, sorry for the confusion. Never used a custom class in UnityScript, so I had some mistakes in my code. First of all, UnityScripts are Subclasses of $$anonymous$$onoBehaviour by default, and they can't have constructors with arguments, so you need to specify that this class should not extend $$anonymous$$onoBehaviour...also constructors apparently don't have a return type in UnityScript. I updated the code above (the Box.js), and now it should work.
That fixed most of the problem, although "testArr[i].ID" doesn't seem to reference the class. I get the error "NullReferenceException: Object reference not set to an instance of an object"
Thank you for all this help btw, I really do appreciate it.
Your answer
Follow this Question
Related Questions
Setting Scroll View Width GUILayout 1 Answer
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Arrange ints from biggest to smallest and display them in a table 1 Answer
Joystick ellipse movement (2d mobile asset modification) 1 Answer
Different results between "ContactPoint" and "ContactPoint2D" 0 Answers