get_node returning null

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By i dont like

i need my player to become parented to other objects sometimes, and im trying to do this in a state of the character controller in the physics process

this is what i have for the state:

	if current_state == State.cockpit:
	get_parent().remove_child(self)
	get_node("/the_game/Spaceship").add_child(self)

when i try to activate the state, it says that the node i am trying to get is null. both the player and the part id like it to be parented to are only children of the root part

what am i doing wrong and is there a better way to do this

:bust_in_silhouette: Reply From: Regulus

Maybe you should tryget_tree().get_root().get_node("the_game/Spaceship"), assuming “the_game” is your base scene node,
instead of get_node("/the_game/Spaceship"), as I don’t think that Godot NodePaths which start with “/” mean “relative to root node”.

I don’t think the problem is related to your parenting at all. The node path you’ve noted just isn’t valid. Really, you probably want this:

get_node("/root/the_game/Spaceship").add_child(self)

Absolute node paths always start with the /root node, which is the application’s pre-defined root viewport.

jgodfrey | 2022-09-08 02:14