0 votes

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

Godot version 3.4.4.stable
in Engine by (26 points)
edited by

1 Answer

0 votes

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".

by (136 points)

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.