Can you have independant groups for instanced scenes?

Godot Version



I am instancing 2 instances of the same scene, and want them to have different groups from one another, but the group is shared between them, is there any option to make independant groups for the different instances?
if not, any other recommendations for how to implement something similar are very welcome!
thanks in advance to anyone who wants to help!

You can add any node to a group with Node.add_to_group() Do it after instantiating the node.

that is what is happening
I probably should have specified a little more in the post, so I’ll do it here

the scene I am instancing is already instanced twice in the main scene. during the runtime I am instancing other packed scenes from the instanced scenes I have and adding them to the group. the group is premade but the nodes are instanced during the runtime and added during the runtime, before running the code the group is empty.

Are you talking about “Godot groups” or children? To me, it feels like you’re talking about adding different child nodes to parent nodes that have the same origin class?

no, I am referring to Godot Groups, the ones you can manage in the “Node” tab that is in the top right of the screen.
I am asking if there is a way for an instanced scene to also have sort of an “instanced group” so that when a different instance of the scene adds children to the group, it is to a different independant group of its own and not to a shared group between all instances of the scene.

Sure, but I don’t see where you think it would be a problem? You could for example add the children to an “instance group” that uses the group name of the parent instance, something like str(get_parent()) for the group name?

I couldn’t find a way to make a group during runtime, only while in the editor through the Groups tab
If there is one, can you send it?

I couldn’t find a way to make a group during runtime, only while in the editor through the Groups tab
If there is one, can you send it?

You do not need to explicitly create a group before assigning it to a node. Group names on a node behave more like tags, in that they can be freely added and removed. node.add_to_group("etc") will work as you’d expect regardless of whether you have created a group named "etc" in the editor.

1 Like

I have tried it with the following code

var group = str(self)
func create_card(card, cardTexture, cardValue):
var newCard = card.instantiate()
var numOfCards = get_tree().get_nodes_in_group(group)
newCard.scale = Vector2(0.7 * size.y / 328, 0.7 * size.y / 328)
newCard.i = numOfCards.size() - 1
get_tree().call_group(group, “change_pos”, numOfCards, get_node(“.”))
newCard.choose_card(cardTexture, cardValue)

it doesn’t work with the variable group, but it works with the pre-made group

any idea why that happens?

anyway thank you for the respond!

I believe get_tree only gets nodes that are currently in the scene tree, so when you get nodes in the group it’s only finding nodes that exist in the tree, but you don’t add the newCard node as a child in the scene tree until later in the function. I think that if you moved your add_child(newCard) call to before your get_tree().get_nodes_in_group(group) call then it would probably work as expected.

If that doesn’t do it, can you explain more specifically what you want to happen and what is happening instead?

1 Like

that might be it, but I started to work on something else in the project at the moment, so I’ll check this later when I return to polish some stuff, thank you very much!

You need to add it to the SceneTree before being able to get the nodes in a group. I linked the documentation in my post but I’ll c&p here:

Adds the node to a group. Groups are helpers to name and organize a subset of nodes, for example “enemies” or “collectables”. A node can be in any number of groups. Nodes can be assigned a group at any time, but will not be added until they are inside the scene tree (see is_inside_tree). See notes in the description, and the group methods in SceneTree.

Emphasis mine.