GMS 2 Referencing arrays within arrays

Discussion in 'Programming' started by David Chen, Jul 21, 2019.

  1. David Chen

    David Chen Member

    Joined:
    Jul 20, 2016
    Posts:
    12
    Say I had an array in a loop:

    for (ii=0;ii<5;ii++){
    msg[ii]=array1;
    }

    where global.msg would be a 1D array like: array1= ["hello", "world","more text!"]
    How would you reference specifically array1[0] in the loops?
    You can't do msg[ii][0] as this syntax doesn't work in GMS:2 but this is what I'm trying to get at. Is there an alternative?
     
  2. RefresherTowel

    RefresherTowel Member

    Joined:
    Jul 13, 2016
    Posts:
    1,212
    Extract the internal array first:
    Code:
    var internal_array = external_array[0];
    var value = internal_array[0];
    
     
  3. David Chen

    David Chen Member

    Joined:
    Jul 20, 2016
    Posts:
    12
    You can't do this if it's all inside a loop though


    Code:
    for (i=0;i<5;i++) {
    var internal_array = external_array[0];
    var value = internal_array[i];
    }
    
    [/QUOTE]

    that doesn't work^
     
  4. RefresherTowel

    RefresherTowel Member

    Joined:
    Jul 13, 2016
    Posts:
    1,212
    Uhh, yes it does?
    Code:
    for (var i=0;i<5;i++) {
      var internal_array = external_array[i];
      var value = internal_array[0];
    }
    
    If you want to loop through the outer array and grab slot 0 from each array stored in the outer array, the above code is how you would do it. If you wanted to loop through the internal array stored at slot 0 in the outer array, you would do what you posted above this message. I'm not sure why you think a loop changes this? What specifically doesn't work and what specifically are you trying to do?
     
  5. Annoyed Grunt

    Annoyed Grunt Member

    Joined:
    Jun 20, 2016
    Posts:
    136
    Why wouldn't it work? The problem here is that you can't chain accessors, so you just have to access the arrays separately.

    Code:
    msg[ii][0]
    
    Becomes

    Code:
    var inner_array = msg[ii],
    value = inner_array[0];
    
     
  6. David Chen

    David Chen Member

    Joined:
    Jul 20, 2016
    Posts:
    12
    i
    i'm using:
    Code:
    ///Step
    for(i=0;i<t;i++) {
        var internal_array=current_msg[?i];
        show_debug_message(internal_array);
        var msg=internal_array[n[i]];
    
    i'm getting

    ___________________________________________
    ############################################################################################
    FATAL ERROR in
    action number 1
    of Step Event0
    for object obj_textbox_m:

    trying to index a variable which is not an array
    at gml_Object_obj_textbox_m_Step_0 (line 4) - var msg=internal_array[n];
    ############################################################################################
    --------------------------------------------------------------------------------------------
    stack frame is
    gml_Object_obj_textbox_m_Step_0 (line 4)


    the debug message is returning an array:
    { { It's an Imbued Heart! }, }

    accessing array chains separately doesn't work inside loops??

    tl;dr the problem is how to get something like msg[0][1] to work
     
    Last edited: Jul 21, 2019
  7. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,541
    It's absolutely annoying how you seem to be intentionally misreading everyone's instructions on purpose.

    When you do chained accessors like msg[0][1] in languages like Javascript, first it retrieves msg[0], then subindexes [1] off the result of that. Once you know how the expression is evaluated, it's easy to emulate it in a language the doesn't chain the same way. This is the reason why you don't just take syntax for granted, as a programmer you have to know the entire process of how an expression is evaluated.

    Now for all of your failed attempts:
    This doesn't work because your code is accessing external_array[0][i] instead of external_array[i][0].
    Your code in this post is complete nonsense and shows no effort in reading our replies. You are treating current_msg like a map instead of an array by using the map accessor, and you are accessing n[i] which has not been declared. If your intention is to access internal_array[0][0], then it would have been this:
    Code:
    var msg_array = internal_array[0];
    var msg = msg_array[0];
    Like everyone else has said before:
    Code:
    var inner_array = msg[0];
    value = inner_array[1];
    
    And if you still don't get it, use this workaround.

    Also, real chained accessors are slated for the end of the year, so this solution wouldn't be needed for long.
     
    Anixias and rIKmAN like this.
  8. chamaeleon

    chamaeleon Member

    Joined:
    Jun 21, 2016
    Posts:
    975
    Is n an array, given your code snippet
    Code:
    internal_array[n[i]]
    
    Doesn't seem to make sense given i is already used to get internal_array.
     

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