Help with Winning and Losing Function

Godot 4.2.2

I made a path follow 2d for a Racing game And what I want is when the “Bot” finishes the lap first than the Player that it shows a You Lose screen, But if the player wins it shows a You win Screen. Can anyone help? My code so far:

extends PathFollow2D

var speed = 0.03

func _process(delta):
	progress_ratio += delta * speed

Well, there are three parts of this, right?

  • Figuring out when someone has finished the lap
  • Figuring out who finished the lap (bot or player)
  • Changing to a different screen, depending on who won

For the first two parts, I suggest creating an Area2D at the finish line, giving it a script, and connecting the body_entered and/or area_entered signals to a method in that script. If your player is a CharacterBody2D, for example, then it should trigger body_entered.

I don’t know how your bot is constructed, other than it uses a PathFollow2D - if there’s some kind of physics body in there, it should also trigger a body_entered signal from the Area2D, otherwise you could attach an Area2D to it to trigger the area_entered signal instead… or you could simply use the progress_ratio - if that gets to 1 or above, that means the bot is at the finish line, right?

Switching to a new scene is not very hard - see SceneTree.change_scene_to_file and SceneTree.change_scene_to_packed. If you want more control over the scene change, here is some documentation.

The bot uses a Path Follow 2d @tayacan