Godot Version
4.5.1
Question
I am making a homing missile in my game, I got the bullet to track the player and spawn from my enemy ship, however if my player ship dies while there's a homing missile on screen, I get a global_position error due the fact the bullet on screen cannot use the player's global position any more due to the player being "queue_free"
I have tried to use “is_instance_valid” to check for the player validity then stop the prosses or change the direction to something else or stop checking for global_position of the player, nothing worked.
My current code posted here is just me seeing if the game will print “dead” if the player is no longer valid, but it does not work, game did not print “dead” after the player was “queue_free”
func _process(_delta: float) → void:
if is_instance_valid(PlayerShip):
var direction = position.direction_to(player.global_position).normalized()
velocity = direction * speed
else:
print (“dead”)
tldr:
homing missile crashes my game if it cant find the players global position after player death.
Easiest way to solve it would be with a simple null check.
Before giving the missile directions to the player, check if player != null and if the player is not null, home on the player. If the player is null, you can give the missile other directions, or have it keep going the last direction stored before the player was destroyed.
ok i saw this answer before and for some reason i can not wrap my head around it
func _process(_delta: float) -> void:
if PlayerShip != null:
var direction = position.direction_to(player.global_position).normalized()
velocity = direction * speed
move_and_slide()
I am still getting the error….I know i am missing something simple.
Ok, so in your code I’m seeing two variables, PlayerShip and player. How exactly do those differ? It seems like you might be checking the wrong variable, as in the direction variable it looks for player.global_position and you’re null checking PlayerShip, which is a different variable. If those two are different objects and player is the one getting freed, you need to null check player.
func _process(_delta: float) -> void:
if player != null:
var direction = position.direction_to(player.global_position).normalized()
velocity = direction * speed
move_and_slide()
OMG thank you soooooo much its working now, I still not sure entirely why != null: works but ill poke at it later.
I have been banging my head on this for a while.
Thank you again 
I might be wrong but I believe is_instance_valid() would work the same in this context, as it also checks if the node exists, it’s just that your old code wasn’t checking the correct variable.
I just came off from Heartbeast youtube tutorial, I have been since poking and playing with all the components to better understand what is happing, I think the (player.global_position) is lifited from one of his components and I thought I had to use the class name my player ship uses to find its position.
anyways, thanks very much, getting closer to finishing my game now.
If PlayerShip is a script class name, it will always evaluate to true and checking is_instance_valid(PlayerShip) will also always return true because PlayerShip is an alias for the script resource object which is always valid.
1 Like