Members of a group in subscene (not Scenetree)

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By bobokox
:warning: Old Version Published before Godot 3 was released.

Hey there!

i made a scene with a chest. inside the chest there are position2d nodes in a group named “cslot” for storing items.
the problem is, when i add two or more chests to my main scene, the items are stored in the free slots of one chest. the script is on the root node of my chest scene, the code line that issue is the following:

onready var slots = get_tree().get_nodes_in_group("cslot")

is it possible to ask for the nodes in group only inside the scene? something like get_tree().get_current_scene().get_nodes_in_group() (doesn’t work ;))

thank you very much

:bust_in_silhouette: Reply From: avencherus

As far as I know groups don’t have any filters to them. What’s in the group is what’s in the group. You have to filter them yourself.

You’ll have to decide on what values you want to use or assign your own variables accordingly. An example (though not an optimal one), might look like this:

const GROUP_NAME = "my_group"

func get_nodes_by_chest_name(chest_name):
	var results = []
	
	for node in get_tree().get_nodes_in_group(GROUP_NAME):
		if(node.get_owner().get_name() == chest_name):
			results.append(node)
	
	return results

func _ready():
	var chest_nodes = get_nodes_by_chest_name("chest 1")

hoped for a simpler solution.
but thank you very much. the script is working perfectly!

bobokox | 2017-02-12 03:40

You’re welcome.

avencherus | 2017-02-12 03:50

:bust_in_silhouette: Reply From: eons

It is possible to create many sub-groups using strings, e.g., if the basic group is CSLOT, when adding to scene add it to the group "CSLOT"+"01" too.

Naming rules will depend on your design, could be slots.get_group()+chest.get_name() (you can use metadata or a variable on the chest too, even the chest position).

Then you can get all the stuff on all the chests or filter with your group naming rules.