GML Seeking help to to create a grid structure to select things (player, items, etc..)

B

Biddum

Guest
I am trying to think of the most efficient way to create a grid in which each square is something a player can select (using the arrow keys). The two main pieces would be the grid and then the grid selection.

Creating X amount of objects for each grid square seems inefficient and I am trying to think of a more efficient solution to create the grid. I was not able to find any video tutorials of this in GMS2.

For the selection piece, I figured I would need a separate object for that. But the part I am not certain on is how to move about the grid. I could move it X pixels and Y pixels based on the the up/down/left/right direction the player inputs, but I'm worried that moving in the amount of pixels may not be the most efficient way.

Any suggestions or tutorial links would be greatly appreciated.
grid.png
 

Alexx

Member
This can be done using just one instance of an object.
I would vary my approach depending on:
  • Will the number of selections remain static or change?
  • What do you want to happen when a selection is made?
  • Will each selection have a separate sprite?
 

PlayerOne

Member
Hmm...

If you are just using arrow keys alone to select items you could use a ds_grid / 2d array with a indicator within the grid/array that the item is currently selected.

Take this for example:

Code:
//Create Event:
global.inv = ds_grid_create(8, 2);
var inv_amount = ds_grid_width(global.inv);

for (var g = 0; g<inv_amount; g++)
{
       global.inv[# g,0]=g; // <<< Item number slot     ---  // This should iterate adding a number from 0 to 8 indicating a slot. I could be wrong though about this method.
       global.inv[# g,1]=0; // <<< Item Amount
       global.inv[# g,2]=spr_item; // <<< Item Image
       global.inv[# g,3]="item_name; // <<< Item Name
}
It's the only thing I could come up with from what you are describing.
 
B

Biddum

Guest
@Alexx
  • The number of slots visible to the player will be constant
  • I want to adapt this to a player selection screen, as well as achievements screen (player still uses arrow keys to navigate the optins, hits 'enter' to select the player or view achievement)
  • each selection will have its own sprite (on both cases)
For a player selection screen, I suppose I can create one object with several variables and give each instance its own variables definitions within the room (spr_playerBox = player1box, highlighted = false; etc...). I can then plop the objects into the room anyway I want. Then I can have another object to highlight/select the options (which I am still thinking on the best approach)
 

Alexx

Member
If you want to go down the separate object here is one method.
Create an object for each and assign the sprite.
In the Create Event for each, set a variable my_id to 1,2,3 etc for each.
In a parent Step Event do something like:
Code:
if my_id=global.selected
{
   draw a semi-transparent overlay
}
Create a control object and set global.selected to your desired starting value
Use keypress checks to change the value of global.selected, and perform desired actions.

I have a working example I can share if you'd like it.
 
B

Biddum

Guest
@Alexx thank you for your reply. If you have a working example, I'd love to check it out.

I am also going to explore @PlayerOne idea on the ds_list as well. I haven't played around with ds_lists much but it may be more flexible, if I choose to patch-in updates to the character selection or achievements.
 
Top