godotengine / godot

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

in thread, instantiating PhysicsBody2D with a CollisionShape2D child raises error #81472

Open LingLing40Hours opened 1 year ago

LingLing40Hours commented 1 year ago

Godot version

4.1

System information

Godot v4.1.1.stable - Windows 10.0.19045 - Vulkan (Forward+) - integrated Intel(R) UHD Graphics (Intel Corporation; 31.0.101.4575) - 13th Gen Intel(R) Core(TM) i7-1370P (20 Threads)

Issue description

In thread, instantiating physicsbody2ds with a collision shape child raises a get_transform error.

According to https://docs.godotengine.org/en/stable/tutorials/performance/thread_safe_apis.html#scene-tree, this isn't the intended behavior.

Steps to reproduce

see project

Minimal reproduction project

gd_thread_instantiate.zip

bitsawer commented 1 year ago

Looks like a thread guard issue, instantiating the scene eventually calls Node2D::get_transform() which has ERR_READ_THREAD_GUARD_V() guard. Might be intentional or not.

You can work around this by calling Thread.set_thread_safety_checks_enabled(false) in the thread function, but be careful as this might not be fully supported currently.