How to perform an action on the top one of a stack of same object?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Florix
:warning: Old Version Published before Godot 3 was released.

Hi guys can someone help? I would like to have a stack of object and a button. And would like to delete the top one object of the stack when i push the button every single time until there is no one left on the screen. How can I achive this theorically and better if some code example. Thanks guys

:bust_in_silhouette: Reply From: ericdl

One solution is to iterate through the objects in the stack, compare the y-value of the current object to the previously iterated object and assign the topmost y-value and node path to temporary variables. After the iteration is complete, delete the object with the topmost y-value:

var topmost = get_node("/root").get_rect().size.y
var temp_path = ""
	    	
for i in self.get_children():
	if i.get_global_pos().y < topmost:
		topmost = i.get_global_pos().y
		temp_path = get_path_to(i)
	
get_node(temp_path).queue_free()

Sample project download: https://drive.google.com/file/d/0BwnfZQAEnciAelcwNXM0MDdvQW8/view?usp=sharing