- Home /
+= doesn't work with a 2d array in JS
In a JS I am using 2d arrays. The following works just fine
infastructure[1,i]=infastructure[1,i]+desire[i]*popChange;
However, if I try this:
infastructure[1,i]+=desire[i]*popChange;
I get the following error:
Internal compiler error. See the console log for more information. output was:BCE0051: Operator '+' cannot be used with a left hand side of type 'error' and a right hand side of type 'float'.
And the console does not even seem to be able to identify where in the code the error is arising. Throwing in a debug line before the offending line shows that infastructure[1,i] is properly declared by that point and has a float value in it and that I am not going out of bounds or anything like that.
I have tried a couple different ways of declaring the array including:
var infastructure : float[,] = new float[8,10];
and
var infastructure = new float[8,10];
But no dice.
I figure that the error has to do with JS's typing of variables or something equally annoying. Obviously this isn't a game stopper as I can just avoid using += statements, but it is annoying and right now I am a bit of a loss in regards to getting the darn thing to work.
Try putting some brackets around the place, to see if you can make sure it does things in the right order.
Answer by Flynn · Oct 16, 2011 at 05:43 AM
This is a truly strange one!
Before I go any further, it is important to note that the type of array you are using is not native to JS. It's actually a Unity3D tack-on! (From C# .NET, SEE: http://msdn.microsoft.com/en-us/library/2yd9wwz4.aspx ) So, right here you may be running into issues.
This stated, it may be possible that that type of array simply does not support the += operation (It's hard to say, I've tried to stay away from using it). You may want to try float[][], as opposed to float[,]
Assuming this is not true, it seems to be treating your + and your = as seperate operators.
Try: infastructure[1,i] += desire[i]*popChange; (note that the += is surrounded by spaces) To further specify to the compiler that you are NOT using + separately from the =, you could try (infastructure[1,i]) += (desire[i]*popChange); This isolates the += from either of your statements.
Honestly, I have never played with this type of array, so it is all speculation.
Other alternatives you could consider are:
var arr = new Array();
arr.push(new Array());
Now, this next one is tricky, and I have no idea if it will work. I've long dropped JS for C#, but theoretically it could work:
var arr : ArrayList<float> = new ArrayList<float>;
You will want to import System.Collections for this to work!
Good suggestion but adding the spaces and parentheses did not help. I switched it over to a jagged array and it is all working fine. It is a bit disappointing though because I had heard that the newest version of unity actually supported real 2D arrays in Javascript. If I had realized this was still going to be an issue I would have just done it in C#.
I suppose I will probably submit a bug report unless someone else ends up posting some insight.
The array type isn't "tacked on" in JS, it's fully native. .NET and C# are two completely different things; all languages in Unity use .NET (actually $$anonymous$$ono, to be accurate). You almost never want to use the JS Array class or ArrayList. Regardless, you don't need to import System.Collections in JS. Whitespace is irrelevant to the compiler (its only use is to make things more understandable to humans), and adding parentheses to "isolate" things like that is also irrelevant.
Your answer
Follow this Question
Related Questions
Is there a decent tutorial for a local high score table (android)? 1 Answer
Error when debuging array elements name 1 Answer
Sorting arrays? 1 Answer
How to delete new'd arrays? 2 Answers