0 votes

how to always get the last child of a node independently of the number of children.
Even if the number of children is higher or lower, always take the last one, with the get_child() method I take a specific one but if I add more, I can't get the last one

in Engine by (195 points)
edited by

1 Answer

+1 vote

I think the node indices are stable and predictable. If that's the case, you should be able to get the number of children associated with a parent via get_child_count() and then reference the last one via its index using get_child(). So, essentially this:

var last_child = $Parent.get_child($Parent.get_child_count()-1)
by (14,868 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 [email protected] with your username.