copying arrays in javascript

Does anyone have any suggestions on how to copy arrays of Vector3’s so that the arrays are separate objects in Unity’s javascript?

var otherVecArray : Vector3[ ] = firstVecArray;

There’s nothing fancy about it really, just the above. When you’re dealing with number arrays like Vector3, you can just set the other holder equal to the first one to copy it.

that does not copy the array.
that only creates an additional reference to the array.

Usually in javascript that is accomplished by using the slice() method. However it isn’t mentioned in the documentation so might not be available to the unityscript array class.

unity is no web javascript, its only a javascript alike language. as such browser js features don’t exist, you use .NET functions to achieve it

Since “var otherVecArray : Vector3[ ] = firstVecArray;” does not copy arrays, as mentioned, you can use System.Array.Copy.

–Eric

Really? It never seemed to act like that for me… I guess the easiest way is to just do this, then:

otherArray = new Vector3[firstArray.length];
var temp : int = 0;
while (temp < firstArray.length) {
   otherArray[temp].x = firstArray[temp].x;
   otherArray[temp].y = firstArray[temp].y;
   otherArray[temp].z = firstArray[temp].z;
   temp += 1;
}

Edit: Or the above function. Heh!

It always does, that’s how references work.

var foo = new int[3];
foo[2] = 99;
var bar = foo;
foo[2] = 1;
print (bar[2]); // prints "1", because bar is a reference, not a copy

–Eric

I guess I just never tested that functionality in the arrays. After all, when you’ve got one array of values, why make a duplicate? Heh.

Because the routine you’re coding needs a duplicate, and you don’t want to change values in the duplicate and have the original be affected? There’s only about 50 zillion reasons why you’d need this, which is why System.Array.Copy exists in the first place…they didn’t code that just to take up space… :wink:

–Eric

Eric5h5:

Thanks for the pointer to System.Array.Copy. I’d googled the slice() method for web javascript but couldn’t find slice() in Unity’s docs.

Can you point me to the documentation for System.Array.Copy? I’m having some trouble finding it.

MSDN docs along with the rest of .net, or you can use the Mono docs, although they were kind of incomplete in some areas last time I checked.

–Eric

Is System.Array.Copy a C# construct?

I’m looking for how to copy arrays in Unity’s javascript.

Is there a comparable approach to System.Array.Copy to use for copying arrays in javascript?

Unity’s javascript is not web javascript. You need to be really clear on that. It uses the .Net API, but uses the Mono 1.2.5 implementation of that, which basically translates into .Net 2.0.

You can use MSDN for looking up information on how to use different functions for data structures.

To give you a start on where to look in there, here is a link to their Array.Copy method.

The most important thing to take away from this though is that Unity’s JS is only JS in syntax, not in implementation.

System.Array.Copy. :wink: That has nothing to do with C#, it’s just .net/Mono, which is what Unity is based on, and what all 3 languages use.

–Eric

I think I’m starting to understand System.Array.Copy, but I’m having trouble getting the length of the array to use for the copying. I’ve tried using System.Array.GetLength and System.Array.Length with no luck. I also saw a reference in the forums to System.Array.Count, but that didn’t work either. I’ve got the following:

    var lineArray : Array = new Array();
    var copyLineArray : System.Array = [];
    var systemArray : System.Array = lineArray.ToBuiltin(Vector3);
    var lineArrayLength : System.Int32 = System.Array.Length(systemArray);
    System.Array.Copy(systemArray, copyLineArray, lineArraylLength);

and get the compilation error:

An instance of type System.Array is required to access non static member ‘Length’.

I’ve also tried:

    var lineArrayLength : System.Int32 = lineArray.length;

which compiles OK , but I get the runtime error:

Argument Exception: length
System.Array.Copy (System.Array sourcearray, System.Array destinationArray, int32 length);

I’ve also tried

var lineArrayLength : System.Int32 = 2;

and I’ve gotten the same runtime error: Argument Exception: length

What form of length should I used in System.Array.Copy?

var originalArray = [Vector3.one, Vector3.forward, Vector3.up];
var copyArray = new Vector3[originalArray.Length];
System.Array.Copy(originalArray, copyArray, originalArray.Length);

–Eric