x


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.

more ▼

asked Dec 07 '10 at 11:10 PM

DaveA gravatar image

DaveA
26.5k 151 171 256

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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.

more ▼

answered Dec 07 '10 at 11:28 PM

skovacs1 gravatar image

skovacs1
10k 11 25 91

Thanks. My '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(?)

Dec 07 '10 at 11:37 PM DaveA

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.

Dec 07 '10 at 11:49 PM skovacs1

According to the MSDN page, List<> is not supported or no example available in JScript. Would you know the syntax for declaring that List variable (if possible)?

Dec 08 '10 at 08:29 PM DaveA

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.

Dec 08 '10 at 09:48 PM skovacs1
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Dec 07 '10 at 11:27 PM

Novodantis 1 gravatar image

Novodantis 1
1.7k 14 22 40

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?

Dec 07 '10 at 11:39 PM DaveA

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 )

Dec 08 '10 at 12:09 AM Novodantis 1

This works nicely, thanks.

Dec 08 '10 at 08:47 PM DaveA
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Dec 07 '10 at 11:18 PM

Skjalg gravatar image

Skjalg
1.4k 24 33 50

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

Dec 07 '10 at 11:24 PM DaveA
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5071
x3458
x1358
x90
x18

asked: Dec 07 '10 at 11:10 PM

Seen: 4340 times

Last Updated: Dec 07 '10 at 11:25 PM