Im implementing a simple scene switcher while learning to program a game. I followed this youtube tutorial and looked to implement it myself.
First was the creation of a global scene switcher, here is mine:
extends Node
var current_scene = null
func _ready() -> void:
var root = get_tree().root
current_scene = root.get_child(root.get_child_count() - 1)
func switch_scene(res_path):
call_deferred("_deferred_switch_scene", res_path)
func _deferred_switch_scene(res_path):
current_scene.free()
var s = load(res_path)
current_scene = s.instantiate()
get_tree().root.add_child(current_scene)
get_tree().current_scene = current_scene
Then I implemented a signal in the main town here:
extends Node2D
func _on_house_one_door_body_entered(body):
SceneSwitcher.switch_scene("res://scenes/house_one.tscn")
When I use the code as is, it results in a grey screen. When I replace “body” with “() -> void:”, the main town works, but the function doesnt work. However I do recieve this error in the debugger when I walk into the door:
emit_signalp: Error calling from signal ‘body_entered’ to callable: ‘Node2D(pallet town.gd)::_on_house_one_door_body_entered’: Method expected 0 arguments, but called with 1.
So I believe it is recognizing the collision.
The player has a characterbody2d and a collisionshape2d, and the door has an area2d and a collisionshape2d with monitoring enabled.
Registeel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.