+4 votes

Hello there!

I am still learning about programming, so please, talk with me like you would talk to a kid :-)

For my project in v3.0, I want to respawn my dead player on an instanced spawnpoint (there can be many in my game) that is closest to my player once he is dead. I wrote a code that you can see below.

The question is:
How to make it work? I mean, the position I am trying to get via the code is still nil. I am not able to figure out what to change. I think that the var A should be something else, but I don't know what to put as a value.

Or how would you rewrite the function to work?

Thank you in advance! :-)

The code is:

func respawn_player():
var nearest_spawnpoint = null
var min_distance = 100000
var space_stations = get_tree().get_nodes_in_group("space_station")
var A = space_stations[0]

for B in space_stations:
    if B == A:
        continue
    var distance_to_spawnpoint = global_position.distance_to(B.global_position)
    if distance_to_spawnpoint < min_distance:
        min_distance = distance_to_spawnpoint
        nearest_spawnpoint = B

var new_pos_x = nearest_spawnpoint.global_position.x
var new_pos_y = nearest_spawnpoint.global_position.y
global_position = Vector2(new_pos_x, new_pos_y)
current_health = max_health
can_move = true
in Engine by (60 points)

1 Answer

+3 votes
Best answer

I might do something like this:

func respawn_player():
    # get spawn nodes
    var spawn_points = get_tree().get_nodes_in_group("space_stations")

    # assume the first spawn node is closest
    var nearest_spawn_point = spawn_points[0]

    # look through spawn nodes to see if any are closer
    for spawn_point in spawn_points:
         if spawn_point.global_position.distance_to(player.global_position) < nearest_spawn_point.global_position.distance_to(player.global_position):
            nearest_spawn_point = spawn_point

    # reposition player
    player.global_position = nearest_spawn_point.global_position

If you're running this script from your player node, simply remove references to player.

by (1,549 points)
selected by

This looks a way better than my code. But I am getting an error: "Invalid get index '0' (on base: Array)". Does it mean that the group is empty or what?

Yes, I believe so. I see you used "space station" as the group name in your original post, but I used "space stations", plural. That might be the issue.

Nope. Your code works fine! I just had a mistake in one word. Cool, works fine! Thank you! Really appreciate that:-)

Hm, that is an O(N) solution. I was hoping that Godot internally maintains a KDTree which we can exploit for O(log N) lookup. In my use case the number of things to look up is large, and the linear lookup is prohibitively slow...

hey !
How would you proceed to get the 2nd nearest spawn point ?

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 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.