0 votes

so, hi guys.
im trying to reparent a node if an area is enteret. i tried 1 million things but none worked,
and now im here.
my script is this:

func _on_Area2_body_entered(body):

    if body.get_name() == "stone" and Input.is_action_pressed("mousebuttonL"):
        print ("stone ready")

    var new_parent = get_node("area")
    var source = get_node("deco/steiin2")

    get_parent().remove_child(source)
    new_parent.add_child(source)

    pass 
Godot version 3.3
in Engine by (213 points)

Is all of this:

var new_parent = get_node("area")
var source = get_node("deco/steiin2")

get_parent().remove_child(source)
new_parent.add_child(source)

inside of the if statement? Like did you paste it on this website incorrectly indented?

This is all written in an if statement. Is this an error?

2 Answers

+1 vote

Looking at your code, I think your issue is that you're calling get_parent().remove_child(source) but the node returned by get_parent is the current node's parent, not source's parent. Based on your get_node call, it looks like the parent is deco.

Try

source.get_parent().remove_child(source)
by (28 points)
0 votes

The body_entered signal only fires when a body just enters the area therefore the player would need to be pressing the mousebuttonL input as they enter in order for your code to work. Something like this should work, just connect the body_exited signal:

func _input(event: InputEvent) -> void:
    if get_parent() == get_node("area"): # this is probably what you want to do
        set_process_input(false) # but feel free to change it if not
    elif event.is_action_pressed("mousebuttonL"):
        print ("stone ready")
        var new_parent = get_node("area")
        var source = get_node("deco/steiin2")

       get_parent().remove_child(source)
       new_parent.add_child(source)

func _on_Area2_body_entered(body):
    if body.get_name() == "stone":
        set_process_input(true)

func _on_Area2_body_exited(body):
   if not is_processing_input():
        pass
   elif body.get_name() == "stone":
        set_process_input(false)

Also something of note: if the mousebuttonL input is used in another script, the input may get eaten and not echoed to this script.

by (3,711 points)

thanks for the help, but now everytime you click, "stone ready" gets printed, but nothing happens.

Assuming you indented correctly, if "stone ready" is printing then all of this code is executing as well:

var new_parent = get_node("area")
var source = get_node("deco/steiin2")

get_parent().remove_child(source)
new_parent.add_child(source)
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 webmaster@godotengine.org with your username.