I'm creating my first game in Godot 3.1, a top-down 2D sim game where you can build rooms on a space station. I am using a tileset I created inside a TileMap node. I placed a script inside the node that checks for the mouse position in the world, converts it into a tilemap coordinate, then loads a tile into that tilemap coordinate.
func _input(event):
if (event is InputEventMouse):
var button = event.get_button_mask()
if (button == 1):
var tile_pos = world_to_map(event.position)
var name = _tileset.tile_get_name(0)
set_cellv(tile_pos, _tileset.find_tile_by_name(name))
Works like a champ! As long as the left mouse button is held down, any tile the mouse pointer touches is filled with the tile from the tileset.
Now I am trying to expand this function so that I can click the world and drag across several tiles to form a rectangle or square, then have all of the tiles inside the selected area filled with the selected tile.
func _input(event):
if (event is InputEventMouse):
var button = event.get_button_mask()
if (button == 1):
var tile_pos = world_to_map(event.position)
if (dragStart == Vector2(-1,-1)):
dragStart = tile_pos
var name = _tileset.tile_get_name(0)
if (dragStart != Vector2(-1,-1) && dragStart != tile_pos):
var newX = dragStart.x - tile_pos.x
var newY = dragStart.y - tile_pos.y
while (newX != 0 && newY != 0):
var next = Vector2(-1,-1)
if newX > 0:
next = Vector2(dragStart.x - 1,dragStart.y)
newX = newX - 1
elif newX < 0:
next = Vector2(dragStart.x + 1,dragStart.y)
newX = newX + 1
elif newX == 0:
if newY > 0:
next = Vector2(dragStart.x,dragStart.y - 1)
newY = newY - 1
elif newY < 0:
next = Vector2(dragStart.x,dragStart.y + 1)
newY = newY + 1
print(next)
set_cellv(next, _tileset.find_tile_by_name(name))
else:
dragStart = Vector2(-1,-1)
However, it seems to only load some of the tiles and functions sporadically. Am I not understanding how the system iterates through the while loop or am I missing something else?