GMS 2 How to call array in array?

Discussion in 'Programming' started by Luxxum, Mar 27, 2017.

Tags:
  1. Luxxum

    Luxxum Member

    Joined:
    Dec 22, 2016
    Posts:
    17
    Hey, I was trying to make a global array by using:


    global.arrayname = [ [11,12,13], [21,22,23], [31,32,33] ]


    and gamemaker doesn't seem to mind it, but when I try to call it via:


    variable = global.arrayname[variable2[real_number]]


    it isn't working. Could you tell me what I'm doing wrong?

    the exact error message is:
    -----------------------------------
    FATAL ERROR in
    action number 1
    of Create Event
    for object moving_block_vertical_basic:

    trying to index a variable which is not an array
    at gml_Object_moving_block_vertical_basic_Create_0 (line 18) - yspeed = global.vblock[vblockn[2]]
    ----------------------------------



    Thanks
     
  2. Bingdom

    Bingdom Googledom

    Joined:
    Jul 1, 2016
    Posts:
    1,678
    You should be able to.

    If you don't know how to read errors, I recommend you check the manual.

    It looks like you have accidently placed an 'n'.
     
  3. Luxxum

    Luxxum Member

    Joined:
    Dec 22, 2016
    Posts:
    17
    Ok, ill look through it. Thanks!
     
  4. CMAllen

    CMAllen Member

    Joined:
    Mar 2, 2017
    Posts:
    855
    Do you need to deal with nested array structures? Can you not simply get the nested array's specific reference number and work with the array directly?

    ex:
    Code:
    temp_array = global.arrayname[variable2]
    varialbe = temp_array[real_number]
     
  5. Luxxum

    Luxxum Member

    Joined:
    Dec 22, 2016
    Posts:
    17
    No, i'm getting a different error now:

    FATAL ERROR in
    action number 1
    of Create Event
    for object moving_block_vertical_basic:

    Push :: Execution Error - Variable Index [0,3] out of range [1,3] - -1.vai(100036,3)
    at gml_Object_moving_block_vertical_basic_Create_0 (line 20) - yheight = vai[3]


    I think I am misunderstanding how arrays work in GM2. I've only really used matlab before this, so I probably am making some false assumptions.

    Thanks though!
     
  6. salyossy

    salyossy Member

    Joined:
    Aug 10, 2019
    Posts:
    28
    it worked for me! thanks to your solution, now i can use nested arrays
     
  7. RefresherTowel

    RefresherTowel Member

    Joined:
    Jul 13, 2016
    Posts:
    1,212
    Your initial error was a misunderstanding of arrays. To get access to the data contained in an array position, you have to supply a number, so global.vblock[vblockn[2]] will not work (I mean, it could if vblockn[2] returns a number in this scenario, but I doubt that is what you were intending). CMAllen has given the correct answer, first you have to pull the inner array out of the position it is stored in inside the outer array. So if you want the inner array stored in position 0 in the outer array, you would go:
    Code:
    var temp_arr = global.vblock[0];
    // Now the temp_arr variable holds whatever array was stored in global.vblock[0]
    var my_var = temp_arr[2] // This pulls whatever is stored in position 2 from the array stored in position 0 in global.vblock
    
    The error you are getting after trying to do vai[3] is actually caused because the vai array is not 4 indices long (0, 1, 2, 3). Remember all arrays/lists/etc start at position 0. So if you have 3 pieces of data stored in the vai array, they would be stored at position 0, 1 and 2. It's likely that you actually want to access the third thing you added to the vai array, which you would get by calling vai[2], since it would go vai[0], vai[1] and then vai[2] for the third piece of data.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice