godotengine / godot-docs

Godot Engine official documentation
https://docs.godotengine.org
Other
3.87k stars 3.16k forks source link

When can the physics space be locked? #8420

Open synalice opened 11 months ago

synalice commented 11 months ago

Your Godot version:

4.1.1

Issue description:

Tutorial page about Ray-casting says this:

_"Godot physics runs by default in the same thread as game logic, but may be set to run on a separate thread to work more efficiently. Due to this, the only time accessing space is safe is during the Node._physics_process() callback. Accessing it from outside this function may result in an error due to space being locked"_.

I think it would be best if there was some clarification about what exactly causes this lock and what's the best way to use ray-casting outside of physics_process.

URL to the documentation page:

https://docs.godotengine.org/en/stable/tutorials/physics/ray-casting.html

ratkingsminion commented 10 months ago

I would also like a way to find out if the physics direct state is currently locked; or maybe if the last emitted signal was SceneTree.physics_frame and not SceneTree.process_frame. This way one could write code that works (more or less) safely in different projects without having to make sure it's called at the right place.

AThousandShips commented 10 months ago

This is not directly available on the server in c++ but is an internal implementation detail, and isn't very useful in general I'd say, since you can just use the safe scope to do physics, i.e. in the process function as instructed by the documentation

Regarding what it means, it simply means that physics isn't accessible outside of the dedicated methods, because of how it's implemented, it's just a minor detail and maybe doesn't really belong in the documentation here, as it's an implementation detail

ratkingsminion commented 10 months ago

you can just use the safe scope to do physics, i.e. in the process function as instructed by the documentation

I find that's exactly the problem for me - sometimes I want to do one-shot raycasts or similar, and put this functionality in helper functions. Such functions I like to reuse, but as I can't make sure inside the function that the physics state is actually available, it's reducing reusability of physics related code.

AThousandShips commented 9 months ago

That's not something you should do, you should only do it when it's supported, it's not supported, as the documentation says

ratkingsminion commented 9 months ago

I just noticed that there is Engine.is_in_physics_frame() which is probably what I want for checking if I can use Raycasts and the like or not (and if not, I do await get_tree().physics_frame.