Having the Player Exit A Building

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By doinsdf


I recently began working with Singletons in Godot and I really need help. When the player exits a building, I want him to be right outside the building, naturally. However, the player just loads right where he originally started in the scene. To get around this, I tried to make a variable that is accessed by the area2D sprite that he has to click to leave, and the original level scene. Here’s my Area2D script:

func _physics_process(_delta):
	var bodies = get_overlapping_bodies()
	for body in bodies:
		if body.name  == "Player" and Input.is_action_just_pressed("go_inside"):
			is_outside_room1 = true

then I have the Level scene call it:

 func _physics_process(_delta):
	var firstroom = get_node("/root/FirstDoorGoBack")
	var move = firstroom.is_outside_room1
	if move == true:
		$Player.position = Vector2(1040.840088, 553.8471)

For some reason, when I write print(move) all I get is false, forever. But I should be getting true, eventually. Maybe I’m going about this all wrong?


I didn’t understand how your problem is related to singletons, but anyway, just a few comments:

  1. Have you checked that this IF statement conditions are actually met and the code inside it is executed?
if body.name  == "Player" and Input.is_action_just_pressed("go_inside"):
  1. When getting a value from another node, it is better to make a function like GetMove(), and then call this function from the other node. This way, there is no risk of you changing a variable from outside the node that has the variable.

  2. Do you have a main scene, with a Main.gd script, that knows and controls everything? If not, you should really do it, it will make everything easier, because then, your Main script would just unload one level (the building), load another (the Level1), and place the player in the desired place. This way, the Level1 scene doesn’t even need to know about the building scene, what state it is, how the player came from it, etc.

Saitodepaula | 2020-06-09 21:45