Topic was automatically imported from the old Question2Answer platform.
Asked By
Idleman
In the script of my enemy scene, I get the node of the Player with $../Player so my enemy could move_towards the player’s position and attack. But once the player dies, the game stops with a bug that says Player node can not be found. Is there any way to let the games runs after the player node has been queue_free? Thank you.
Scene structure
World
|- YSort
| - Player
| - Enemy
Hi,
Check that the node exists before getting the position of it:
bool is_instance_valid(instance: Object)
Returns whether instance is a valid object (e.g. has not been deleted from memory).
Thank you for answering my question, I know the the idea now not sure how to use that in code. From some example I found, I could print the bool value of the my node. But I am not sure how to put into my code, put it directly into the elif condition? Could you kindly to put it in a sample code for me, so I know how to implement it into coding, thank you.
CHASE:
var player_node = $"../Player"
print(is_instance_valid(player_node))
var player = playerDetectionZone.player
if player != null:
move_towards_point(player.global_position, delta)
elif stats.health < 2:
move_towards_point(player_node.global_position, delta)
else:
state = IDLE
Idleman | 2020-07-14 05:00
Hi,
As the is_instance_valid(player_node) will return true or false you can put it in an if statement.
You may need to make the else statement an else for the new if statement that checks if the instance is valid, so the state becomes idle (I don’t know what you want that part of the code to do).
The line if is_instance_valid(player_node): is the same as saying if is_instance_valid(player_node) == true:
You may also see examples like if !is_instance_valid(player_node): which means “if the instance is NOT valid” or if is_instance_valid(player_node) == false:
Hope that makes sense.
CHASE:
var player_node = $"../Player"
if is_instance_valid(player_node):
var player = playerDetectionZone.player
if player != null:
move_towards_point(player.global_position, delta)
elif stats.health < 2:
move_towards_point(player_node.global_position, delta)
else:
state = IDLE
deaton64 | 2020-07-14 10:20
The function is now working as planned, thank you very much for taking the time to explain this in detail. Now I know how it works.