Grid of Maps Question

Hi All,

I am working on a project that involves populating a ds_grid (jigsaw puzzle) with a number of maps (which contain info about the tabs and slots of the puzzle pieces). Ideally, the grid and map would be initialized, and then nested for loops iterate through the x and y coordinates, checking the values of each spot in the ds_map, which then gets saved to the grid, and the map is reset when the loop...loops.

The trouble that I am having is that whenever I change a value in the map (currentpiece[? "North"]) it changes that value ("North") for every spot in the grid. This throws a wrench in my code, as it stands now. I have no doubt it would work perfectly well with a different approach, but I am curious why the grid isn't behaving like I'd expect.

This is the create event which initializes a couple variables used in the script:

Code:
//ivars
tabs = ds_map_create()

tabs[? "North"] = 9
tabs[? "South"] = 9
tabs[? "East"] = 9
tabs[? "West"] = 9
directions = ["North","South","East","West"]
...and this is the step event:

Code:
xspaces = obj_puzzle.linecountx+1
yspaces = obj_puzzle.linecounty+1



//if you click on the start button
//if(position_meeting(mouse_x,mouse_y,self)&&mouse_check_button_released(mb_any))
if(keyboard_check_released(vk_space))
{
    scr_generatepuzzle()
}
...finally, this is the script scr_generatepuzzle:
(This is the part which seems to be causing issues.)

GML:
function scr_generatepuzzle(){
    
    
//creates a new grid, then sets all of the cells to the "empty" tabs map
puzzlegrid = ds_grid_create(xspaces,yspaces)
//ds_grid_add(puzzlegrid,all,all,tabs)
//calculates the number of unique junctions based on the puzzle divisions
uniquejunctions = 4+3*((xspaces-2)+(yspaces-2))+2*((xspaces-2)*(yspaces-2))
//creates and populates a list with 1 or 0 to indicate slot/tab for each UNIQUE junction (-1 for edges)
junctionvals = ds_list_create()
    
    
    
//currentpiece = puzzlegrid[# 0,0]
//northpiece = puzzlegrid[# 0,0]
//westpiece = puzzlegrid[# 0,0]
//nextpiece = puzzlegrid[# 0,0]
    
    
for( var count = 0; count<uniquejunctions; count++)
{
    junctionvals[| count] = irandom(1)
}
    
for (var i = 0; i < xspaces; ++i)
{
    for (var j = 0; j < yspaces; ++j)
    {
            
            
            
        //this is probably working correctly, but the value is being saved to every location at the same time
        //currentpiece = ds_grid_get(puzzlegrid,i,j)
        //nextpiece = ds_grid_get(puzzlegrid,i,j+1)
            
        currentpiece = tabs
        
        
            
            
        //show_message("currentpiece = "+string(currentpiece[? "North"])+string(currentpiece[? "South"])+string(currentpiece[? "East"])+string(currentpiece[? "West"]))
        //show_message("nextpiece = "+string(nextpiece[? "North"])+string(nextpiece[? "South"])+string(nextpiece[? "East"])+string(nextpiece[? "West"]))
            
            
            
        if(j!=0)
        {
        northpiece = ds_grid_get(puzzlegrid,i,j-1)
        }
        if(i!=0)
        {
        westpiece = ds_grid_get(puzzlegrid,i-1,j)
        }
            
            
            
        //removes the tabs/slots from the edges
        if(i==0)
        {
            currentpiece[? "West"] = -1
        }
        if(i == xspaces-1)
        {
            currentpiece[? "East"] = -1
        }
        if(j==0)
        {
            currentpiece[? "North"] = -1
        }
        if(j = yspaces-1)
        {
            currentpiece[? "South"] = -1
        }
            
        //show_message("i = "+string(i)+"\nj = "+string(j)+"\nNorth = "+string(currentpiece[? "North"])+"\nSouth = "+string(currentpiece[? "South"])+"\nEast = "+string(currentpiece[? "East"])+"\nWest = "+string(currentpiece[? "West"]))
        
        //nextpiece = ds_grid_get(puzzlegrid,i,j+1)
        //show_message("nextpiece = "+string(nextpiece[? "North"])+string(nextpiece[? "South"])+string(nextpiece[? "East"])+string(nextpiece[? "West"]))
                
        for(var dirs = 0; dirs<4; dirs++)
        {
            var currentdir = directions[dirs]
                
                
            if(currentpiece[? currentdir]==9)
            {
                //If the current direction is north, reference the piece to the north's "South" direction and then change the value
                if(dirs==0)
                {
                    currentpiece[? "North"] = northpiece[? "South"]
                    if(currentpiece[? currentdir]==0)
                    {
                        currentpiece[? currentdir] = 1
                    }
                    else
                    {
                        currentpiece[? currentdir] = 0
                    }
                }
                //same thing for south (I don't think I can use Booleans here, since I need three options)
                if(dirs==3)
                {
                    currentpiece[? currentdir] = westpiece[? "East"]
                    if(currentpiece[? currentdir]==0)
                    {
                        currentpiece[? currentdir] = 1
                    }
                    else
                    {
                        currentpiece[? currentdir] = 0
                    }
                }
                else
                {
                    //places the top value from junctionvals into the current direction tab
                    currentpiece[? currentdir] = junctionvals[| 0]
                    //removes the top value from the list
                    ds_list_delete(junctionvals,0)
                }
                    
                    
                    
            }
                
        }
            
            
            
        ds_grid_add(puzzlegrid,i,j,currentpiece)
        
        ds_map_destroy(currentpiece)
            
        
    //show_message("Post Save"+"\ni = "+string(i)+"\nj = "+string(j)+"\nNorth = "+string(currentpiece[? "North"])+"\nSouth = "+string(currentpiece[? "South"])+"\nEast = "+string(currentpiece[? "East"])+"\nWest = "+string(currentpiece[? "West"]))
            
    
        
        
    }
}   
}
(Please pardon the formatting/commented out messages :) )
 

TheouAegis

Member
Taking a guess, isn't currentpiece still just a reference back to tabs in the end? You didn't copy tabs, you simply saved its address to currentpiece, so any change you make to currentpiece affects tabs.
 
I just had an epiphany leading me to the same conclusion. I'll try using arrays instead of maps (since I don't STRICTLY need to use them here, I was just trying to learn more about them) and perhaps that will work better.
 
Top