Here you go (commented version further down below):
onready var curve = $Path2D.curve
onready var follow = $Path2D/PathFollow2D
onready var sprite = $Sprite
var point_offsets = []
func _ready():
var points = [Vector2(100, 100), Vector2(400, 400), Vector2(800, 100), Vector2(1000, 100)]
for point in points:
curve.add_point(point)
for i in curve.get_point_count():
var point = curve.get_point_position(i)
var offset = curve.get_closest_offset(point)
point_offsets.push_back(offset)
var point_label = Label.new()
point_label.name = "Label" + str(i)
point_label.rect_position = point
point_label.text = str(point)
point_label.visible = false
add_child(point_label)
func _physics_process(delta: float) -> void:
follow.offset += 5
sprite.position = follow.position
for i in point_offsets.size():
var point_offset = point_offsets[i]
if follow.offset > point_offset:
get_node("Label" + str(i)).show()
else:
get_node("Label" + str(i)).hide()
Version with comments:
# some node paths that we use repeatedly in this script - _yours may differ_!
onready var curve = $Path2D.curve
onready var follow = $Path2D/PathFollow2D
onready var sprite = $Sprite
# an array to store the offset of each point on the path, populated in _ready
var point_offsets = []
func _ready():
# plug-in your point coordinates here - or remove the next three lines and create the path in the editor
var points = [Vector2(100, 100), Vector2(400, 400), Vector2(800, 100), Vector2(1000, 100)]
for point in points:
curve.add_point(point)
# for each point of the curve...
for i in curve.get_point_count():
# ... get the point's position...
var point = curve.get_point_position(i)
# ... and it's offset from the start of the curve...
var offset = curve.get_closest_offset(point)
# ... then store this information for later use
point_offsets.push_back(offset)
# creates a label for each point...
var point_label = Label.new()
# ... named "Label1" for the 1st point, "Label2" for the 2nd and so on...
point_label.name = "Label" + str(i)
# ... places the label at the points position...
point_label.rect_position = point
# ... and makes it display the point coordinates...
point_label.text = str(point)
# ... but hides it by default...
point_label.visible = false
# ... then adds the label to the tree
add_child(point_label)
# if you prefer to add the labels in the editor, simply remove the last block!
# however, make sure that the labels are named properly (Label1, Label2, ...)
# each physics-frame ...
func _physics_process(delta: float) -> void:
# ... increase the offset from the start of the curve by 5...
# (if this value becomes bigger than the curve's length, it will reset to 0)
follow.offset += 5
# ... and move the sprite's position along with it
sprite.position = follow.position
# for each point of the curve...
for i in point_offsets.size():
# ... get its stored offset...
var point_offset = point_offsets[i]
# ... and compare it to the current offset:
if follow.offset > point_offset:
# if the current offset is bigger, show the label
get_node("Label" + str(i)).show()
else:
# if the current offset is smaller, hide the label
get_node("Label" + str(i)).hide()