Search Unity

Strange Array problem

Discussion in 'iOS and tvOS' started by felixk, Dec 8, 2010.

  1. felixk

    felixk

    Joined:
    Dec 17, 2008
    Posts:
    38
    Hi,

    I have a script that populates an Array in Start():

    Code (csharp):
    1. private var letters = new Array();
    2.  
    3. letters[0] = "A";
    4. letters[1] = "B";
    5. letters[2] = "C";
    6. letters[3] = "D";
    7. letters[4] = "E";
    8. letters[5] = "F";
    9. letters[6] = "G";
    10. letters[7] = "H";
    11. letters[8] = "I";
    12. letters[9] = "J";
    13. letters[10] = "K";
    14. letters[11] = "L";
    15. letters[12] = "M";
    16. letters[13] = "N";
    17. letters[14] = "O";
    18. letters[15] = "P";
    19. letters[16] = "Q";
    20. letters[17] = "R";
    21. letters[18] = "S";
    22. letters[19] = "T";
    23. letters[20] = "U";
    24. letters[21] = "V";
    25. letters[22] = "W";
    26. letters[23] = "X";
    27. letters[24] = "Y";
    28. letters[25] = "Z";
    29. letters[26] = "*";
    In start() i do the following:

    Code (csharp):
    1.  
    2. letter1 = PlayerPrefs.GetInt("letter1");
    3. letter2 = PlayerPrefs.GetInt("letter2");
    4. letter3 = PlayerPrefs.GetInt("letter3");
    5.    
    6. Debug.Log("Letter 1: " + letter1);
    7. Debug.Log("Letter 1: " + letters[letter1]); //Dies on this line
    8.  
    This runs fine in the Editor, but when I run this code on the iPhone i get the following:

    Code (csharp):
    1. ExecutionEngineException: Attempting to JIT compile method '(wrapper dynamic-method) Boo.Lang.Runtime.RuntimeServices:RuntimeServices$op_Addition$System.String$System.String (object,object[])' while running with --aot-only.
    2.  
    3.   at System.Delegate.CreateDelegate (System.Type type, System.Object firstArgument, System.Reflection.MethodInfo method, Boolean throwOnBindFailure) [0x00000] in <filename unknown>:0
    4.   at System.Delegate.CreateDelegate (System.Type type, System.Reflection.MethodInfo method, Boolean throwOnBindFailure) [0x00000] in <filename unknown>:0
    5.   at System.Delegate.CreateDelegate (System.Type type, System.Reflection.MethodInfo method) [0x00000] in <filename unknown>:0
    6.   at System.Reflection.Emit.DynamicMethod.CreateDelegate (System.Type delegateType) [0x00000] in <filename unknown>:0
    7.   at Boo.Lang.Runtime.DynamicDispatching.Emitters.DispatcherEmitter.CreateMethodDispatcher () [0x00000] in <filename unknown>:0
    8.   at Boo.Lang.Runtime.DynamicDispatching.Emitters.DispatcherEmitter.Emit () [0x00000] in <filename unknown>:0
    9.   at Boo.Lang.Runtime.DynamicDispatching.MethodDispatcherFactory.EmitMethodDispatcher (Boo.Lang.Runtime.CandidateMethod found, System.Type[] argumentTypes) [0x00000] in <filename unknown>:0
    10.   at Boo.Lang.Runtime.DynamicDispatching.MethodDispatcherFactory.Create () [0x00000] in <filename unknown>:0
    11.   at Boo.Lang.Runtime.RuntimeServices.DoCreateMethodDispatcher (System.Object target, System.Type targetType, System.String name, System.Object[] args) [0x00000] in <filename unknown>:0
    12.   at Boo.Lang.Runtime.RuntimeServices.CreateMethodDispatcher (System.Object target, System.String name, System.Object[] args) [0x00000] in <filename unknown>:0
    13.   at Boo.Lang.Runtime.RuntimeServices+<Invoke>c__AnonStorey13.<>m__7 () [0x00000] in <filename unknown>:0
    14.   at Boo.Lang.Runtime.DynamicDispatching.DispatcherCache.Get (Boo.Lang.Runtime.DynamicDispatching.DispatcherKey key, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) [0x00000] in <filename unknown>:0
    15.   at Boo.Lang.Runtime.RuntimeServices.Dispatch (System.Object target, System.String cacheKeyName, System.Type[] cacheKeyTypes, System.Object[] args, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) [0x00000] in <filename unknown>:0
    16.   at Boo.Lang.Runtime.RuntimeServices.Dispatch (System.Object target, System.String cacheKeyName, System.Object[] args, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) [0x00000] in <filename unknown>:0
    17.   at Boo.Lang.Runtime.RuntimeServices.Invoke (System.Object target, System.String name, System.Object[] args) [0x00000] in <filename unknown>:0
    18.   at Boo.Lang.Runtime.RuntimeServices.InvokeRuntimeServicesOperator (System.String operatorName, System.Object[] args) [0x00000] in <filename unknown>:0
    19.   at Boo.Lang.Runtime.RuntimeServices.InvokeBinaryOperator (System.String operatorName, System.Object lhs, System.Object rhs) [0x00000] in <filename unknown>:0
    20.   at InitialsConfigScript.Start () [0x00000] in <filename unknown>:0
    This happens on the second debug.log line. letter1=25, and letters[letter1] is where it shows this error.

    Anyone have any idea what I'm doing wrong?

    Thanks
    Felix
     
  2. felixk

    felixk

    Joined:
    Dec 17, 2008
    Posts:
    38
    So I went ahead and made this change:

    private var letters : String[] = new String[27];

    Now it works fine!


    What's the deal?
     
  3. defjr

    defjr

    Joined:
    Apr 27, 2009
    Posts:
    436
    Using the Array class you would add elements with:

    letters.Push("A");
    letters.Push("B");
    etc
     
  4. felixk

    felixk

    Joined:
    Dec 17, 2008
    Posts:
    38
    This code has always worked for me in the past. Then I upgraded to the latest Unity and it stopped working.

    Plus the docs say that you can set Arrays that way as well:

    From
    Code (csharp):
    1. function Start () {
    2.     var arr = new Array ();
    3.  
    4.     // Add one element
    5.     arr.Push ("Hello");
    6.    
    7.     // print the first element ("Hello")
    8.     print(arr[0]);
    9.  
    10.     // Resize the array
    11.     arr.length = 2;
    12.     // Assign "World" to the second element
    13.     arr[1] = "World";
    14.    
    15.     // iterate through the array
    16.     for (var value : String in arr) {
    17.         print(value);
    18.     }
    19. }
    The strangest part is that it works in the Editor, but not in iOS.
     
  5. defjr

    defjr

    Joined:
    Apr 27, 2009
    Posts:
    436
    To make the first bit of code you posted work, add this line between the variable declaration and where you start assigning values:

    letters.length = 27;
     
  6. marjan

    marjan

    Joined:
    Jun 6, 2009
    Posts:
    563
    You get this error because you are trying to combine a string with an untyped value from an array. Unity iOS doesn' like this any more. To get around that you either need to use the internal typed arrays, As you already did. Or use (myarray[0] as String).

    Indeed your attempt would work in unity 1.5. Since 1.6 it became for some reason more and more strict, using JavaScript. I had to figure this the painful Way too.
     
    Last edited: Dec 8, 2010
  7. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    Dare I ask why you're filling an array with the alphabet in the first place?
     
  8. felixk

    felixk

    Joined:
    Dec 17, 2008
    Posts:
    38
    Thanks Marjan. Makes sense now.

    Daniel: Because i'm lazy, and luckily i'm the only one who maintains this code.
     
  9. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    It's perfectly legitimate to add elements like felixk's code was originally doing. i.e.,

    Code (csharp):
    1. private var letters = new Array();
    2.  
    3. letters[0] = "A";
    4. letters[1] = "B";
    --Eric