Chunking system causing stutters

Godot Version

4.2.1

Question

Hi all, I’m currently trying to make a chunking system for my project. Each chunk is set up as a scene, in which it does set_cell and set_cell_terrain_connect operations on procedural generation, and it does this on a higher level shared tilemap. Whenever I load the chunks, however, the main thread is blocked by the slow set_cell_terrain_connect method.

I’ve managed to remove some of this by using

await get_tree().process_frame

after each row of the chunk calculations, but this doesn’t help with the slowness of the terrain function. I’ve thought about splitting the shared tilemap into tilemaps that each chunk can share, and then do the operations before they’re added to the tree, but I don’t know how separate tilemaps in different scenes can connect via terrains.

Any help would be greatly appreciated as I’ve been stuck on this concept for a while, thanks.

You want to do your chunk loading in a background thread. You can start by reading the documentation for ResourceLoader.load_threaded_request. I have posted code for this several times, with the most recent one being in:

1 Like

Another tip for performance is to use a VisibleOnScreenNotifier2D to hide chunks that are outside of the view:

Part of the issue is that set_cell is not thread safe, however, is it ok if the chunk i’m trying to load does it’s set cell operations on it’s tilemap and THEN added to the scene?