0 votes

How would I use get_used_cells()to generate an array of row and an array of column positions based on a tile position?

Tile position from mouse

func get_n_pos():
var grid_tile = world_to_map(get_local_mouse_pos())
return grid_tile

A concept that I've been trying to adapt based on what I know so far...

func get_cells_row(tile_pos):
var all = get_used_cells()
var new_array = []
for r in all:
    for y in range(0,9):
        all.find(tile_pos,y)
        new_array.append(r)
        all.pop_front()
    #if tile_pos:
        #new_array.append(r)
print(new_array)
return new_array
in Engine by (33 points)

1 Answer

+2 votes
Best answer

Is it for a "bomberman-like" rows and columns query?
Do you really want every single tile in the tilemap width and height?

Using get_used_cell(), you can do this, but it will be slower on big tilemaps. If you have a small tilemap and you don't do this every frame, it should be ok:

func get_cell_row(tile_pos):
    var row = []
    var used_cells = tilemap.get_used_cells()
    for cell in used_cells:
        if cell.y == tile_pos.y:
            row.append(cell)
    return row

Alternatively, if you know the extents of your tilemap, you can do this instead, which is faster because it queries only a row instead of every tile:

func get_cell_row(tile_pos):
    var row = []
    for x in range(tilemap_min_x, tilemap_max_x):
        if tilemap.get_cell(x, tile_pos.y) != -1:
            row.append(Vector2(x, tile_pos.y))
    return row

If you want tiles in a rectangular region, you need a different if test in the first version, and a double-for in the second version.

In your second code you used all.find(tile_pos,y), which is not going to do what you want. If it ever works, what it does instead is "find the index in the list at which the value tile_pos is, ignoring all indexes before y, then return it as an integer", which you aren't storing anyways. Not what you asked at all, right^^

by (28,788 points)
selected by
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.