0
\$\begingroup\$

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?

\$\endgroup\$
0
\$\begingroup\$

Sometimes, you just need a break to see what you were missing. I had the code all wrong. After the lightbulb went off, here's what I came up with:

func _input(event):
if (event is InputEventMouse && buildOn == true):
    var button = event.get_button_mask()
    if (button == 1):
        var name = _tileset.tile_get_name(14)
        var oldRect = get_used_cells_by_id(14)
        var i = 0
        while (i < oldRect.size()):
            set_cellv(oldRect[i],-1)
            i = i + 1
        var tile_pos = world_to_map(event.position)
        if (dragStart == Vector2(-1,-1)):
            dragStart = tile_pos
        if (dragStart != Vector2(-1,-1)):
            var newX = dragStart.x - tile_pos.x
            var next = Vector2(-1,-1)
            var startX = dragStart.x
            while (newX != 0):
                var newY = dragStart.y - tile_pos.y
                var startY = dragStart.y
                if newX > 0:
                    while (newY != 0):
                        if newY > 0:
                            next = Vector2(startX,startY)
                            newY = newY - 1
                            startY = startY - 1
                        elif newY < 0:
                            next = Vector2(startX,startY)
                            newY = newY + 1
                            startY = startY + 1
                        set_cellv(next, _tileset.find_tile_by_name(name))
                    if newY == 0:
                        next = Vector2(startX,startY)
                        set_cellv(next, _tileset.find_tile_by_name(name))
                    newX = newX - 1
                    startX = startX - 1
                elif newX < 0:
                    while (newY != 0):
                        if newY > 0:
                            next = Vector2(startX,startY)
                            newY = newY - 1
                            startY = startY - 1
                        elif newY < 0:
                            next = Vector2(startX,startY)
                            newY = newY + 1
                            startY = startY + 1
                        set_cellv(next, _tileset.find_tile_by_name(name))
                    if newY == 0:
                        next = Vector2(startX,startY)
                        set_cellv(next, _tileset.find_tile_by_name(name))
                    newX = newX + 1
                    startX = startX + 1
            if newX == 0:
                var newY = dragStart.y - tile_pos.y
                var startY = dragStart.y
                while (newY != 0):
                    if newY > 0:
                        next = Vector2(startX,startY)
                        newY = newY - 1
                        startY = startY - 1
                    elif newY < 0:
                        next = Vector2(startX,startY)
                        newY = newY + 1
                        startY = startY + 1
                    set_cellv(next, _tileset.find_tile_by_name(name))
                if newY == 0:
                    next = Vector2(startX,startY)
                    set_cellv(next, _tileset.find_tile_by_name(name))
    elif (button == 0 && dragStart != Vector2(-1,-1)):
        var name = _tileset.tile_get_name(15)
        var tile_pos = world_to_map(event.position)
        if (dragStart != Vector2(-1,-1) && dragStart != tile_pos):
            var newX = dragStart.x - tile_pos.x
            var next = Vector2(-1,-1)
            var startX = dragStart.x
            while (newX != 0):
                var newY = dragStart.y - tile_pos.y
                var startY = dragStart.y
                if newX > 0:
                    while (newY != 0):
                        if newY > 0:
                            next = Vector2(startX,startY)
                            newY = newY - 1
                            startY = startY - 1
                        elif newY < 0:
                            next = Vector2(startX,startY)
                            newY = newY + 1
                            startY = startY + 1
                        set_cellv(next, _tileset.find_tile_by_name(name))
                    if newY == 0:
                        next = Vector2(startX,startY)
                        set_cellv(next, _tileset.find_tile_by_name(name))
                    newX = newX - 1
                    startX = startX - 1
                elif newX < 0:
                    while (newY != 0):
                        if newY > 0:
                            next = Vector2(startX,startY)
                            newY = newY - 1
                            startY = startY - 1
                        elif newY < 0:
                            next = Vector2(startX,startY)
                            newY = newY + 1
                            startY = startY + 1
                        set_cellv(next, _tileset.find_tile_by_name(name))
                    if newY == 0:
                        next = Vector2(startX,startY)
                        set_cellv(next, _tileset.find_tile_by_name(name))
                    newX = newX + 1
                    startX = startX + 1
            if newX == 0:
                var newY = dragStart.y - tile_pos.y
                var startY = dragStart.y
                while (newY != 0):
                    if newY > 0:
                        next = Vector2(startX,startY)
                        newY = newY - 1
                        startY = startY - 1
                    elif newY < 0:
                        next = Vector2(startX,startY)
                        newY = newY + 1
                        startY = startY + 1
                    set_cellv(next, _tileset.find_tile_by_name(name))
                if newY == 0:
                    next = Vector2(startX,startY)
                    set_cellv(next, _tileset.find_tile_by_name(name))
            dragStart = Vector2(-1,-1)
            #set_cellv(next, _tileset.find_tile_by_name(name))
    else:
        dragStart = Vector2(-1,-1)

This code will let you left-click and drag across the tilemap, filling each cell with a tile from the tile set, then changing the tiles in all of the cells to another tile when the button is released. Works great from what I can see, but if anyone has some pointers to improve performance or clean up my code, please share!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.