The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

0 votes

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

Godot version 3.5.1
in Engine by (15 points)

1 Answer

0 votes

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)
by (6,942 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.