0 votes

Hi all,

I am making an arkanoid clone for learning. I have this line which appears to be called when I press escape, to leave the current scene and load the main menu:

if get_tree().get_nodes_in_group("balls").size() <= 1:
    for ball in get_tree().get_nodes_in_group("balls"):
        if ball:
            #do stuff here 

I get the following error:
Attempt to call function 'get_nodes_in_group' in base 'null instance' on a null instance.

There are definitely ball/s on screen when I press escape. The code above is within a function called:

func _on_Timer_timeout():

So I can't see even why the timer would be triggered and time out.

I'm a little confused. Can anyone see or think of anything that I'm missing?

Thanks so much....

in Engine by (824 points)

1 Answer

+1 vote
Best answer

It seems like your node is not inside the scene tree, therefore get_tree() returns null. You might want to check this before running your code with is_inside_tree().

by (703 points)
selected by

Thank you! That's a great little function. I can already see other ways I can use this. Much appreciated. Problem now solved.

How could you fix that problem, please? I'm acrossing a similar issue

Hi there, I used is_inside_tree() as per /u/timoschwarzer suggested. I hope that helps

@Robster I think what he meant is, how did you actually fix the error? I am running into the same issue now.

Indeed. I guess looking back (this was a long time ago) I PROBABLY did something like:

i

f get_tree().get_nodes_in_group("balls").is_inside_tree():
    if get_tree().get_nodes_in_group("balls").size() <= 1:
        for ball in get_tree().get_nodes_in_group("balls"):
            if ball:
                #do stuff here 

This is a guess and the code is backed up in cold storage now but I suspect I was using the function is_inside_tree() to check first if the node was actually inside the scene tree.

extends Panel

func _ready():
    var commons = get_tree().get_nodes_in_group("commons")      
    get_node("Button").connect("button_down", self, "_on_Button_button_down")
    get_node("Button").connect("button_up", self, "_on_Button_button_up")
    get_node("Button").text = str(commons)

func _on_Button_button_down():
    get_node("Label").text = "Hello!"

func _on_Button_button_up():
    get_node("Label").text = ""

In my case it works only if var commons is in the func _ready()

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.