• 2D
  • Tile issue: set_cell() clears the cell instead of changing it

Hello, I'm just trying to replace a tile on the map by another one from the tileset. I use set_cell() for that but it doesn't change the tile, it just erases it. I use this simple script attached to my tilemap node:

func _process(delta):
	var tile = Vector2(3, 0)
	set_cell(tile.x, tile.y, 35)
	update_dirty_quadrants()

What I want to do is change the 3rd cell on the 1st row of the map by the 35th tile in the tileset (a water tile). And it gives this result:

Can anyone help me with that? I've been struggling for a while and read the doc, it seems to be a problem with the tileset or I don't know.

Welcome to the forums @Hexabin!

Do you have a tile with a index of 35? If I recall correctly, Godot will place a empty/blank tile either if the tile index is -1 (no tile) or if it cannot find a tile with the passed-in ID. Based on the result you are getting, I think the issue may be that there is a not a tile with an index of 35 in the tileset you are using. Maybe try using a different number?

Another thing you could do is export the variable for quick testing of tile indexes:

export (int) var tile_number = 35
func _process(delta):
	var tile = Vector2(3, 0)
	set_cell(tile.x, tile.y, tile_number)
	update_dirty_quadrants()

Then you can select the tilemap and change the value in the inspector, and it should change the tile shown in the game (without needing to restart! You may need to save the scene each time though)

@TwistedTwigleg said: ! > Welcome to the forums @Hexabin! ! > ! > Do you have a tile with a index of 35? If I recall correctly, Godot will place a empty/blank tile either if the tile index is -1 (no tile) or if it cannot find a tile with the passed-in ID. Based on the result you are getting, I think the issue may be that there is a not a tile with an index of 35 in the tileset you are using. Maybe try using a different number? ! > ! > Another thing you could do is export the variable for quick testing of tile indexes: ! > ! > export (int) var tile_number = 35 ! > func _process(delta): ! > var tile = Vector2(3, 0) ! > set_cell(tile.x, tile.y, tile_number) ! > update_dirty_quadrants() ! > ! > Then you can select the tilemap and change the value in the inspector, and it should change the tile shown in the game (without needing to restart! You may need to save the scene each time though)

Thanks for your answer and for welcoming me! I tried many numbers but the result is the same, except with the number 0, I just found it out. It displays the 1st tile in the tileset correctly. So, there is something wrong with the tileset I think, I will try a different one maybe... I tried different tile modes by the way, single, autotile and atlas, but this doesn't change anything.

Edit: I kind of solved the problem, I found what was wrong anyway, the tile_number (35) was indicating the atlas and not the tile number. I can't find any way to use other modes though, only atlas mode works for me. I used a trick by using a tilemap imported from Tiled Map Editor (by using the great add-on Tiled Importer), I don't how this add-on works but it doesn't use atlas mode or anything, it manages to give a number to the tiles. The method set_cell() works now, it changes a tile with the tile number I ask. The problem is not really solved actually, but if someone has the same problem as me, Tiled Importer can help you.

2 months later

It's not very clear in the documentation, but the coordinates of the tile within your tile atlas are actually a different argument in set_cell. So instead of:

set_cell(x, y, tile_number)

you would use (assuming your tile atlas has id=0, which it sounds like it does):

set_cell(x, y, 0, false, false, false, tile_atlas_coords)