This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

I have the following code

When I show the nodes that are in the group, 2 appear
Because I only have 2 node in that group, and in my whole project

enter image description here

But when I use the loop to print the nodes
At the end of all I get a null
.

@onready var enemies = get_tree().get_nodes_in_group("enemy")

func _ready():

    print(enemies)
    print(print_enemies(enemies))
    pass

func print_enemies(enemies_groups):
    for enemy in enemies_groups :
        if enemy != null:
            print(enemy)

.
Here is the example of what I get

all nodes in group enemy

[MyEnemy:<Sprite2D#25753027719>, MyEnemy3:<Sprite2D#25786582153>]

loop

MyEnemy:<Sprite2D#25753027719>
MyEnemy3:<Sprite2D#25786582153>
<null>

Why does that null appear? How do I make it not appear?

Godot version 4.0.1
in Engine by (42 points)

1 Answer

+2 votes

Your loop is printing correctly but the code is also printing the return value of the print_enemies function (which is null because it doesn't return a value). If your test code is:

func _ready():
    print(enemies)
    print_enemies(enemies)

It'll work.

by (1,406 points)

It worked, thanks

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 Frequently asked questions and 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 [email protected] with your username.