User Interaction with TileSets

Godot Version

4.1.1

Question

I am making an isometric 2D city-builder, and I am attempting to implement the idea that when the player clicks somewhere on the map, if the tile is valid (has ground underneath it), the player can place a “house” tile (tile id = 1) in the “Place-able” tilemap.

I haven’t implemented the validity yet, and I read the docs and have no idea still how set_cell() works. I also want to make it so if there happens to be trees (in the “Resources” tilemap) on that tile, they should disappear when the house is placed. Thanks for the help!

Code attached to “Place-able” tilemap:

extends TileMap

var tilemap_position
var tile_to_place = "res://pixil-frame-0 - 2024-04-19T104520.869.png"

func _ready():
	pass

func _process(delta):
	pass
func _input(event):
   # Mouse in viewport coordinates.
	if event is InputEventMouseButton:
		print("Mouse Click/Unclick at: ", event.position)
		tilemap_position = event.position
		set_cell(1, tilemap_position, -1, Vector2i(0, 0), 0)
		$"../Resources".set_cell(???)

You re setting the tileMap " tileMap Source" to -1
I think it starts at 0, in the “tileSet” menu you can see the images on the left.

extends TileMap

var arrayTile = [ Vector2(6,1), Vector2(7,1), Vector2(8,1) ];

var frame1 = 0;
var speed = 0.5;


func _ready():
	pass # Replace with function body.

func _process(delta):
	if ( frame1 < 3 ):
		frame1 += 1.5 * delta;
		if ( frame1 > 3 ):
			frame1 = 0;
	
	set_cell(0, Vector2(11,6), 0, arrayTile[ frame1 ] );
	#1-the layer nr ,2-level coords, 3-tileMap Source, 4-tileMap coords

this is a small animation made with tiles:
the “var arrayTile”, picks up the position of 3 tiles in the tileMap editor
"frame1 " loops trow those 3 tiles… Its the only example i have

I’ve recoded the line

set_cell(0, tilemap_position, 0, Vector2i(1,1))

It doesn’t throw an error anymore, but how do I know the TileMap Source and TileMap Coords?

the tile map source is in “TileSet” its the position of the image, it can go from 0, 1, 2, 3, 4 etc… depending of how many images you have.
the coords you can hover the mouse in the “tile” and it will give you the position of that “tile”

You place the house in layer 1 if you have the trees on that layer, it will replace the already existing tiles… If you place it on layer 0 and the floor is there, it will draw a hole on the floor if the images dont match.

Thanks! I got it working.