Help with respawning system

Godot Version

4.2.2

Question

I am new to godot and working on a respawning system for my game. For some reason, whenever the player dies, they respawn really far away from the checkpoint. Here is my code:

Checkpoint code:

extends Area2D

func _on_body_entered(_body):
	$"Ambient particles".emitting = false
	$"Explode".emitting = true
	$"Sprite2D".texture = load("res://Assets/Other/checkpoint closed.png")
	$Timer.start()

func _on_timer_timeout():
	Singleton.checkpoint.emit()

Player code (may need to scroll):

extends RigidBody2D

@export var speed = 40
var respawn_location: Vector2

func _ready():
	Singleton.jump_pad.connect(jump_pad)
	Singleton.respawn_player.connect(respawn)
	Singleton.checkpoint.connect(checkpoint)

func _physics_process(delta):
	if Input.is_action_pressed("Move right"):
		angular_velocity += speed * delta
	if Input.is_action_pressed("Move left"):
		angular_velocity -= speed * delta

func jump_pad():
	linear_velocity.y = -800

func respawn():
	linear_velocity = Vector2.ZERO
	angular_velocity = 0
	position = respawn_location

func checkpoint():
	respawn_location = position

Killzone code:

extends Area2D

@onready var timer = $Timer
var player

func _on_body_entered(body):
	player = body
	timer.start()

func _on_timer_timeout():
	Singleton.emit_signal("respawn_player")

Not understand properly, please upload the full project removing you secrets or game idea

Here is a stripped down version of the project:
https://www.mediafire.com/file/4o5jpkmy1w681wm/project.zip/file

Occasionally when you die, you will fail to respawn.
You may need to die a few times to see the bug.

1 Like

I am trying to fix it

First, when I not reach in checkpoint, I fall down for test, then I spawn in air I means in wrong position, to fix it replace this code var respawn_location: Vector2 with this correct code var respawn_location: Vector2 = Vector2(4861, 103-10), second…, let me know if its working then I will mention the second problem…

Second, I see, you created a big mistake, to fix it

  1. Add a variable in Singleton, “checkpoint_reached = false”
  2. In ball.gd, replace your codes checkpoint function with:
func checkpoint():
	if Singleton.checkpoint_reached: return
	respawn_location = position
	Singleton.checkpoint_reached = true
  1. In ball.gd, replace the respawn function with this correct codes:
func respawn():
    if Singleton.checkpoint_reached:
        respawn_location = Vector2(5010.683, 124.0556-25)
    else:
        respawn_location = Vector2(4861, 103-10)

    linear_velocity = Vector2.ZERO
    angular_velocity = 0
    position = respawn_location
  1. Don`t forget to add this code in the ball.gd:
var respawn_location: Vector2 = Vector2(4861, 103-10)
  1. Let me know if its works?
  2. If you suffers with adding more checkpoint, you can tell me I will help you
  3. What is the big glitch/mistake you made? as you mentioned the respawn position (5010.683, 124.0556), the y axis is underground, so its the collision problem so I substract the y axis with 25 means upper position than the ground
1 Like

Thanks!

1 Like

@KingGD Is there any chance you could help me with the other issue I’m having?

Ok, I will try it.