How to get all nodes that aren't in a specified group?

Godot Version

4.4

Question

So, I’m just looking for a way to find all the nodes that aren’t in a specified group. What im looking for would basically just be everything that isn’t found after using get_tree().get_nodes_in_group().

There’s not a method that would give you all nodes outside of a certain group, and there’s not a method that will give you all the nodes either. So you would have to recursively loop through the entire tree and check if every node is not in the group you want to exclude.

But there is probably a way to accomplish what you want without looping through the entire tree, if you provide what you are trying to do.

What I’m trying to do is create an overlay of an animatedsprite2d over the game, but make everything that isn’t the animatedsprite2d disappear. The animatedsprite2d is a child node of the player, because the camera is also a child of the player. I can’t simply move the player to the bottom of the scene tree because that wouldn’t work since there are other objects that need to appear in front of the character.

Is there anyway you could mock up what you’d want? I don’t think you’d want everything else to be visible = false for an overlay? If you only want the animated sprite on top you could set it’s z-index to a very high value

2 Likes

Agreed! There’s certainly a simpler solution than changing almost every node in the tree.

If you’re trying to make a screen transition, then putting the transition on a different CanvasLayer is enough, since the position of the player/camera will not affect things on a different CanvasLayer.

Hi,

I quickly put this together, that does what you want, but as other suggest there might be a better option

func get_nodes_not_in_group(groupName: String) -> PackedStringArray:
	var nodes: PackedStringArray = get_children()
	var nodesingroup: PackedStringArray = get_tree().get_nodes_in_group(groupName)
	var nodesnotingroup: PackedStringArray = []
	for i: int in nodes.size():
		if nodesingroup.find(nodes[i]) == -1:
			nodesnotingroup.append(nodes[i])
	return nodesnotingroup

Change ‘PackedStringArray’ to whatever you need, changing it to just ‘Array’ should return a list of objects rather than name and UID. Group name is case sensitive as well…

Kindly

Ryn

Why not just add everything you want hidden to a different group?