Function can only be accessed from either the main thread or a thread group - instantiating scenes on the go

Godot Version



I’m instantiating my scenes in a thread. This is fine until I added physics and I get the following:

This function in this node (CollisionShape3D) can only be accessed from either the main thread or a thread group. Use call_deferred() instead

(Despite this is still works, but there’s a jitter so I’m guessing the physics bit is just happening on the main thread).

I understand the error, but don’t know about a thread group. I’ve searched, is this ‘WorkerThreadPool’ what it means by group?

Also based on the example for WorkerThreadPool in the docs

func _process(delta):
    var task_id = WorkerThreadPool.add_group_task(process_enemy_ai, enemies.size())
    # Other code...
    # Other code that depends on the enemy AI already being processed.

Does this mean that it still takes place in one frame, and the frame will wait until the thread is completed before continuing (while your other code in process runs on the main thread) ?

Finally, is there any suggested or built in way to instantiate scenes on the go, or am I correct in threading and splitting it up and so on. I get 200ms+ lag spikes if I do it in the main thread.

Thank you