- Home /
Resizing arrays, and what is the default array type anyway?
I need to be able to add and remove things from an array of custom structures.
In my JS file I have:
var stops : TourStop[];
where TourStop is a class containing various things. This 'stops' variable shows up in the Inspector, and I can edit it there (to initialize it).
I need to resize the array (add/remove) at runtime.
Is this a 'built-in' array? Or a JS array? Or something else?
I tried
stops.length = stops.length + 1;
But despite what the documentation says (assuming this were an Array), 'length' is read-only. So I'm not quite sure what this thing is so I don't know which API's to call to muck with it.
Answer by skovacs1 · Dec 07, 2010 at 11:28 PM
A built-in array instance : T[]
is a fast, fixed-sized array. To resize such an array, you would need to create a second array of the new size and duplicate the items from the old array over. It may show up in the inspector as its content's type is known, but only if it is a serializable type.
A "javascript" Array instance : Array
is a not-so-fast, dynamically-sized array. It does not show up in the inspector as its content's type is unspecified.
See here for more about arrays. Note that you can convert between built-in arrays and Arrays, so if you start with an array and then convert it to an Array, change the Array and then convert back, you can shorthand the process, but this is hardly the most efficient route.
If you wanted a dynamically sized array-like structure that serializes in the inspector, you want a System.Collections.Generic List of a serializable type. See here for more about serializable types.
Thanks. $$anonymous$$y 'TourStop' is a class containing serializable types (strings, ints, Vector3, etc). But it being a class, I don't suppose that itself is serializable? I suppose worst case, I could make a custom inspector editor which exposes javascript arrays(?)
If you specify the class TourStop as serializable (http://unity3d.com/support/documentation/ScriptReference/Serializable.html) by extending System.Object, then it too will be a serializable type. As far as making dynamically sized arrays, you could make a custom inspector that exposes js Arrays (or at least exposes the arrays of something that contains them), but when List already serializes, it seems like wasted effort.
According to the $$anonymous$$SDN page, List is not supported or no example available in JScript. Would you know the syntax for declaring that List variable (if possible)?
That's correct. I too know of no syntax to specify the type for generics in javascript. I believe none exists in unity's js but would be glad to learn otherwise. The solution is to create such a list in c# and if this c# script is compiled first, you can then reference the list elsewhere. If you like, you could easily put together a serializable wrapper for the generic list that js can take in. Pretty much all you would need is a class whose constructor takes in the type and stores a reference to a List to hold that type.
Answer by Novodantis 1 · Dec 07, 2010 at 11:27 PM
Using:
var stops : TourStop[];
Initialises one of Unity's builtin/.NET arrays. These cannot be resized, but are fast and exposed in the Inspector.
var stops = new Array();
Creates a more traditional (to programmers) dynamic Javascript array. You can add new values onto this using stops.Push(value), and alter its size. The downside is that they aren't available in the Inspector window.
Unity's reference asserts it is easy to convert between them to cover all requirements. Check out the full page here.
So, in theory, I could convert my built-in TourStop array to a JS array, manipulate it, then reassign it back to the 'stops' variable using the reverse conversion?
Yup, you can just convert to a dynamic array when you need to push or pop them etc. The script manual linked gives a good example of switching between them (that's neater than me trying to paste code into these kooky comment windows :P )
Answer by Skjalg · Dec 07, 2010 at 11:18 PM
You probably need to create a new array with length = oldArray.length+1 and copy over the objects inside.
What you probably want to do, is to use a List.
How would I construct the new array? Where can I find documentation on 'List' (not in Unity nor msdn (but they do have IList and ArrayList)? Thanks