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