Setting Tiles During Runtime

Godot Version



I have a large 2d world that is being chunk loaded from a dictionary.

Currently, I just implemented something that loops through the data and sets each individual cell.

for layer in WORLD_DATA[CHUNK]:
			for tile in WORLD_DATA[CHUNK][layer]:
				TILE_MAP.set_cell(layer, tile, layer, WORLD_DATA[CHUNK][layer][tile])

This has extremely terrible performance though. The entire game freezes momentarily while running this operation. From my understanding it is doing an update to the tilemap (which is costly) every single time it does set_cell. I can’t seem to figure out how to solve this.

At first I looked to multithreading, but that isn’t safe as the tilemap can be edited from other sources as well (such as a player breaking or placing a block) triggered on the main thread. I could still possibly just make it so any changes that players might trigger enter into the same queue as chunk loading and just are applied alongside any chunk loading. But even if I successfully am able to implement something like that, I rather use a more efficient method to set the cells in the tilemap. But using set_cell (albeit in a different thread) is still an extremely slow method of setting the tiles up when a chunk loads/unloads.

Secondly I looked into trying to set the actual tile data for each layer (layer_0/tile_data and layer_1/tile_data properties), but I have no idea how to manipulate the data of that property to do what I want. I tried printing out the tile data to get an idea of how it is structured, but I do not understand at all. My best guess was that it is an array with the TileData object ID then an atlas coord, but there seem to be entries that are single digit after the long numbers, so as far as I know that can’t be correct, unless possibly these single numbers (always 1) mean that tile is set to nothing? That seems the most plausible, especially since when you are editing a tilemap manually in a scene it shows the tile grid overlay in a square. Regardless, even if my understanding is correct that each entry in the array is ordered pairs of TileData object + tile type, I’m not sure where I’d even begin with manipulating the data so I can set it all at once when I want to change a specific portion.