Check if node in scene tree is loaded?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By smooth_stacks
extends Control

onready var Debug = $Debug

onready var player = get_parent().get_node("PlayerCharacter")

onready var player_state = player.STATE

onready var player_physics = player.PHYSICS


func _process(delta):
	
	Debug.text = "FPS: " + str( Engine.get_frames_per_second() ) + "\n"
	
	for n in player_state:
		
		Debug.text += str( n ) + ": " + str( player_state[n] ) + "\n"
	
	Debug.text += "\nVelocity: " + str( player_physics.VELOCITY )

This only works if this GUI control is loaded after the playercharacter node from my understanding. Is there a way to have a conditional statement that I can implement in process() to check if the player node in the tree is loaded before calling process code?

:bust_in_silhouette: Reply From: IvanVoirol

Before answering, one quick tip, you can replace

onready var player = get_parent().get_node("PlayerCharacter")

with

onready var player = get_node("../PlayerCharacter")

To know wether the node is loaded, you can use the has_node(name : String) method on its potential parent node. It returns a bool value (true if it has the node with the name you gave as an argument).

:bust_in_silhouette: Reply From: Erich_Lamontagne

If you instanced a scene but haven’t added it as a child anywhere in the active scene, you can call get_parent() on the instance to see if it has a parent. It can be used in an if statement to see if it’s loaded in the scene.