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):
queue_free()

Scene tree looks like:

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

Thanks

: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

#save
extends Area2D
func _on_Coin_body_entered(body):
    add_to_group("Collected")
    queue_free()

#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():
    player.set_global_position(start_position)