# How to Check and Destroy “linked” gems quickly efficiently

Engine: Gamemaker Studio

(simplified) Situation: I have a 6x6 board (at this moment) stored in a 2D matrix, a mechanism for linking adjacent squares, similar to Puzzlecraft, and a button that confirms the link - adding the resources, and destroying the instances of the squares, then moving existing squares and generating new ones.

Question: Is there a way to instantly go through the linked squares, and not just check for any instances marked as linked = true.

Efficiency is in all likelihood not a true concern, especially on this tiny scale, but I'd like to know for the sake of self-improvement more than anything.

Idea for Solving it: I've thought of a way that may be fairly lightweight and quick, I don't know if it is though:

Once a square is added to the link, put the square's position in the matrix in a separate array (one which has 36 slots, in case the entire board is linked). Then once the button to confirm is pressed, it goes up from the bottom of the array, until it reaches a null or -1 value - destroying and setting the value of this array at the appropriate position to null or -1.

Question on the idea: Would this work? How would I store the square's position in the matrix in this array? Would I need to make the array 2D so that it can hold both the x and y position of the square [ie. 0 to 5]?

Your own suggestion sounds exactly what I would do. Note that you are not limited to store just ints or strings in an array. Make an array of objects: Basically you create a game cell object that holds information you would need like position, type and so on.

This cell object is then added to the 1D array:

 linkedcells[i]  =  instance_create(x,y,obj_cell)

Stacks would actually, in all likelihood, be easier (and faster, so I've heard) than an array. Also, it's got really useful tools like clear which is great when you just want to negate the entire link.
Well, it would be if simply clearing the internal link was all you needed to mark everything as "not linked". You'd probably still want to go 1 by 1 and by using the pop function for stacks, so you can do all the appropriate stuff...but it's still more convenient than having to track and compare to say the Link_Length, and then count up to / down from that number.