func get_nearest_node():
var array = get_tree().get_nodes_in_group("team_a")
#This code gets the nodes in the "team_a" group to an Array
var player_in_team = [ [array[0].position.distance_to(player.position),array[0]] , [array[1].position.distance_to(player.position),array[1]] , [array[2].position.distance_to(player.position),array[2]] ]
#This code takes the distances of all the objects in the group to the player and makes it a new array
player_in_team.sort_custom(func(a,b): return a[0] < b[0])
#This code sorts the distance codes to the player in the array we just made, from smallest to largest.
return player_in_team[0][1]
#This code returns the name of the nearest node

So you can use this function like that:
look_at(get_nearest_node().position)

You can improve this code , whatever you like and make it even better. This is just the simpliest code to teach beginners
You are welcome!

Better Look:

10 days later

I made it better and easier:
You can use max() and min() instead of sort_custom()

i will give example and you can understand what is max() and min()
var a = 1
var b = 2
var c = 50
var d = 0.1

var r = max(a,b,c,d) returns 50
var r2 = min(a,b,c,d) returns 0.1

What if there is 100 nodes in the group?

    I'm kind of a for loop person:

    var dis = 1000
    var temp_dis
    var closest_player 
    for n in group:
         temp_dis = player.position.distance_to(n.position)
         if temp_dis < dis:
               dis = temp_dis
               closest_player = n
    return closest_player

    It's more lines but it seems clearer to me. Good job, though, and it's a good idea to post code like that. This way doesn't sort either, it just returns closest player. I didn't test it.

    xyz You can improve the first and second technique. I writed the simpliest way to do it. But if you like loops(that i don’t like for performance issues) you can use loops too.

    • xyz replied to this.

      For an extra boost, use distance_squared_to instead of distance_to. It's faster since it doesn't do a square root.
      (Although the hit of some square roots is probably rather small compared to the fact its running in GDScript)

      TalhaDev You can improve the first and second technique. I writed the simpliest way to do it. But if you like loops(that i don’t like for performance issues) you can use loops too.

      It's not very useful as a generalized solution though as it hardcodes the number of nodes and the group name. It's ok that you want to avoid iteration for performance reasons but if you provide a solution for others to use it should cover variety of use cases. With your approach one would still need to use a loop if number of nodes is not known, which is almost always the case.

      It also has a bug. It may not work properly if nodes are in different coordinate spaces since your code used local instead of global positions.

      Here's a proper generalized solution without loops:

      func get_nearest_node_in_group(group, position):
      	var nodes = get_tree().get_nodes_in_group(group)
      	nodes.sort_custom( func(a,b): return a.global_position.distance_to(position)<b.global_position.distance_to(position) )
      	return nodes[0]
        23 days later

        Is there anything similar to python's list comprehension in gdscript?

        • xyz replied to this.

          xyz
          Oh well... alright. I love list comprehensions but i can also live without that feature.
          Thanks for your reply!

          7 months later

          xyz I'm trying to wrap my head around your code but can you please explain line by line how it works? I'd really appreciate it.

          Arrays and dictionaries are difficult topics for me.

          • xyz replied to this.

            dib
            line 1: function definition

            line 2: get the array of nodes in a group

            line 3: sort that array's elements by their distance to the given position. For sorting, the sort_custom() method of the Array class is used. As an argument to this method, you need to provide a custom comparison function that returns which of two given elements is greater/lesser. In this case a lambda callable is passed as that function. Lambda is used to shorten the code. A regular function might have been used as well. The function in the example compares distances of two input arguments to a given position. sort_custom() calls this function whenever it needs to compare elements during the sorting.

            line 4: return the first element of the sorted array as it has the shortest distance to the position