FIXED - Array/Grid problem - array_set changing values for all arrays in the grid

Iskardes

Member
Hi,

I'm having some trouble with randomizing the first entry in an array which is stored in a grid. I think I've setup the grid and list correctly and have tried to run some code in the room start event to randomize the values in a for loop. The 3x2 grid has an array in each cell and the first entry in the array tells the Draw function what to output.

The code partially works but seems to be setting all 6 cells to the same value rather than setting it for the individual cell. I've tried to debug the issue and look at what accessors I'm using but I can't stop all the arrays being changed!

Create Event: DS setup
GML:
// DS that holds information about how the current board is laid out
// Each cells holds an array? [PERS_STYLE, 0/1 to rep current choice array]

enum PB_HOR { LEFT, MIDDLE, RIGHT, COUNT}
enum PB_VERT { TOP, BOTTOM, COUNT }

ds_persuasion_board = ds_grid_create(PB_HOR.COUNT, PB_VERT.COUNT);
ds_grid_clear(ds_persuasion_board, [-1, -1]);

// DS listing possible persuasion choices

ds_pers_list = ds_list_create();

ds_list_add(ds_pers_list, PERS_STYLES.INTIMIDATE, PERS_STYLES.ENCOURAGE, PERS_STYLES.PETITION,
PERS_STYLES.DEMAND, PERS_STYLES.EMPATHISE, PERS_STYLES.RATIONALE);
GML:
    randomize();
    // Create a random ordered list of pers types
    ds_list_shuffle(ds_pers_list);
    // Apply the first two on that list to positions on the persuasion board
    
    for (var i = 0; i < ds_list_size(ds_pers_list); i++)
    {
        show_debug_message("--------" + string(i) + "---------");
        
        var _gx = i;
        var _gy = 0;
        
        if (_gx > 2)
        {
            _gx -= 3
            _gy += 1;
        }
        
        show_debug_message("_gx: " + string(_gx));
        show_debug_message("_gy: " + string(_gy));
        
        
        show_debug_message("Grid pos: " + string(_gx) + "," + string(_gy));
        show_debug_message("Value to set: " + string(ds_pers_list[| i]));
        var _array = ds_persuasion_board[# _gx, _gy];
        var _value = ds_pers_list[| i];
        
        show_debug_message("Current value in " + string(_gx) + "," + string(_gy) +": " + string(array_get(_array,0)));
        array_set(ds_persuasion_board[# _gx, _gy], 0, ds_pers_list[| i]);
        
    }
I've included some images below to show the problem I'm having. The draw event code is definitely working properly as all the information in the arrays is the same, I did some show_debug_message() work and the hints that the values are being overwritten for all the cells each time the for loop runs.

Any help would be greatly appreciated! Thanks in advance :)



arraydebug.png
ABOVE: All entry [0] in the array are set to 2.


arrayproblem.png
ABOVE: Output to screen - All boxes should contain a different word, not the same.

debug.png
ABOVE: Current values in the grid before being set match up with the value that WAS set in the previous iteration of the for loop.
 

Roldy

Member
The 3x2 grid has an array in each cell
No it does not.

Arrays are passed by reference:

GML:
ds_grid_clear(ds_persuasion_board, [-1, -1]);

// The above line is the same as the following

var _anArray = [-1, -1];  // _anArray is a reference
ds_grid_clear(ds_persuasion_board, _anArray);  // This copies the reference (not the array) into each cell

// The above places a reference to the same array in every grid cell
As you can see all the cells in your grid have the same value: 2B80E80. i.e. they all reference the same array

1627578707147.png

You need to loop through your grid and put a unique array in each cell.

GML:
for (var _i = 0; _i < PB_HOR.COUNT; _i++) {
    for (var _j = 0; _j < PB_HOR.COUNT; _j++) {
  
     
         ds_persuasion_board [# _i, _j] = [-1, -1];
    }
}
 
Last edited:

Iskardes

Member
No it does not.

Arrays are passed by reference:

GML:
ds_grid_clear(ds_persuasion_board, [-1, -1]);

// The above line is the same as the following

var _anArray = [-1, -1];  // _anArray is a reference
ds_grid_clear(ds_persuasion_board, _anArray);  // This copies the reference (not the array) into each cell

// The above places a reference to the same array in every grid cell
As you can see all the cells in your grid have the same value: 2B80E80. i.e. they all reference the same array

View attachment 41740

You need to loop through your grid and put a unique array in each cell.

GML:
for (var _i = 0; _i < PB_HOR.COUNT; _i++) {
    for (var _j = 0; _j < PB_HOR.COUNT; _j++) {
 
    
         ds_persuasion_board [# _i, _j] = [-1, -1];
    }
}

Ohh, you absolute genius! That's worked! I'll remember that for next time, I thought I was going crazy but looking in the wrong place 🤦‍♂️ Thank you so much!
 
Top