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

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?