A Checkpoint
Part of the Signals and Collisions section of Coddy's Godot journey. Lesson 20 of 27.
A checkpoint changes where the spikes send you. So the start point becomes a variable at the top of main.gd, where every function can reach it:
var start_point = Vector2(80, 280)
func _on_spikes_body_entered(body):
body.position = start_pointThe spikes send you to whatever start_point holds at that moment. Change the variable, and the next hit sends you there.
Change it with start_point = .... Writing var start_point = ... inside a function makes a new variable that exists only in that function. The one at the top stays the same.
Challenge
MediumMake the flag a checkpoint.
- In
_on_checkpoint_body_entered, setstart_pointto$Checkpoint.position, with novar. - Turn the flag mint:
$Checkpoint.modulate = Color(0.49, 0.95, 0.75).
Press Play, touch the flag, then the spikes: you come back at the flag. Then Check.
Try it yourself
extends Node2D
var start_point = Vector2(80, 280)
func _ready():
$Spikes.body_entered.connect(_on_spikes_body_entered)
$Checkpoint.body_entered.connect(_on_checkpoint_body_entered)
func _on_spikes_body_entered(body):
body.position = start_point
func _on_checkpoint_body_entered(body):
pass
All lessons in Signals and Collisions
Practice on your own: Godot playground