godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
90.48k stars 21.07k forks source link

Building GridMap in a Thread may miss cells' both rendering and phsic body #63106

Closed LiBooks closed 2 years ago

LiBooks commented 2 years ago

Godot version

Godot 4.0 alpha 12

System information

Windows10 Vulkan

Issue description

When I use GridMap in a Thread and use its “set_cell_item” function , I find that some cells cannot be set and it will also affect its octave. This is different from the bug I submitted before, because the missing cell not only has no rendering, but also has no physical body .And the loss of these cells occurs when gridmap runs “set_cell_item” function , but the previous bug occurred during the movement of players.

I checked the source code of gridmap , and I think the problem is between “set_cell_item” function and “_octant_update” function .This is because when using GridMap to create a world , the thread often executes the "set_cell_item "function dozens or even more times, which will continuously write information to the "Octant" and "cell_map" . However, this part is only an operation on data , the code that really involves rendering and physical systems is executed by the main thread, which means that when the main thread works, other threads will still keep writing data to gridmap .In addition, the order of writing is to write into “Octant” first and then “cell_map”. QQ截图20220717185457 QQ截图20220717190230

The above figure shows the code executed by the main thread. You can see that octant is accessed first, and then cell map。 However, due to the asynchronous execution of the thread, another thread may not have written to the cell map , and finally an error occurs in this place. Besides ,the previous code has erased the rendering and physical body of the cell in octant.

My suggestion is to change the order of the last few lines of "set_cell_item" function in "grid_map.cpp" file like the following figure.

%QOCF54L501{9B B4RO@T)J

Steps to reproduce

The project uses gridmap to create the same world repeatedly, but by observing, you will find that some cells and octants in this world will disappear。

Minimal reproduction project

GridMap_BUG.zip

LiBooks commented 2 years ago

My suggestion is wrong and I will try to find the reason .

Calinou commented 2 years ago

Duplicate of https://github.com/godotengine/godot/issues/42917. Thanks for posting your investigation nonetheless :slightly_smiling_face:

LiBooks commented 2 years ago

archived

I

Duplicate of #42917. Thanks for posting your investigation nonetheless 🙂

I have thought of a way to solve this problem and tested it. I will submit the code later.