get_nodes_in_group() from another scene

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

Is it possible to grab all nodes from another scene that are in a group? I’m basically trying to find all nodes in a group called “Enemy” in the world scene. Once they’re all found I want to then choose a random one and grab its texture for UI sprite.

The errors I’m getting:
Attempt to call function ‘get_nodes_in_group’ in base ‘null instance’ on a null instance.

It’s also saying that world.tscn isn’t being found. So maybe I’m not under standing how to grab nodes from another scene correctly?

Can anyone point me in the right direction or tell me where I’m going wrong? I’m new to GODOT.

extends CanvasLayer

onready var ammobar = get_node("TextureProgress")
onready var bounty = get_node("Panel/Bounty")
# class member variables go here, for example:
# var a = 2
# var b = "textvar"
var bounty_Ended = true
onready var enemies = get_node("res://world.tscn").get_nodes_in_group("Enemy")

func update_HUD():
	var amount = get_parent().ammo
	ammobar.set_val(amount)
	
func update_Bounty():
	if(bounty_Ended == true):
		bounty.Texture = enemies.rfind(rand_range(1,5))
		bounty_Ended = false
	    
func _ready():
	set_fixed_process(true)

func _fixed_process(delta):
	update_HUD()
	update_Bounty()
:bust_in_silhouette: Reply From: cardoso

I think you just need to change get_node("res://world.tscn").get_nodes_in_group("Enemy") to get_tree().get_nodes_in_group("Enemy")

By the way, as long as you can access the SceneTree (using get_tree()) - and I think you can do that if a script inherits from any kind of node (like CanvasLayer) - you can access groups anywhere.

And the nodes in the group need to be in the tree to be found, you can’t get things outside the tree.

eons | 2017-12-02 02:16

Thank you for the answer! It finds it now when using get_tree()!!!

Forward | 2017-12-03 03:44