When using the remove_child it gives me an error

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

I get remove_child: Cannot remover child node of this node
but I previously added it with add_child using a variable instance, because it tells me that this node does not exist, can someone help me?

Instead of asking different variations of the same question over and over, why not just use the Comment feature provided within a question to continue the conversation in a single place? It’s very confusing for people trying to help you or people seeking the same information in the future when the info is scattered between multiple questions.

Back to your question…

To get help, you’re going to need to:

  • Post the offending code (using the Code Sample { } button)
  • Indicate what line is causing the error
  • Post (either as text or a screenshot) the organization of your scene tree
  • Post the exact error message you see in Godot

jgodfrey | 2020-10-21 14:08

ok sorry look

onready var iteam_ma = load("res://Scrips materiales/Madera.tscn") 

   
func _process(delta):
var madera1 = iteam_ma.instance()

if madera == true && ocupado1_ma == false && ocupado_universal1 == false && ocupado2_ma == false:
	$slot1.add_child(_slot1)
	$item_slot1_ma.add_child(madera1)
	ocupado1_ma= true
	ocupado_universal1 = true

["that's how I add the node"]

if Input.is_action_pressed("Shif"):
	$item_slot1_ma.remove_child(madera1)

[“and that’s how I want to eliminate”]


and the error is

E 0:00:16.417 remove_child: Cannot remove child node Madera as it is not a child of this node.
` <Error de C++>Condition “idx == -1” is true.
<Fuente C++> scene/main/node.cpp:1256 @ remove_child()
Inventario.gd:58 @ _process()

elrico26 | 2020-10-22 00:01

:bust_in_silhouette: Reply From: jgodfrey

So, it looks like you’re creating a new, independent instance of iteam_ma in every frame with this code:

var madera1 = iteam_ma.instance()

I doubt that’s what you intend to do here…

Next, you might add the current instance to the scene tree via a call to add_child() - assuming the complex if logic is all true in the current frame.

Finally, you might try to remove the current instance from the scene tree, if the Shif action is pressed in the current frame…

I’d say what’s likely happening is this:

In some frame…

  • You create a new instance
  • You add that instance to the tree
  • The Shif action is not pressed, so you don’t remove the instance from the tree

Now, in some future frame…

  • You create a new instance
  • You don’t add it to the tree (because your if checks aren’t true)
  • You attempt to remove that instance from the tree (because Shif if pressed)

Since that instance was never added to the tree, it cannot be removed from the tree, and will lead to the error you reported…

Thanks bror
you were right what it was

 var madera1 = iteam_ma.instance()

now the instance () is created only when the logic is true and at the same time in the logic is add_chill

also for it to work well he had to put

if Input.is_action_just_pressed("Shif"):
		$item_slot1_ma.remove_child(iteam_new)

previously the logic was

   if Input.is_action_pressed("Shif"):
    		$item_slot1_ma.remove_child(iteam_new)

this was changed because when saying is_action_pressed An error was created since an attempt was made to remove a node that had already been removed since this function is executed over and over again while the indicated key is pressed

Now with

is_action_just_pressed

it is executed only once when pressing the key
but of course if you press for the second time it gives you the error that there is no chill node in …

elrico26 | 2020-10-22 13:53