Godot Version
4.2.2-stable.mono ( after some threads I migrated to 4.3 )
Question
I have been following The ultimate introduction to Godot 4, and I was trying to refactor the code a little bit and to make it more modular, but I have been getting errors.
In essence, I have a BaseLevel
class that is extended by two level classes called OutsideLevel
and InsideLevel
. Both levels have an Area2D
node that sends a signal to the parent node and is received by their script (called OutsideLevel
and InsideLevel
).
The OutsideLevel
script is supposed to load the InsideLevel
and vice versa.
Here is some sample code:
BaseLevel
# BaseLevel.gd
class_name BaseLevel extends Node2D
# Utility function that implements the transition to a new scene with a proper transition animation
func transition_to_scene(scene: PackedScene) -> void:
get_tree().change_scene_to_packed(scene)
InsideLevel
# InsideLevel.gd
extends BaseLevel
const outside_level_scene: PackedScene = preload("res://scenes/levels/outside_level.tscn")
# Signal receptor for when player exits the building gate
func _on_exit_gate_area_body_entered(_body):
self.transition_to_scene(outside_level_scene)
OutsideLevel
# OutsideLevel.gd
extends BaseLevel
const inside_level_scene: PackedScene = preload("res://scenes/levels/inside_level.tscn")
# Signal receptor for when player enters the building gate
func _on_gate_player_entered_gate():
self.transition_to_scene(inside_level_scene)
When I run the main scene (outside), I instantly get the following errors:
Error 1
E 0:00:01:0250 _parse_ext_resource: res://scenes/levels/outside_level.tscn:55 - Parse Error: [ext_resource] referenced non-existent resource at: res://scenes/levels/outside_level.gd
<C++ Source> scene/resources/resource_format_text.cpp:163 @ _parse_ext_resource()
Error 2
E 0:00:01:0262 set_path: Another resource is loaded from path 'res://scenes/levels/outside_level.tscn' (possible cyclic resource inclusion).
<C++ Error> Method/function failed.
<C++ Source> core/io/resource.cpp:75 @ set_path()
I assume that these first two errors are somehow related to how the preloading works, but I’m quite lost on the minutia honestly.
Then, as I enter the building, it happens, but I get the following 2 errors:
Error 3
E 0:01:28:0512 level.gd:31 @ transition_to_scene(): This function can't be used during the in/out signal.
<C++ Error> Condition "locked" is true.
<C++ Source> scene/2d/area_2d.cpp:328 @ _clear_monitoring()
<Stack Trace> level.gd:31 @ transition_to_scene()
outside.gd:9 @ _on_gate_player_entered_gate()
gate.gd:6 @ _on_area_2d_body_entered()
Error 4
E 0:01:28:0512 level.gd:31 @ transition_to_scene(): Removing a CollisionObject node during a physics callback is not allowed and will cause undesired behavior. Remove with call_deferred() instead.
<C++ Source> scene/2d/collision_object_2d.cpp:98 @ _notification()
<Stack Trace> level.gd:31 @ transition_to_scene()
outside.gd:9 @ _on_gate_player_entered_gate()
gate.gd:6 @ _on_area_2d_body_entered()
These next 2 seem to concern a change from godot 4.0 (I think that the video was recorded in that version), and godot 4.2 which might be related to this issue. I however lack the general knowledge of a lot of those topics, so some context would really help.
Finally, as I exit the building I get errors 3 and 4 again, and then I find myself unable to re-enter the building.
Could someone help explain this lil mess?