0 votes

I am making a 2d platformer with a checkpoint. The character will respawn there after dying. However, I cannot teleport to the checkpoint after dying it teleports me to the wrong place. In a previous question, a user told me to use global_position() I have changed my code to this.

Checkpoint script:

extends Area2D

var checkpoint_pos = self.global_position



func _on_Checkpoint_body_entered(body: Node) -> void:
    queue_free()
    Vector2(get_global_position())

Player hit checkpoint script:

func _on_Checkpoint_body_entered(body: Node) -> void:
Vector2(get_global_position())
checkpoint_hit = true

Player respawn script:

elif will_die == true and is_on_floor() and checkpoint_hit == true:
    self.global_position = CheckpointReal.checkpoint_pos
    print("fall damage taken")
    will_die = false

Just a note: CheckpointReal is the global variable
The checkpoint script is an area 2d and not A 2D node
The player script is in a kinematic body 2d

Thanks so much :D

Godot version v3.4.4
in Engine by (31 points)

The _on_Checkpoint_body_entered() function should probably look more like this:

func _on_Checkpoint_body_entered(body: Node) -> void:
    # It may be a good idea to check what's coming through the Area2D. 
    if body is the_player:
        # Make sure this variable's value is accessible from the script which puts the player at the checkpoint.
        var some_position: Vector2 = body.get_global_position()
        queue_free()

The function related to the checkpoint in the player's scene probably isn't necessary; the checkpoint script should take care of any respawning.

The code for respawning the player (or putting the player at the checkpoint) should be changed:

...
elif will_die and is_on_floor() and checkpoint_hit:
    # Put the player at the checkpoint. As you may guess, the player scene's object is represented by the variable `the_player`.
    the_player.global_position = CheckpointReal.checkpoint_pos
    print("Going down!")
    will_die = false

I don't know whether this will work. But it should help you to find an answer.

I would like to ask about the
the_player.global_position = CheckpointReal.checkpoint_pos print("Going down!") will_die = false

What is "the_player" supposed to mean? Is it what I name my sprite? Should I use self?

the_player is a reference to the player script/scene. The respawning code should get a reference to the player's script/scene.

1 Answer

+1 vote

Where in your code are you setting the CheckpointReal global variable?

I use the following code:

Checkpoint scene script:

func _on_Area2D_body_entered(body):
    if "Player" in body.name:
        PlayerVariables.last_checkpoint = body.global_position
        PlayerVariables.reached_checkpoint = true

Player script:

func move_last_checkpoint():
    global_position = PlayerVariables.last_checkpoint
    $Camera2D.reset_smoothing()

PlayerVariables is a global script: (has an initial spawn point set)

var last_checkpoint = Vector2(157,140)
by (53 points)

Thanks so much :D

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.