SOLVED How to Colorize ds_grid_disk ?

noorsalim

Member
I'm making a Turn-Based Strategy (TBS for short) game and want to use ds_grid to store data of the grid and manipulate it later.

Here is the basic code to create the grid and populate it with oNode objects:

GML:
///@desc Create the grid
global.grid = ds_grid_create(room_width/GRID_SIZE, room_height/GRID_SIZE);
global.map = undefined;

//create global.map objects, positioned neatly within the grid------------------------------------------
for(xx = 0; xx < ds_grid_width(global.grid); xx += 1){
    for(yy = 0; yy < ds_grid_height(global.grid); yy += 1){
            global.map[xx, yy] = instance_create_depth(xx * GRID_SIZE, yy * GRID_SIZE, 3, oNode);
            global.map[xx, yy].gridX = xx; //store and set global.map instance's x position
            global.map[xx, yy].gridY = yy; //store and set global.map instance's x position
    }

}
Reading the manual, I know that there are built-in functions that allow us to get and set a circular radius of the grid data, as demonstrated in the image below:

ds_grid_disk.png

What I want to achieve is, with my code above, to change the color of my oNode objects (stored in an array variable) to different color by utilizing this built-in disk shaped grid creation? Let's say I want to put this in global left mouse event, for the sake of discussion. So whenever the player tap left mouse button, the specified oNode objects will change it's color to yellow (currently it's white, see image below)

Here is how my game room looks like:


room_game.png

the white rectangle is the oNode object. This image below is what I (roughly) want to achieve for the moment:

room_game.png

Let me know if any of you guys need further explanation of my project to help answering my question!
--------------------------------------------------------------------------------------------------------------------------------------------------------
UPDATE 21 APR 2020 [SOLVED]

For the sake of you who may have the same problem, I've finally resolved this issue! Thanks to all of you replying to my post, really helpful!

Here is the screenshot of what I've achieved (actual one, not an edited image!):

working!.png

Here is the code that I use in the Create event:

Code:
///@desc Set the grid
global.grid = ds_grid_create(room_width/GRID_SIZE, room_height/GRID_SIZE);
ds_grid_clear(global.grid, 0); //set all grid value to zero

//set disk value to 5
ds_grid_set_disk(global.grid, 10, 10, 3, 5);
Here is the code that I use in the draw event:

GML:
for(xx = 0; xx < ds_grid_width(global.grid); xx += 1){
    for(yy = 0; yy < ds_grid_height(global.grid); yy += 1){          
           
        //set color and draw rectangle with specified outline color
        draw_set_colour(c_yellow);
        draw_rectangle(xx * GRID_SIZE, yy * GRID_SIZE, (xx + 1) * GRID_SIZE, (yy + 1) * GRID_SIZE, true);
           
        //draw disk with green color
        if(global.grid[# xx,yy] == 5){          
            draw_set_colour(c_lime);
            draw_rectangle(xx * GRID_SIZE, yy * GRID_SIZE, (xx + 1) * GRID_SIZE, (yy + 1) * GRID_SIZE, false);
        }
       
    }

}
 
Last edited:

MD_Wade

Member
Are you drawing your grid at any point? I suppose you could do something like this:

GML:
for (var xx = 0; xx < ds_grid_width(grid); xx ++)    {
    for (var yy = 0; yy < ds_grid_height(grid); yy ++)    {
        switch (grid[# xx, yy])    {
            case 1:
                draw_set_colour(c_yellow);
                draw_rectangle(xx * GRID_SIZE, yy * GRID_SIZE, (xx + 1) * GRID_SIZE, (yy + 1) * GRID_SIZE, false);
            break;
        }
    }
}
 

noorsalim

Member
Are you drawing your grid at any point? I suppose you could do something like this:

GML:
for (var xx = 0; xx < ds_grid_width(grid); xx ++)    {
    for (var yy = 0; yy < ds_grid_height(grid); yy ++)    {
        switch (grid[# xx, yy])    {
            case 1:
                draw_set_colour(c_yellow);
                draw_rectangle(xx * GRID_SIZE, yy * GRID_SIZE, (xx + 1) * GRID_SIZE, (yy + 1) * GRID_SIZE, false);
            break;
        }
    }
}
I have drawn the grid in the draw event using draw_rectangle instead of objects, but still not successful in drawing the desired 'disk' and turn their color to yellow. will try several things and probably report again later.
 
Last edited:

sp202

Member
I suppose if each grid cell is an object, you could set its image_blend to a particular colour if the cell it occupies has a particular value. However I think drawing the grid is a better solution than having so many objects active.
 

sylvain_l

Member
not sure I understand the question but,
you could just filled the grid with the colors, for example default would be c_white
and then you add your yellow circle with
ds_grid_set_disk(4,4,3,c_yellow)
 

noorsalim

Member
not sure I understand the question but,
you could just filled the grid with the colors, for example default would be c_white
and then you add your yellow circle with
ds_grid_set_disk(4,4,3,c_yellow)
This actually sounds simple. I assume I need to put this in Draw event since it's about drawing things inside the specified coordinate? Definitely worth trying later.

Have a time to try this today. It was unsuccessful, probably because the ds_grid_set_disk function store the value of c_yellow instead of drawing a yellow grid, even though I've put that in draw event. probably what's happening is, the game store integer form of c_yellow inside the specified grid data.
 
Last edited:

noorsalim

Member
I suppose if each grid cell is an object, you could set its image_blend to a particular colour if the cell it occupies has a particular value. However I think drawing the grid is a better solution than having so many objects active.
Thank you for your suggestion, based on replies above, I'm starting to think drawing the grid is a better solution instead of spawning many objects. I am relatively new to coding and totally noob in GML so I don't have much thought when trying to do this or that in GameMaker 2, which is why any suggestions are welcome! My main goal is to make informed decisions later on.
 
Top