I have constructed multidimensional array in javascript using the MultiDim.cs script. I would now like to be able to pass the array to a function in another class without having to break it down to a single dimensional array and then rebuild it in the other class. What would be the proper typing to insert into the "function something( multiDimArray : Type )" in order to pass the array, if it's possible?
I would make a script containing the below:
class MultiDimContainer
{
var multiDim;
function MultiDimContainer(originalMulti)
{
multiDim = originalMulti;
}
function Get()
{
return multiDim;
}
/* //this setter function is optional, for changing the array in the container after creation
function Set(newMultiDim)
{
multiDim = newMultiDim;
}
*/
}
What you would then do is something like this to use it:
//calling site
var multiDimContainer = new MultiDimContainer(yourMultiDimArray);
YourFunction(multiDimContainer);
//the called function
function YourFunction(multiDimContainer : MultiDimContainer)
{
//do something with MultiDimContainer.Get();
}
Edit: this is outdated in Unity 3.2. You can just use 2D arrays in JS now.
You can't declare the correct type in this case, which is why the MultiDim workaround exists in the first place. You can leave out the type, which makes a dynamic variable. This variable itself won't work with multi-dimensional syntax, but you can declare another local variable in the function using MultiDim in order to get the correct type using type inference, then assign the passed variable to that. This works because arrays are passed by reference not value. (Why yes, that is an annoying hack. ;) Proper multi-dimensional array support in JS would be nice, eh?)
function Something (multiDimArray) {
var realArray = new MultiDim.IntArray(0, 0); // This is just to get "realArray" to be the correct type, the dimensions don't matter and might as well be 0
realArray = multiDimArray;
}
How BCE0005: Unknown identifier: ‘MultiDim’ affected my workflow. so I find out you need a script for MultiDim. the script is “outdated” and in 404 land “slow clap for unity everyone”. I find that apparently its because you can pass a multidimensional array of type float but not int? However, I haven’t been able to pass this in a function… it’s sad how they made their own javascript engine. I was trying to port over PCA to unity so I could sort items in 3D space for binary search.
- Sources.