SOLVED Question about clearing all tiles

Rexzqy

Member
Hi,

So I want to clear all tiles on a tilemap.

This is in the create event of the object that puts tiles on the map:
GML:
global.ground_tilemap_wood =layer_tilemap_create("Instances",0,0,ti_mud,room_width,room_height);

for (i =0;i<105;i+=1)
{
    for (j=0;j<=130;j+=1)
    {
        if position_meeting(i*100,j*100,obj_wall_no_ground_spawn) = false
        {
            var ran_data=irandom_range(1,100);
            tilemap_set_at_pixel(global.ground_tilemap_wood,ran_data,100*i,100*j)
        }
    }
}
This is in the step event:
Code:
if season != obj_daycycle.season
{
tilemap_clear(global.ground_tilemap_wood,0);
layer_tilemap_destroy(global.ground_tilemap_wood);
season = obj_daycycle.season;
}
However it is not working and the tiles are still there. Any help is greatly appreciated!

EDIT: I am such a idiot, I forgot to put the stuff in create event. Fixed it now
 
Last edited:

trip

Member
Have you any texture above or below this tilemap?
when no then you must clear Iam not sure maybe application layer?

example:
Room with 1 asset layer nothing more.
Draw sprite
Destroy sprite
Sprite still visible because no texture can redraw sprite.

try this in draw begin event after you clear tilemap or maybe in pre-draw (before drawing other texture).
draw_clear_alpha(c_black, 1);
 
Last edited:

rIKmAN

Member
What's stored in global.ground_tilemap_wood?

You need to use the tilemap element ID returned from layer_tilemap_create() if the tile layer was created in code or layer_tilemap_get_id() if it was created in the IDE.

If it's a layer in the IDE try using tilemap_clear(layer_tilemap_get_id("TileLayer"), 0) and replace "TileLayer" with the name of your own actual tile layer.

That code works here, but it'd be better to cache the result of layer_tilemap_get_id() into a variable to reuse elsewhere and not have to call it every time you need to access the tilemap layer.
 

Rexzqy

Member
What's stored in global.ground_tilemap_wood?

You need to use the tilemap element ID returned from layer_tilemap_create() if the tile layer was created in code or layer_tilemap_get_id() if it was created in the IDE.

If it's a layer in the IDE try using tilemap_clear(layer_tilemap_get_id("TileLayer"), 0) and replace "TileLayer" with the name of your own actual tile layer.

That code works here, but it'd be better to cache the result of layer_tilemap_get_id() into a variable to reuse elsewhere and not have to call it every time you need to access the tilemap layer.
I am so sorry, I forgot to put the stuff in create when I posted this, the code I posted is actually in step event. I fixed it now, could u take a look now please? Thank you so much!
 

TheouAegis

Member
global.ground_tilemap_wood =layer_tilemap_create("Instances",0,0,ti_mud,room_width,room_height);
should technically be 105,130 instead of room_width,room_height.

So you have 100 tiles (or more) in your tileset and each tile is 100x100 pixels in size?

Have you debugged to make sure season and global.season are both changing separately? In other words, have you debugged the conditional itself?
 

Rexzqy

Member
should technically be 105,130 instead of room_width,room_height.

So you have 100 tiles (or more) in your tileset and each tile is 100x100 pixels in size?

Have you debugged to make sure season and global.season are both changing separately? In other words, have you debugged the conditional itself?
I changed the room width and room height to 105 and 130, ty!!
There's 100 tiles and each tile is actually 64*64(I am not sure if it not being 100*100 affects it?), and I changed it so season is completely irrelevant for now.

GML:
if keyboard_check_released(vk_f1)
{
    tilemap_clear(global.ground_tilemap_wood,0);
    layer_tilemap_destroy(global.ground_tilemap_wood);
}
It's still not working D:

EDIT: When I delete the tile right after I create them in the create, it somehow works:
GML:
global.ground_tilemap_wood =layer_tilemap_create("Instances",0,0,ti_mud,105,130);

for (i =0;i<105;i+=1)
{
    for (j=0;j<=130;j+=1)
    {
        if position_meeting(i*100,j*100,obj_wall_no_ground_spawn) = false
        {
            var ran_data=irandom_range(1,100);
            tilemap_set_at_pixel(global.ground_tilemap_wood,ran_data,100*i,100*j)
        }
    }
}

tilemap_clear(global.ground_tilemap_wood,0);
layer_tilemap_destroy(global.ground_tilemap_wood);
When I put the delete code in step, it no longer works for some reason. I am confused
 
Last edited:

Rexzqy

Member
We can get to that in a sec. But if your tiles are 64x64, why are you multiplying i and j by 100?
Bc the map is 10500 * 13000, these 2 arguments are the x pos and y pos in the room according to the manual

EDIT: basically what the code is doing is randomly picking and placing tiles into the room
 
Last edited:

TailBit

Member
When I delete the tile right after I create them in the create, it somehow works:
If you destroy a tile and there is no backgroun behind it, then the old drawing of it will still exist on the application surface as @trip told in the 2nd post here, did you try clearing the drawing area?
 

trip

Member
Your room width and height is not correct for subtract 64 I thing this is first problem (If you want tilemap across whole the room)
change the map size to 10496(164 tile) and 13056 (204 tile) or other tile width*64 and tile height*64

tell me:
Is tilemap_clear and layer_tilemap_destroy realy executed after you release F1? use breakpoint
Have you other layer with texture in the room? ( it can be a black background layer )
Is your code to create layer_tilemap called only 1?

GML:
//place the macro anywhere in start code and you can use this anywhere in your code for easy changing.
#macro TILE_SIZE 64
#macro TILE_SHIFT 6
var _tile_width = room_width>>TILE_SHIFT
var _tile_height = room_height>>TILE_SHIFT
var _x,_y

global.ground_tilemap_wood =layer_tilemap_create("Instances",0,0,ti_mud,_tile_width,_tile_height)

for (_x=0; _x<_tile_width; _x+=1){
    for (_y=0; _y<_tile_height; _y+=1){
        // I dont understand this function
        //if position_meeting(_x*100,_y*100,obj_wall_no_ground_spawn) = false{
        //    var ran_data=irandom_range(1,100);
       //     tilemap_set_at_pixel(global.ground_tilemap_wood,ran_data,100*_x,100*_y)
       // }

      //when you want random tile on background use this:
      tilemap_set(global.ground_tilemap_wood,irandom_range(1,100),_x,_y) // maybe 99
    }
}
 
Last edited:

Rexzqy

Member
Your room width and height is not correct for subtract 64 I thing this is first problem (If you want tilemap across whole the room)
change the map size to 10496(164 tile) and 13056 (204 tile) or other tile width*64 and tile height*64

tell me:
Is tilemap_clear and layer_tilemap_destroy realy executed after you release F1? use breakpoint
Have you other layer with texture in the room? ( it can be a black background layer )
Is your code to create layer_tilemap called only 1?

GML:
//place the macro anywhere in start code and you can use this anywhere in your code for easy changing.
#macro TILE_SIZE 64
#macro TILE_SHIFT 6
var _tile_width = room_width>>TILE_SHIFT
var _tile_height = room_height>>TILE_SHIFT
var _x,_y

global.ground_tilemap_wood =layer_tilemap_create("Instances",0,0,ti_mud,_tile_width,_tile_height)

for (_x=0; _x<_tile_width; _x+=1){
    for (_y=0; _y<_tile_height; _y+=1){
        // I dont understand this function
        //if position_meeting(_x*100,_y*100,obj_wall_no_ground_spawn) = false{
        //    var ran_data=irandom_range(1,100);
       //     tilemap_set_at_pixel(global.ground_tilemap_wood,ran_data,100*_x,100*_y)
       // }

      //when you want random tile on background use this:
      tile_map_set(global.ground_tilemap_wood,irandom_range(1,100),_x,_y) // maybe 99
    }
}
Thx for the reply!
1. i put a debug message in the bracket and it ran, so i think they did run
2. there are 2 other tiles generated similarly
3. yes, this is only called once in this create event

EDIT: tried your code, it's not working and I believe my code works for at least spreading the tiles all over the room.
 
Last edited:

Rexzqy

Member
If you destroy a tile and there is no backgroun behind it, then the old drawing of it will still exist on the application surface as @trip told in the 2nd post here, did you try clearing the drawing area?
i believe there's another tile layer that has higher depth so it should be behind it..? Let me test a bit more, ty!
 

Rexzqy

Member
Ah, fixed it. I am such an idiot, forgot I generated another tile using the same global var. I am so sorry and thank yall so much for your time!
 
Top