-
-
Notifications
You must be signed in to change notification settings - Fork 21.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Creating GridMaps from secondary thread throws octant_update errors #68597
Comments
GridMap creation and access is not thread safe due to [0] and [1]. However blocks can still be generated on a separate thread and placed on the main thread. [0] godotengine/godot#42917 [1] godotengine/godot#68597
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. |
My use case is a bit different and instead of trying to set GridMap cells in a separate thread I am instantiating a whole scene in a separate thread that contains several GridMaps (since I use each GridMap as a layer) among other nodes. I guess when you do so internally Godot is doing set_cell_item operations or similar to build GridMaps contained by the scene and it throws the exact same error as reported in the initial post in this issue. So the fix from Laserology is not fully solving this for me. |
I personally do not have an issue with doing this, are you sure you aren't doing a cross-thread operation somewhere? |
You are totally right @Laserology Thank you for looking into this! I was about to completely change strategy for loading gridmap based levels in my game but thanks to your comment I looked into this more. In case it is helpful for anyone, I attach the simple project created that allows to test this. |
I looked into this more and run more tests. The issue is indeed when instantiating the scene that contains GridMaps in a thread that is not the main thread. But it seems that the issue is related with the size of the GridMap and it won't fail with small GridMaps. Large GridMaps are anyways giving Godot a hard time as it crashes the editor often by just moving the GridMap around in the scene with this error:
This is kind of random since sometimes I move it to were I want without a crash while other times it crashes. So I might just be using GridMaps that are larger than the intended purpose of the engine. But it is a shame since other than those crashes in the editor or the problem I am reporting here with instancing in a secondary thread, the engine handles the rendering of those large GridMaps without issues in-game and with a decent performance. To reproduce this without all the complexity of my main project I created a simple project that contains a very simple setup to test this. There you will find these options if you click in the main node: By default it will try to load a scene with a large gridmap (a 128x128x128 gridmap full of solid cells) and in my case it does not load it correctly. Now the way this fails seems to vary in each run:
However, if I choose the "Single Thread" option in that Main node, then it will always always load correctly. When the scene loads you can move around using AWSD and the mouse (mouse wheel to change speed). I might be doing something wrong with the way I load from a thread as I am more in the noob side for game development and Godot. The code is quite small so it should be easy to review. Example of single thread load: Example of threaded load (see the missing cells, but sometimes it is much less obvious than this): |
Godot version
v4.0.beta4.official [e675154]
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
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
The text was updated successfully, but these errors were encountered: