How to run a function on physics thread?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By YanDaik

When engine uses “run on separate thread” setting for 2d/3d physics how to run custom function on physics thread? From main thread.

Without creating custom event dispatcher?

:bust_in_silhouette: Reply From: Zylann

I think there is no API to run some custom script code on the Physics thread(s). Those are dedicated to the engine. Callbacks like _physics_process or _integrate_forces are executed during a sync point with the main thread so they also don’t run within such threads, AFAIK.

get_tree().physics_frame.connect(Callable, Object.CONNECT_ONE_SHOT) works for me so far (Godot 4.2.2). I’m able to get PhysicsServer2D.body_get_direct_state() this way.