0 votes

Hello everyone, I'm creating a 2D strategy, but I don't know how I can interact with the tilemap in the game, for example, so that when I click on a certain tile, it changes

in Engine by (20 points)

1 Answer

0 votes

Here is an example. I haven't tested the code so there might be issues. Also it's not pretty. :D

if event is InputEventMouseButton:
    if event.pressed:
        var pos = event.position
        var x = int(pos.x / 20)
        var y = int(pos.y / 20)
        var clicked_tile_pos = Vector2(x, y)
        # gets the tile index from the tileset in the given position
        var current_tile = map.get_cellv(clicked_tile_pos)
        # sets the tile to index in the given position
        map.set_cellv(clicked_tile_pos , new_tile)

Hopefully this helps.

by (80 points)
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.