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);
|