How do I get my player to restart halfway through game after hitting enemies?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By holhow
:warning: Old Version Published before Godot 3 was released.

Hey guys,

I’m making a platformer game with no prior experience.
I have a kinematic body for my main character and area2D bodies for my enemies.

So far, I’ve been able to make my character restart at the beginning of the level after she hits various enemies using this method:

Area 2D body is connected with body_enter

character script has this script:

onready var initial_pos = get_pos()

func _kill():
set_pos(initial_pos)

and the main node has this script:

extends Node2D

func _on_Enemy_body_enter( body ):
if body.get_name() == “Player”:
body._kill()

so it all works fine but I’d now like now is: after a certain point in the game, when the Player dies she only returns to the halfway point.

How can I achieve this?

You could also check the players position at point of death and use that to determine the respawn point.

D | 2017-03-18 20:01

:bust_in_silhouette: Reply From: M. Alkotob

You can create an invisible Sprite checkpoint node (or visible if you want) and place it as a checkpoint in the level.

Then, when you want to move the player, try:

func onEnemybodyenter( body ):
if body.getname() == "Player":
body.kill(get_node("checkpoint"))

and in player:

func kill(checkpoint):
setpos(checkpoint.get_pos())

If you want to have multiple checkpoints, then instead of passing getnode(“checkpoint”), you can pass a node LatestCheckpoint, which is changed every time a player passes through a new area.

You can create a large collision rectangle for the checkpoint (as a child) to see if a player passed the area. Make it tall vertically so he wouldn’t jump over it by accident.

P.S. Let me know what happens.

It worked!

Minor minor thing was that setpos in the player needed to be set_pos and I had to change the funtion to “_hurt” and replace all the kills for those enemies with hurt as the function had already been used.

Thanks so much!

holhow | 2017-03-18 15:03

Ah apologies about setpos(), but the website converts underscores to italics so I removed it :stuck_out_tongue:

(Next time I’ll add code tags)

Good luck with your game!
(I’m working on one too and I just like to take a break and solve other people’s bugs hehe)

M. Alkotob | 2017-03-18 17:25

Oh cool! Good luck with it!

Would you know how to make the player bounce [bouncy?] when it contacts one specific static body? I want the player to bounce off the floor in one part of the game but not in others.

holhow | 2017-03-18 22:30

To make the player bounce, this is similar to what I use:

	if(not is_bouncing):
		is_bouncing = true
		set_linear_velocity(Vector2(-get_linear_velocity().x/2, -get_linear_velocity().y/2 ))

The if() statement is necessary since without it the velocity is divided every frame that you touch the ground (instead of only once).

Instead of dividing by 2, you can try other numbers like 1.5 or 3. Using 2 makes it realistic and using 1 will make him bounce with the same speed (which can be fun).

Note that this will only let you bounce once, or else you will want to reset the boolean to false elsewhere in the code.

In order to reset the boolean, you can try this function in the player:

func reset_bounce():
    for collider in get_colliding_bodies():
        if(collider.get_name() == 'bouncy_platform' and is_bouncing):
            return
    is_bouncing = false

M. Alkotob | 2017-03-19 06:18

hey, i’m sorry, i’m super new to godot but it’s running the error that it can’t find is_bouncing and so it won’t run - do you know what that means?

holhow | 2017-03-19 13:03

You need to put this line at the top of the script:

var is_bouncing = false

M. Alkotob | 2017-03-19 14:21

hey, sorry again, feel free to not reply but it’s also saying that set _ linear _ velocity is an nonexistent function and I’ve tried adding constants and variables but nothings working.

holhow | 2017-03-19 15:49

set_linear_velocity is not present for kinematic bodies, you will want to use the move() function described here:

https://godot.readthedocs.io/en/stable/tutorials/2d/kinematic_character_2d.html

Try to also add the velocity code they have so you can use my code on it.

M. Alkotob | 2017-03-19 15:52

Don’t use a Sprite. Position2D nodes were made specifically for things like this.

timoschwarzer | 2017-03-19 19:50

You’re right Position2D would do.

It’s just me, but I like to use accurate replicas of my character so I can have a sense of where he will be located after respawn.

M. Alkotob | 2017-03-19 19:52

Don’t even need a node other than area unless you need more visual debug than a shape.

And shape could be a line shape if your game is a strict sidescroll, that way the player won’t miss the checkpoint no matter how much tries to cheat :stuck_out_tongue:

eons | 2017-03-19 22:22

please show the whole script

christianbegg2 | 2018-07-30 18:35

:bust_in_silhouette: Reply From: eons

Like a checkpoint?
Create an area that acts as a checkpoint and when player enters that area, register the new position on the player, it can use another variable for checkpoints or replace the value of the initial position with the new one.

The type of area you use and what you do after the player reachs the checkpoint will depend on the design.


A more flexible way could be creating re/spawn zones on your levels and make one active when the player touches it, that way you will de-couple the player from the spawn process leaving it on the level where it may belong (always depending on the design, of course).