- Home /
Passing a Multidimensional array in Javascript
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?
Answer by Mike 3 · Jul 26, 2010 at 04:20 PM
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(); }
I ultimately did something similar. After some experimentation I realized that all I had to do was declare the variable and then define it in the constructor and I could simply access the variable by passing the object around. Thanks for the response!
Answer by Eric5h5 · Jul 26, 2010 at 06:22 PM
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;
}
Answer by fenderrex · Jan 06, 2018 at 01:00 AM
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.
Your answer
Follow this Question
Related Questions
Passing Object References Through Function 0 Answers
Instantly pass other script's variable value to another. 2 Answers
c# 2D array error: need Help! 1 Answer
Projectiles passing through objects 0 Answers
Instantiate question 1 Answer