GMS 2 Double List not correctly storing information

Discussion in 'Programming' started by Sardec Xavier, May 23, 2019.

  1. Sardec Xavier

    Sardec Xavier Member

    Joined:
    Oct 27, 2017
    Posts:
    14
    I am trying to save controls and call them back when the game is loaded from an ini file. the controls are set and stored inside the second array correctly, but when accessing them later on, the second array was not set correctly and is just still 0. for instance at global.controlList[0, 5] at the start is set to a number (the index of the button the game uses) however, later on when accessing the same point it returns 0. I cannot figure out why
    Code:
    ///Set up the game
    randomize(); // Give me true random
    //Set up main menu control for later calls
    global.primary_control = 0;
    //Set up player controls lists
    var buttonTotal = 12;
    global.controlList = array_create(4)
    global.controlList[0] = array_create(buttonTotal);
    global.controlList[1] = array_create(buttonTotal);
    global.controlList[2] = array_create(buttonTotal);
    global.controlList[3] = array_create(buttonTotal);
    controlNames = script_execute( scr_control_names, buttonTotal );
    //Check for a load file for previously saved controls
    ini_open("control.sav");
    //For every player...
    for ( var controlId = 0; controlId < 4; controlId++ )
    {
       var _controlList = global.controlList[controlId];
       //Go through every button
       for( var loc = 0; loc < buttonTotal; loc++ )
       {
           // Read in the right controls from the button ID off the save file, if they don't exist, it will give the value of the buttone from a default conrol scheme
           var buttonId = controlNames[loc];
           var readButton = ini_read_real( "Player: " + string(controlId), buttonId, scr_default_controls( loc ) )
           show_debug_message( buttonId + ": " + string(readButton) );
           _controlList[loc] = readButton;
           ini_write_real( "Player:" + string(controlId), buttonId, readButton );
       }
    }
    //Glose the save file that is open
    ini_close();
    //Goto the main menu room
    room_goto( rm_title_screen );
    
    scr_control_names
    Code:
    // Define the keys inside the Ini file
    
    var arr = array_create( argument0 );
    
    arr[0] = "Shoot";        // Shoot = RT
    arr[1] = "Ledge";        // LedgeHang = RB 
    arr[2] = "Toggle";        // Toggle = Y
    arr[3] = "Interact";    // Interact = X
    arr[4] = "Jump";        // Jump = A
    arr[5] = "Move_Left";    // Move Left = LS
    arr[6] = "Move_Right";    // Move Right = LS
    arr[7] = "Aim_U";        // Aim Up = LS
    arr[8] = "Aim_D";        // Aim Down = LS
    arr[9] = "Sprint";        // Sprint = LSB
    arr[10] = "Aim_DU";        // Aim Diup = LT
    arr[11] = "Aim_DD";        // Aim Didown = LB
    
    return arr;
    
    scr_default_controls
    Code:
    ///Default control scheme for the game
    var defaultList = array_create(12);
    
    defaultList[0] = gp_shoulderrb;        // Shoot = RT
    defaultList[1] = gp_shoulderr;        // LedgeHang = RB 
    defaultList[2] = gp_face4;            // Toggle = Y
    defaultList[3] = gp_face3;            // Interact = X
    defaultList[4] = gp_face1;            // Jump = A
    defaultList[5] = gp_axislh;            // Move Left = LS
    defaultList[6] = gp_axislh;            // Move Right = LS
    defaultList[7] = gp_axislv;            // Aim Up = LS
    defaultList[8] = gp_axislv;            // Aim Down = LS
    defaultList[9] = gp_stickl;            // Sprint = LSB
    defaultList[10] = gp_shoulderl;        // Aim Diup = LT
    defaultList[11] = gp_shoulderlb;    // Aim Didown = LB
    
    //Set Controls
    return defaultList[ argument0 ];
    
    
     
  2. Bořek Tuleja

    Bořek Tuleja Member

    Joined:
    Nov 20, 2018
    Posts:
    11
    This scripts returns just one value, don't you want to return the entire array?
     
  3. Sardec Xavier

    Sardec Xavier Member

    Joined:
    Oct 27, 2017
    Posts:
    14
    No, because when you are looping through, you are assigning each index in the double array (saved controls) to be only one of the default buttons. Therefore, you only want to return the correct button index.
     
    Last edited: May 23, 2019
  4. samspade

    samspade Member

    Joined:
    Feb 26, 2017
    Posts:
    1,965
    Couple questions:
    • Have you walked through the first block of code (which would include the two scripts) in the debugger and checked each value at each point to make sure it is working as expected?
    • Have you looked at the ini directly - to see what is being saved and loaded?
    • You say: global.controlList[0, 5] but your global.controlList is a 1d array not a 2d. What does this mean?
    • You say: later on when accessing the same point it returns 0. What does that code look like?
    I did look through the code and it mostly seemed good. Although I might have missed something (and with saving you've added an extra layer of hidden information) but the rest or your description makes it seems like the code you posted isn't the problem. That the problem occurs somewhere else.
     
  5. Sardec Xavier

    Sardec Xavier Member

    Joined:
    Oct 27, 2017
    Posts:
    14
    So sam a few things:
    1) global.controlList is a 1d array or 1d arrays, it is another way of creating a 2d array
    2) I am not really good at going back through the debugger / how to use it.
    3) Later on, the way it accessed is the same way I used to define it. Such as:
    Code:
    _controlList = global.controlList[ player ];
    show_debug_message( string(_controlList[5] );
    
    this occurs when the user is attempting to control the player, and I use the _controlList[ index ];
    to get a button that I can use as an input for the player. this code should return a number that relates to Left stick on a controller. However, it does not and returns a 0;
    Code:
    key_fire = gamepad_button_check_pressed(player,_controlList[0]);  // Shoot = RT
    
    That should set the RT from the control list to the button input used in the rest of the code
     
  6. TsukaYuriko

    TsukaYuriko Q&A Spawn Camper Forum Staff Moderator

    Joined:
    Apr 21, 2016
    Posts:
    1,536
    Use the debugger to step through your code and verify that all variables have the value you expect them to have backwards. If _controlList[5] doesn't contain the value you expect, go up one step in the ladder and check _controlList, player and global.controlList.

    Also, I'd highly advise against naming a variable list when it holds an array. Those terms are not usable interchangeably as lists are an entirely separate data structure, so it may be confusing to yourself or other developers.
     
  7. samspade

    samspade Member

    Joined:
    Feb 26, 2017
    Posts:
    1,965
    Since the error occurs later on, have you verified that there is anything wrong with the code you posted?
    What does the ini file save and load at this point?

    Overall, I think this would be a good time to learn to use the debugger.
     
  8. Sardec Xavier

    Sardec Xavier Member

    Joined:
    Oct 27, 2017
    Posts:
    14
    The values are defined correctly and moved into controlList correctly. However, when accessed later they are not able to find anything inside the values. Is it possible because _controlList[0] is defined as a local variable, that it is not able to store values into it. Is it not like a pointer in C++?
     
  9. Sardec Xavier

    Sardec Xavier Member

    Joined:
    Oct 27, 2017
    Posts:
    14
    So in the beginning, yes the values are retrieved correctly, but they are not set in the array of global.controlList[0] at all. So they are retrieved but not stored in _controlList[loc].
     
  10. Sardec Xavier

    Sardec Xavier Member

    Joined:
    Oct 27, 2017
    Posts:
    14
    I got it, just had to use a different method of accessing differently
     
  11. GMWolf

    GMWolf aka fel666

    Joined:
    Jun 21, 2016
    Posts:
    3,360
    Hmm I don't expect this would work.
    I think they did make changes to how 2D arrays work recently, but historically, they where just a 1D array, with special indexing.

    I would recommend you get you variables out in two steps. First get the array in the array, then the value I'm that 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