How to free a scene and it not to come back after reloading the level.

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

I am a beginner. I created a simple 2d platformer game and I am trying to add coins. Whenever the player dies, get_tree().change_scene(level scene) is run to reset the scene and the player’s pos. However, when the player dies and the scene is reset, the coins reappear. I would like the player to go back to the original pos but the coins that have been collected stay collected and don’t reappear.

*The coins are children of the level scene

current code for the coins:

extends Area2D
func _on_Coin_body_entered(body):

Scene tree looks like:

→ -Coin(Area2D)
→ -> -AnimatedSprite
→ -> -CollisionShape


:bust_in_silhouette: Reply From: Wakatta

There are several things you can do here

  • You can save the scene before reset and load it after
  • You can, instead of using get_tree().change_scene(level scene) reset only the player’s position to a predefined position or a checkpoint

Option 1

extends Area2D
func _on_Coin_body_entered(body):

#load in level scene
get_tree().call_group("Collected", "queue_free")

Option 2

onready var start_position = player.get_global_position()

func _on_player_death():