Reputation: 13
this might be an easy thing to solve but i'm pretty new to godot and gdscript so i have no idea of how to do it and i can't find a tutorial or anything that works for my 2D platformer.
I have a Sprite with a CollisionShape2d that changes the scene when the player enters it's area and i want to make it compare it's own position to the player's, and change to the previous scene if the player's x coordinate is higher when the collision happens or change to the next scene if not, i use a simple condition if position.x > player.position.x:
but i get the error "the identifier "player" isn's declared in the current scope" and i don't know how to fix it. I was trying to do something similar with an enemy that is always facing the player, needless to say i get the same error.
here´s the entire script:
extends Sprite
func _on_hitbox_area_entered(area: Area2D) -> void:
if area.is_in_group("shield"):
if position.x > player.position.x:
get_tree().change_scene("res://Levels/world_" + str(int(get_tree().current_scene.name) + 1) + ".tscn")
else:
get_tree().change_scene("res://Levels/world_" + str(int(get_tree().current_scene.name) - 1) + ".tscn")
Maybe there´s another way to do it but when i search for a solution i find unrelated stuff and i don´t know what else to do
Upvotes: 0
Views: 1621
Reputation: 64
The solution for this is to replace player with area so that the code will look like this:
extends Sprite
func _on_hitbox_area_entered(area: Area2D) -> void:
if area.is_in_group("shield"):
if position.x > area.position.x:
get_tree().change_scene("res://Levels/world_" + str(int(get_tree().current_scene.name) + 1) + ".tscn")
else:
get_tree().change_scene("res://Levels/world_" + str(int(get_tree().current_scene.name) - 1) + ".tscn")
Upvotes: 0