0 votes

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

in Engine by (49 points)

1 Answer

0 votes

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

by (1,316 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.