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)
 linkedcells[i].type  =  1 

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.

  • \$\begingroup\$ Note that a stack is a last-in-first-out structure. If you want to 'pop' linked items it will be the last one first or you would have to reverse the stack entirely. Otherwise you lose all speed advantages of a stack. So stacks have their use (a gamestate manager could use a stack for example) so you need to consider the strenght and weaknesses of each structure. \$\endgroup\$ – Felsir Jun 14 '16 at 18:49
  • \$\begingroup\$ When you're just looking to destroy all the entire link, it doesn't matter which order it is in, but yeah. Queues can do it forwards as well. \$\endgroup\$ – SangoProductions Jun 15 '16 at 2:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.