Hello, when i am trying to create a teleporter in godot 4, it doesnt teleport me to the second level when touched. Please help.
extends CollisionShape2D
func _on_collision_shape_2d_child_entered_tree(node: Node) -> void:
get_tree().change_scene_to_file("res://Levels/level_2.tscn")
What signal is this function hooked up to? You should probably want the function to be connected to a body_entered
or area_entered
signal, depending on what sort of class your Player
class derives from.
1 Like
well its connected to a collision shape 2d
also for some reason it can only connect to the player 2d for some reason
Prepending _on
before a function name is usually done to indicate the function is a signal handler. Is this supposed to be a signal handler? i.e. did you go to the Node->Signals
panel to connect the function to a signal, or do you have a line of code somewhere that looks like this:
something.connect.(_on_collision_shape_2d_child_entered_tree.bind())
?
i went to the signal panel and connected it via there
And what signal is the handler connected to?
the player scene for some reason it doesnt let me put it anywhere else
You’re trying to connect a handler to a child_entered_tree
signal. That’s the wrong signal. You probably want a body_entered
signal for that.
Rename the loading zone scene something like LoadingZoneScene
, then replace your script with this:
func _ready()->void:
body_entered.connect(_on_body_entered.bind())
func _on_body_entered()->void:
get_tree().change_scene_to_file("res://Levels/level_2.tscn")
Warning: the script I provided will respond to any body entering the zone. You probably want it to filter out anything that isn’t the player.
oh the player will be the only body in the game
also now the line
func _ready()->void:
body_entered.connect(_on_body_entered.bind())
is broken because it says: Identifier “body entered” not declared in current scope
Replace the StaticBody2D
with an Area2D
and attach the script to that.
ok i have now fixed it! i just recreated the area 2d as a different scene and it worked! thanks for your help!
1 Like