Repository: godot
Issue: Errors when changing scene using change_scene_to_file()
Labels: discussion,topic:core

### Tested versions

Reproducible in: 4.2 stable

### System information

Win 10, Godot 4.2 stable, Forward +, dedicated Nvidea RTX 1080

### Issue description
--------------------------------------------------
**Edit:** currently recommend workaround/fix seems to be to call `change_scene_to_file()` **deferred**.

So instead of
```gdscript
get_tree().change_scene_to_file("res://some_scene.tscn")
```
write:
```gdscript
get_tree().change_scene_to_file.bind("res://some_scene.tscn").call_deferred()
```
--------------------------------------------------
--------------------------------------------------
Original issue description:

Using `get_tree().change_scene_to_file("res://some_scene.tscn")` works , but it produces a number of red errors. These seem to be different depending on the circumstances.

To test this, I made two scenes, **Scene A** and **Scene B** using Area3Ds and CharacterBody3D (with default template script):

![image](https://github.com/godotengine/godot/assets/47016402/72d41e41-ffa5-4d5f-8113-792abbaa0012)



--------------------------------------------------
In **Scene A** I change scene by walking into an Area3D with a CharacterBody3D (template script), triggering a `body_entered` signal:

```gdscript
extends Node3D

func _on_area_3d_body_entered(body):
	if "Player" in body.name:
		get_tree().change_scene_to_file("res://change_3d_scene_b.tscn")
```
This will produce these two errors:

> E 0:00:05:0401   change_3d_scene_A.gd:5 @ _on_area_3d_body_entered(): This function can't be used during the in/out signal.
>   <C++ Error>    Condition "locked" is true.
>   <C++ Source>   scene/3d/area_3d.cpp:289 @ _clear_monitoring()
>   <Stack Trace>  change_3d_scene_A.gd:5 @ _on_area_3d_body_entered()

> E 0:00:05:0401   change_3d_scene_A.gd:5 @ _on_area_3d_body_entered(): Removing a CollisionObject node during a physics callback is not allowed and will cause undesired behavior. Remove with call_deferred() instead.
>   <C++ Source>   scene/3d/collision_object_3d.cpp:113 @ _notification()
>   <Stack Trace>  change_3d_scene_A.gd:5 @ _on_area_3d_body_entered()

--------------------------------------------------

In **Scene B** I change scene by walking into an Area3D with a CharacterBody3D, triggering a `body_entered` signal which will set a `can_interact` boolean variable in the character script. If the player then presses an Input action, the character fires a global custom signal, which the level connects to and changes the scene:

Global Autoload:
```gdscript
signal change_level
```

Character:
```gdscript
var can_interact = false

func _physics_process(delta):
	if can_interact:
		if Input.is_action_just_pressed("ui_accept"):
			Global.change_level.emit()
```

Level:
```gdscript
extends Node3D

func _ready():
	Global.change_level.connect(change_to_other_level)

func _on_area_3d_body_entered(body):
	if "Player" in body.name:
		body.can_interact = true

func _on_area_3d_body_exited(body):
	if "Player" in body.name:
		body.can_interact = false

func change_to_other_level():
	get_tree().change_scene_to_file("res://change_3d_scene_a.tscn")
```
This will produce these four errors:

> E 0:00:07:0043   change_3d_scene_b.tscn::GDScript_7cg2h:34 @ _physics_process(): Condition "!is_inside_tree()" is true. Returning: Transform3D()
>   <C++ Source>   scene/3d/node_3d.cpp:343 @ get_global_transform()
>   <Stack Trace>  change_3d_scene_b.tscn::GDScript_7cg2h:34 @ _physics_process()

> E 0:00:07:0043   change_3d_scene_b.tscn::GDScript_7cg2h:34 @ _physics_process(): Condition "!is_inside_tree()" is true. Returning: Transform3D()
>   <C++ Source>   scene/3d/node_3d.cpp:343 @ get_global_transform()
>   <Stack Trace>  change_3d_scene_b.tscn::GDScript_7cg2h:34 @ _physics_process()

> E 0:00:07:0043   change_3d_scene_b.tscn::GDScript_7cg2h:34 @ _physics_process(): Parameter "body->get_space()" is null.
>   <C++ Source>   servers/physics_3d/godot_physics_server_3d.cpp:915 @ body_test_motion()
>   <Stack Trace>  change_3d_scene_b.tscn::GDScript_7cg2h:34 @ _physics_process()

> E 0:00:07:0043   change_3d_scene_b.tscn::GDScript_7cg2h:34 @ _physics_process(): Condition "!is_inside_tree()" is true. Returning: Transform3D()
>   <C++ Source>   scene/3d/node_3d.cpp:343 @ get_global_transform()
>   <Stack Trace>  change_3d_scene_b.tscn::GDScript_7cg2h:34 @ _physics_process()

Note: Line 34 in the character script is just `move_and_slide()`


--------------------------------------------------
Possibly related issue (however that's a different error): https://github.com/godotengine/godot/issues/84681

### Steps to reproduce

Use `get_tree().change_scene_to_file("res://some_scene.tscn")` as described above, or test the provided Minimal Reproduction Project below.

### Minimal reproduction project (MRP)

[change_scene_errors.zip](https://github.com/godotengine/godot/files/13588297/change_scene_errors.zip)