godotengine / godot

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

Creating GridMaps from secondary thread throws octant_update errors #68597

Open SdgGames opened 1 year ago

SdgGames commented 1 year ago

Godot version

v4.0.beta4.official [e6751549c]

System information

Windows Server 2022, Vulcan

Issue description

Creating a GridMap from a secondary thread throws errors and eventually causes a crash. This issue is in Godot 4, so I don't think that it's related to 42917

The GridMap is not a child of the scene tree when its contents are being modified. The following error is emitted while calling gridmap.set_cell_item

E 0:00:02:0592   _octant_update: Condition "!cell_map.has(E)" is true. Continuing.
  <C++ Source>   modules/gridmap/grid_map.cpp:610 @ _octant_update()

Additionally, the game will randomly crash at times.

Steps to reproduce

Start a secondary thread. From the secondary thread, create a GridMap. Do not add as a child of the scene yet. Call set_cell_item() multiple times to populate the GridMap. Errors will randomly appear, and the game might crash.

Using Reproduction Project: Pressing the "Start" button will cause errors to show up in the debugging output Pressing the "Start" button multiple times in will eventually cause the game to crash.

Minimal reproduction project

GridMap.zip

Laserology commented 1 week ago

A temporary fix exists by adding the items on the main thread:

YourGridMap.call_deferred("set_cell_item", Vector3i(X, Y, Z), ID)

This won't cause any noticeable slowdown on the main thread, as it is only adding the block. The other code that surrounds it will still be running on their own thread.