Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411469 Posts in 69368 Topics- by 58422 Members - Latest Member: daffodil_dev

April 23, 2024, 02:13:06 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)[SOLVED]Game Maker storing ds_grid coordinates in an object?
Pages: [1]
Print
Author Topic: [SOLVED]Game Maker storing ds_grid coordinates in an object?  (Read 5228 times)
denzgd
Level 1
*



View Profile WWW
« on: April 30, 2016, 05:22:14 PM »

Hello again, friends!

Today, I'm working on a Match 3 battle puzzle game with hexagonal tiles. Right now, the method I'm using to manage the tile layout is by randomly-generating values which correspond to a tile type (of which there are five, that do different things), and storing them in a ds_grid. From there, in the Create Event of an object, I execute a script which parses the ds_grid and places tiles in the room at their intended location, one by one. By this, I mean I actually have it go through each of the 60+ spots on the ds_grid, individually. Basically 60+ instances of
Code:
instance_create();

I'm not sure how to do that more efficiently at the moment.

However my main concern right now is being able to store a given tile's ds_grid coordinates in it so that the parser I'm working on can compare it with the active tile being dragged around. My goal is to do a collision check between the active tile, and the location where the player will drop it. Tiles can be moved like a queen in chess, along three axes, as far as the player wishes to move it. Then, the surrounding tiles part and shift, to accommodate the impending movement, and then the tile is dropped into place, thus securing its new ds_grid coordinates.

Any help regarding this is much appreciated!
« Last Edit: May 02, 2016, 07:05:13 PM by denzgd » Logged

ProgramGamer
Administrator
Level 10
******


aka Mireille


View Profile
« Reply #1 on: April 30, 2016, 05:44:38 PM »

How about storing the tile type in the objects and putting the instance ID of the objects in the ds_grid? That way every attribute of a tile can be checked by giving an instance ID to a function, and moving tiles around is easy. Does this solve your problem?
Logged

denzgd
Level 1
*



View Profile WWW
« Reply #2 on: May 01, 2016, 10:01:32 AM »

How about storing the tile type in the objects and putting the instance ID of the objects in the ds_grid? That way every attribute of a tile can be checked by giving an instance ID to a function, and moving tiles around is easy. Does this solve your problem?

I like this idea, but I'm not sure how I'd get the objects to store their ID in the ds_grid in the correct format, after they are spawned when the room starts.
Logged

ProgramGamer
Administrator
Level 10
******


aka Mireille


View Profile
« Reply #3 on: May 01, 2016, 12:11:15 PM »

Don't create the objects first, create the ds_grid, fill it with the tile values and then loop through it again, taking the values and putting them in a newly created object, storing the instance ID in the spot where the original value was in the grid.

If you want to make specific layouts by placing objects in the room editor on top of this, just create some dummy objects for each tile type and make the script which puts values in the grid look for those dummies' positions.
Logged

Razz
Level 6
*


subtle shitposter


View Profile WWW
« Reply #4 on: May 02, 2016, 03:41:01 PM »

instance_create actually returns the id of the object created. what this means is that you can do something like this:
var = instance_create();

now var will always return the instance id of that object (and create it at the same time). with this information you can actually create objects, and give them unique variables independent of their create event.

here's an example:
var obj;
obj = instance_create();
obj.ds_grid_x = *x position in ds_grid*

does this help? with this you could easily set reference variables in each tile as you parse through the ds_grid creating them.
Logged

denzgd
Level 1
*



View Profile WWW
« Reply #5 on: May 02, 2016, 06:51:00 PM »

instance_create actually returns the id of the object created. what this means is that you can do something like this:
var = instance_create();

now var will always return the instance id of that object (and create it at the same time). with this information you can actually create objects, and give them unique variables independent of their create event.

here's an example:
var obj;
obj = instance_create();
obj.ds_grid_x = *x position in ds_grid*

does this help? with this you could easily set reference variables in each tile as you parse through the ds_grid creating them.

Hold on, let me make sure I've got this right. I can still use the instance_create() function like normal, right?

Code:
var obj;
obj = instance_create(x,y,obj_puzzle_tile);
obj.ds_grid_x = *x position in ds_grid*;

And then it stores the x axis location in the newly-created puzzle piece?

Edit:

I actually tried it out, and it worked like a charm! Far as I'm concerned, this thread is solved!
« Last Edit: May 02, 2016, 07:04:59 PM by denzgd » Logged

JWK5
Guest
« Reply #6 on: May 03, 2016, 01:55:04 AM »

There is another approach which may be faster or more convenient in some cases (depending on how you're using it)...

In whatever object you are creating and storing your ds_grid in put the following code (I tend to have an object called "main" to store databases and whatnot in it so I can just use "main.whatever" to access them):



---------------------------------------------------------------------------------------------------
var W,H,TGX,TGY;
//W and H are temporary variables and will get disposed of after the script runs and ends (as defined by "var").
//You can use TGX and TGY to move around the entire grid of tile objects so that you can easily place them all
//wherever you want in the room. W=Width, H=Height,TGX=Tile Grid X, TGY=Tile Grid Y

W
H
TGX = 0;
TGY = 0;

//TILEOBJECT is your tile object you will be putting in the grid.

//Rename YOURDSGRID whatever you want.
YOURDSGRID = ds_grid_create();

//Now to quickly fill your ds_grid with tile object IDs and place them in the room tiled at the same time...

//This for loop creates the grid rows
for (W=0; W<60; W+=1)
{
     //This for loop creates the grid columns
     for (H=0; H<60;H+=1)
     {
         //Let's say your tiles are 32x32 pixels...
          //This bit here "x+(W*32)" and here "y+(H*32)" allow you to create
          //and place your tiles in a grid pattern in the room as they
          //are added to the ds_grid.

          ds_grid_add(YOURDSGRID,W,H) = instance_create(TGX+(W*32),TGY+(H*32),TILEOBJECT);
     }
}
---------------------------------------------------------------------------------------------------

Sans comments:

var W,H,TGX,TGY;
TGX = 0;
TGY = 0;

YOURDSGRID = ds_grid_create();

for (W=0; W<60; W+=1)
{
     for (H=0; H<60;H+=1)
     {
          ds_grid_add(YOURDSGRID,W,H) = instance_create(TGX+(W*32),TGY+(H*32),TILEOBJECT);
     }
}




Once created, all you need to do to access whatever object is:
ds_grid_get(YOURDSGRID,x,y);

So, let's say you wanted the tile object's X value:
ds_grid_get(YOURDSGRID,x,y).x

To get an object's position in the DS grid (if it has one) use ds_grid_value_x() or ds_grid_value_y(), so let's say you wanted to find the X (room position) of a character object stored inthe ds_grid but you don't know its position in the ds_grid:


var W,H;

W = ds_grid_value_x(YOURDSGRID,0,0,59,59TILEOBJECT.id);
H = ds_grid_value_x(YOURDSGRID,0,0,59,59,TILEOBJECT.id);

ds_grid_get(YOURDSGRID,W,H).x


...or just:

ds_grid_get(YOURDSGRID,ds_grid_value_x(YOURDSGRID,0,0,59,59TILEOBJECT.id),ds_grid_value_x(YOURDSGRID,0,0,59,59,TILEOBJECT.id)).x


In whatever object you used to create the ds_grid, in it's DESTROY event add the following (so you can free up the memory):
ds_grid_destroy(YOURDSGRID);
« Last Edit: May 03, 2016, 02:19:20 AM by JWK5 » Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic