reparent node. please help, nothing works :(

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

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 

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?

timothybrentwood | 2021-07-06 22:42

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

theMX89 | 2021-07-07 06:00

:bust_in_silhouette: Reply From: frosty

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)
:bust_in_silhouette: Reply From: timothybrentwood

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.

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

theMX89 | 2021-07-07 16:06

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)

timothybrentwood | 2021-07-07 17:47