#ray is your RayCast2D node.
var objects_collide = [] #The colliding objects go here.
while ray.is_colliding():
var obj = ray.get_collider() #get the next object that is colliding.
objects_collide.append( obj ) #add it to the array.
ray.add_exception( obj ) #add to ray's exception. That way it could detect something being behind it.
ray.force_raycast_update() #update the ray's collision query.
#after all is done, remove the objects from ray's exception.
for obj in objects_collide:
ray.remove_exception( obj )