How to get the farthest node in the scene ?

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

How to get the farthest node position in the scene ?
I know a way to loop the root node and compare the positions but I wondered if there was a faster way, specially a built in one.

Thanks.

There is no better way that I know of…

Bojidar Marinov | 2016-02-27 19:26

:bust_in_silhouette: Reply From: Kiori

I believe you are looking for this:
put this inside func _ready for instance
var last_node = get_tree().get_root().get_child(0).get_child(get_child_count()).get_name() print(last_node)

I’m not sure what you want but get_child(get_child_count()) will get the last of whatever you’re at.

If you surround your code in ‘`’ (backward-slanted apostrophe), it would be recognized as code and not as text.

Bojidar Marinov | 2016-02-28 09:35

Thanks, didn’t pick that up.

Kiori | 2016-02-28 22:43

Normally arrays go from 0 to x -1. But since you’re getting child count and your root is 0 is indeed get_child(get_child_count())

trollworkout | 2016-02-29 02:09

Also, if you do somenode.get_child(get_child_count()), it would get the child of that node which is with the index of the last child of the current (self) node. You should rather do somenode.get_node(somenode.get_child_count())

Bojidar Marinov | 2016-02-29 10:01

:bust_in_silhouette: Reply From: Mohammad Hadi Aliakb

when you add a node in screen or you set the position you should send to a singelton that will add it in a sorted array in this way you always have a sorted array for position and you can find everything.
if you need to find the farthest node just once go with your solution but if you need it multple times I think the best way is that you have a sorted array and build it on _ready and change it using overriding the set_pos

:bust_in_silhouette: Reply From: Bojidar Marinov

In this answer, I would like to present you a non-builtin way to get the farthest node. The idea is to loop over all nodes, and then compare the distances between their positions.

First of all, if you want to compare distances, you should use Vector2::distance_squared_to and Vector2::length_squared (or Vector3), to save a call to sqrt.

Now, to get the farthest node in an array, you would go with something like (2D):

func get_farthest_node2d(array_of_node2ds):
    var max_node = null
    var max_distance_squared = -1
    for node2d in array_of_node2ds:
        var distance_squared = get_pos().distance_squared_to(node2d.get_pos())
        if distance_squared > max_distance_squared:
            max_distance_squared = distance_squared
            max_node = node2d
    return max_node

In 3D it is similar, just replace get_pos with get_position, and node2d with spatial. It is also quite similar for finding the one that’s closest, just flip the sign, change max with min, and make sure you handle the initial case correctly, so that it doesn’t stay equal to -1 forever.

To use the function on a node’s children, you can use get_farthest_node2d(node.get_children()).