How to render sprite repeatedly on given Path2D?

Hello! I want to render links of chain on a given Path2d repeatedly and I actually achieved what I wanted with code:
image

var Chain = load("res://traps/saw/saw_chain.tscn")

const chain_size = 3

# Non relevant code is omitted. This method is called on ready
func _spawnChain():
	var linksAmount = floor(path.curve.get_baked_length() / (chain_size * 2))
	for i in linksAmount:
		var link = Chain.instantiate()
		path.add_child(link)
		pathFollow.progress = chain_size + (chain_size * 2) * i
		link.position = pathFollow.position

However I have a feeling that there should be a better way to do it. This is a very simple example because the path is static but what if the path is changing dynamically or what if it is acting like a spring? Another idea I have is to utilize Curve2D of the Path2D by either populating dots on it via script automatically and then rendering links on every such dot or, this part I’m not sure about because docs are kind of vague, by utilizing its bake_interval somehow and getting all the cached points but I’m not sure how to do it. But even still I’m wondering if there is a way to achieve it without much coding or maybe even GPU based process via shaders?

I found a way of doing this by using the Path2D.curve.get_baked_points() method.

Add a script to your Path2D node.
in the _ready() function get the array of baked points from the curve, then loop through them and you can spawn an image at each point.

extends Path2D

@export var image:Texture2D

func _ready():
	var baked_points = curve.get_baked_points()
	curve.bake_interval = 5 #edit this to have more distance between each sprite
	for point in baked_points:
		var s = Sprite2D.new()
		s.texture = image
		s.position = point
		add_child(s)
1 Like