+1 vote

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 'getnodesin_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()
in Engine by (37 points)

1 Answer

+3 votes
Best answer

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.

by (377 points)
selected by

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

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.