- Home /
what is the correct syntax for the creation of arrays?
I've read this: http://unity3d.com/support/documentation/ScriptReference/Array.html
But can't figure out when they're making up words to describe what they're doing or when they're using commands or reserved words.
Is there a perfect, clear definition of all the ways to create/declare arrays floating around somewhere? Real Reference material perhaps?
By way of example:
// This appears to create an array
function Start () { var arr = new Array ();
// but it looks all the world like declaring a function. // why does it look like declaring a function? // Why would you use this method when there are ways to declare it more structurally that don't confuse?
// This also creates an array
var values : float[]; // apprently this is a "builtin" array. How do I know that? // How do I know when an array is a builtin array or not? // float is a type, right? How many different types are there that arrays can be? // how many different types are there that builtin arrays can be?
If you use C#, there are lots of dedicated handbooks available. While Unity's docs frequently don't have C# examples, learning these fundamentals is much easier, and provides the reference it seems you are looking for. $$anonymous$$y 2 cents
Answer by Berenger · Dec 21, 2010 at 02:37 PM
Well the problem here is that javascript accept a lot of thing without complaining, so you don't really understand what's happening. Let's take a look at your code :
var arr = new Array();
This is just a short a way to declare an Array. In fact, here is what happened, but was hidden :
var arr : Array; // Declaration
arr = new Array(); // Initialisation, or instanciation. It's the same as i = 0, but Array is an object and need a call to it's constructor.
If it's a little fuzzy for you, you need to learn a bit more about "programmation orient objet" (don't know the good english term, sorry)
About the difference between classical array and built-in array, it's about performance. The first provide some useful tools (resizable, you can put anything in, etc ) but the second one is quicker. And you can build one with any type you want, even one you created, you just can't put different type in the same built-in array. Well, maybe that's not very clear ^^. I should let the code talk for me !
// Here is a class I created. "A" become a type, just like float. class A { function A(){ toto = 0; } //Constructor var toto : int; }
function Start() { // This is an instance of A, in a simple variable var simpleA : A = new A();
// This is a classical array, I put anything I want in it. var classicalArray : Array = new Array(); classicalArray.Add(simpleA ); classicalArray.Add("Hello"); classicalArray.Add(5); // Now you have in classicalArray => (simpleA, "Hello", 5 )
// This is a built in array of A. I fill only with A variables var builtinArray : A[]; builtinArray = new A[3]; // This create the array of size 3 //I fixed this line -Peter G
builtinArray[0] = new A(); // This create a new A builtinArray[1] = new A(); // This create a new A builtinArray[2] = new A(); // This create a new A // Now you have in builtinArray=> (A, A, A ) }
I hope it's more clear !
I truly appreciate your effort! But you've lost me with the class, but let's get to that later. Does a built in array have to be called builtinArray to work as a built in array? How does Unity know WHEN an array is a built in array as opposed to it being a classical array? Is it only by checking the contents? How do I define an array and BE SURE IT'S A BUILT IN ARRAY? Just by making sure the contents is all of a type that's declared? I don't have to say "hey, this here array is a built in array because I say I want it to be!" and then do the declaration? Unity just somehow knows it is?
Your built-in array code is not correct. You cannot initialize it like that, and you have to define its length as well.
Yep I just saw that, shame on me ! (It was the line builtinArray = new A();). And no, the name doesn't matter, the syntax Type[] does
The term you're looking for in your post is "Object Oriented Program$$anonymous$$g."
Answer by Statement · Dec 21, 2010 at 03:59 PM
Is there a perfect, clear definition of all the ways to create/declare arrays floating around somewhere? Real Reference material perhaps?
I think you really should pick up a book and learn programming. All of this will become immediately clear after finishing a beginners book in about any language.
I guess there are mainly two ways arrays (or rather collections) are formed. One way to do it is to use built in arrays, and another way to do it is using links/references to next item (linked lists or any node based architecture).
Fundamentally about every other collection class internally use either, or a combination of the above mentioned methods. Those collection classes include Array, List (and many other classes).
You were a bit confused about the constructor of Array.
But it looks all the world like declaring a function.
Why does it look like declaring a function?
What you're doing with new Array() is allocate memory for the type Array and invoke its constructor. The constructor is a function that is called when you are creating new instances of classes. This is similar to Awake() in your scripting. Again, I'd strongly advice you to go pick up a book about programming.
this is about the last thing I want to do, but increasingly beco$$anonymous$$g apparent that it's necessary. I, naively perhaps, thought that an engine with 250,000 users would have something targeted in terms of literature about how it's iteration of a language works. Ins$$anonymous$$d, as I've read elsewhere, and you're reinforcing, I need to learn a full blown language OUTSIDE of the direct Unity language and then reapply that knowledge here. That's kind of odd. 250,000 users is a LOT for a program like this. The kind of audience that warrants a dedicated handbook to the language constructed for this app.
one more go at this... there's the "builtin" array described as being much faster, is there an easy way for you to describe to me how/why Unity deter$$anonymous$$es that the array is builtin and not a traditional array? builtin implies that it's part of either javascript(unityscript) or Unity. So how/why/what does the trick to ensure that it's builtin?
Array, ArrayList, List, Dictionary, LinkedList etc are all classes that at best wrap functionality to a built in array (they can be resized and host other extra methods that a linked list cannot). The reason it is "built in" is because it's native to the framework. An array should really just be an array of items, one item after another, lined up in memory. These native (built in) arrays offer faster access speed but there is so much more to data structures than access speed.
Since these classes wrap native arrays, you're indirectly accessing native arrays through function calls. These function calls might to other work than simply access the data. They can resize the array if its too small if you're adding another item to it etc. This adds (a slight) computational overhead. See also http://answers.unity3d.com/questions/32048/is-listt-as-fast-to-access-as-a-standard-array/ which is a question related to speed of arrays.
Hey Statement, are you available for consultation? check my profile and email me if so.