Godot Version
4.2.1
Question
Hi all. I’ve been hitting a brick wall on this problem - I haven’t found any solution within Godot since the end of last year when I started working on this project, and I’m at my wits’ end.
To summarize, I’m making a chunking system for a procedurally generated overworld. The tiles are set on top of perlin noise values. The issue I’m facing is the performance of setting the tiles in chunks. I’m using set_cell_terrain_connect(), as my world is made up of various terrains. Here is the current state of loading chunks via this function:
As you can see, there is a bad stutter when using the terrain. The grass/sand is done via set_cell at the minute. I have tried splitting it up across frames as you can see, which is only 50 calls in a frame, and the frames will drop. Doing the same with the set_cell_terrain_connect function array is even worse:
Clearly, the main thread is being blocked. So what have I tried to do to mitigate this beyond the above? Firstly, I tried implementing threads. I realize that altering the scene tree on threads is not safe, so what I thought to do instead was to give every chunk IT’S OWN tilemap, and then do all the calculations, including the set_cell operations, on the tilemap which isn’t on the tree just yet. Then, I proceeded to call_defer the adding of the chunk to the scene_tree (as I also know that doing this on a thread is not safe). Here’s the result:
As you can, the offloading of the chunk to the thread works perfectly with no stuttering… until it crashes. The docs state that ‘Interacting with the active scene tree is NOT thread-safe.’, which I thought was fine, as I’m not interacting the with the active scene tree at all, I’m setting cells on something which wasn’t been added yet. However, the docs also mention that
Instancing nodes that render anything in 2D or 3D (such as Sprite) is not thread-safe by default. To make rendering thread-safe, set the Rendering > Driver > Thread Model project setting to Multi-Threaded.
This implies that this method shouldn’t be used either - even if it’s not affecting the scene tree from the thread. Setting the rendering Thread Model to Multi-Threaded made no difference either.
So what options are left? Is this a complete impasse as far as TileMaps are concerned? I’m reluctant to progress with the rest of my project at all, given the state of this basic feature that many published games include.
Any help would be greatly appreciated, as I do not want to drop this as it is a passion project and something I’ve wanted to do for a long time. Thank you in advance.