$begingroup$

I’m making an attempt to spawn sufficient gameobjects(ropePieces) between two level form of like a rope.
However IDK easy methods to cease when i attain the top level

extends Node

onready var rb=$RigidBody2D
onready var begin=$StaticBody2D
onready var finish=$StaticBody2D2
var piece= preload("res://scenes/RopePiece.tscn")

var isStop=false
onready var obj2=$StaticBody2D


func _ready():
    for n in 5:
        var obj=piece.occasion()
        add_child(obj)
        obj.place=obj2.place
        obj.place.y+=30
        
        obj.get_node("PinJoint2D").node_a=obj2.get_node(".").get_path()
        obj.get_node("PinJoint2D").node_b=obj.get_node(".").get_path()
        obj2=obj
        var distance=obj2.position-obj.place
        print(distance)

$endgroup$