Remove Child, problems

Godot Version

v4.2.2

Question

So i make a list in the same script that contains info from all the nodes we make and automaticly generate.

static List<ClassTile> instances = new List<ClassTile>();
private Node3D _instance;
	private String _id;
	private Vector3 _position;
	
	
	public ClassTile(String id, Node3D instance, Vector3 position)
	{
		_instance = instance;
		_id = id;
		_position = position;
	} 

I am placing them in the map with AddChildDeferred.

private void AddChildDeferred(Node node)
	{
		CallDeferred("add_child", node);
	}

But i can’t find out how to delete a certain node that is in the list. I found out wich one that i want deleted at a certain time.
Now the trouble is getting rid of that certain tile. I’ve tried a lot of diffrent things that i thougt might work. I searched for it online but i can’t seem te find out what i need. For example i tried:

private void RemoveChildDeferred(Node node)
	{
		CallDeferred("remove_child", node);
	}

But then i get this error :

E 0:00:08:0973   remove_child: Condition "p_child->data.parent != this" is true.
  <C++ Source>   scene/main/node.cpp:1450 @ remove_child()

i also tried this and some other things

i.GetInstance().GetParent().RemoveChild(i)

And i tried a few other methods but maybe i did something wrong. I would appreciate it if u could help, thanks.
(Random thought but is there a way to delete a node bases on the cords, even it is not in the list)

I looked at the source code. All it means is that it will not allow you to remove child node if you are not the direct parent of that node.

So if I do

Var nodea
Var nodeb
Var nodec

nodea.add_child(nodeb)
nodec.remove_child(nodeb) # fatal exception 

But since you are intended to be added and removed from the same parent. My guess is that it may have to deal with the deferred calls? Some how remove_child is called before the add_child?

1 Like

Thanks man, i managed to fix it based on your help.

Node parent = i.GetInstance().GetParent();
parent.RemoveChild(i.GetInstance());

Just want to ask but i added the node as a scene wich has a collision aswell. But right now the only thing that gets deleted is the node but not the collision. If u have any clue how to fix that would be appreciated.

managed to fix that aswell just needed to delete it’s children

I know you are saying “delete a node”, but remove_child is not deleting the node, it is just removing it from the scene tree.

It is usually suggested to use node.queue_free() to really delete. (There is also node.free() but is a little more risky unless you know you can delete it immediately)

Remove_child is meant to save the resource and potentially add it back to the scene tree.

Just so you are aware it’s not deleting, but only removing from the scene tree.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.