Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Notnasiul |
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 remove_from_group, 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?
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?
Notnasiul | 2023-03-24 20:19