0 votes

Using Godot 4.0. I have a Grid class that handles tiles in a grid-based game. Tiles extend Node and are stored logically in an array that Grid uses to, for instance, draw a tilemap (and much more, game-related).

Tile is also a class, that Wall, Floor and InvalidTile extend. I use InvalidTile to avoid returning null. But Tile(s) are not instanced in the scene (I just create them with new, but don't use add_child, there's no need to), so they are not part of the tree - which might be related.

Thing is, at some point I'm doing this:

void test_tile: Tile = world_to_tile(x,y) //funcion that returns a Tile at a given worldpos
test_tile.add_to_group(Constants.Groups.VISIBLE_TILES)

Then, a bit later, tiles on the tilemap are revisited and I draw a special sprite if they are in the VISIBLE_TILES group, like this:

if is_in_group(Constants.Groups.VISIBLE_TILES):
  tilemap.set_cell(1, position, Vector2i(0, 0))

And this works! Those tiles that were added to the group are drawn properly. In this image, the darkened tiles belong to VISIBLE_TILES

Darkened tiles belong to the VISIBLE_TILES group

BUT, and here's the question, I want to clear that group before recomputing visibility. And for some reason, this line

var visible_tiles = get_tree().get_nodes_in_group(Constants.Groups.VISIBLE_TILES)

returns an empty array, so I can't loop through the group to call removefromgroup, and that group keeps growing (and I know because I redraw tiles and there are more and more drawn in the VISIBLE_TILES way)

Am I doing something wrong here?

Godot version 4.0.0
in Engine by (12 points)
edited by

So it seems that nodes that don't belong to the tree do not create groups, or there's something wrong somewhere. This, for instance, will return that the group does not exist:

# Create a new node
var my_node = Node.new()

# Add the node to a group
my_node.add_to_group("my_group")

# Check if the group exists
if get_tree().has_group("my_group"):
    # yay!
else:
    # but returns false, my_group does not exist

Is this the expected behaviour or a bug?

Please log in or register to answer this question.

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.