Trying to place cells on a TileMapLayer, but nothing is happening

Godot Version

4.4.1.stable

Question

I am trying to use the set_cell() method to place city markers around the world map, but nothing is showing. Below is my code:

extends TileMapLayer

var cities_array = [
	"Los Angeles",
	"San Fransisco"
]

var cities_location_array = [
	Vector2(78, 167),
	Vector2(75, 163)
]

func populate_cities():
	for a in range(cities_array.size()):
		self.set_cell(local_to_map(cities_location_array[a]), 0, Vector2i(0, 0))

You’re not calling your populate_cities() function anywhere in your code. You can add this piece of code and it should place your tiles now:

func _ready() -> void:
	populate_cities()

You don’t need self. here; it’s implicit.

You also don’t need range() unless you’re walking the array in an unusual order; for i in array.size() works.

Assuming @wchc is right and nothing is calling populate_cities(), the fix should be making sure that function gets called. If you are calling it somewhere and you aren’t getting the results you’re expecting, there’s a couple of things to check:

  • Are your tile coords what you expect? Maybe do something like:
func populate_cities() -> void:
    for city in cities_array:
        var map_loc: Vector2i = local_to_map(cities_location_array[city])
        print("New city @ (%d %d)" % [map_loc.x, map_loc.y])
        # You may want to be able to change tiles per city or something...
        var tile: Vector2i = Vector2i(0, 0) # city_tile_array[city]
        set_cell(map_loc, 0, tile)
  • Are you sure (0, 0) is a valid tile in your tileset? If it’s not explicitly marked in the tileset as being in use, you’ll get a blank tile…

The function “populate_cities” is called, just not in the same node. I updated my code accordingly, just for convenience. I tried the code you suggested and the following is shown in the output:
image
I find this odd, because the coordinates in my array are based on what I found on the tilemaplayer screen (not to mention the coordinates for both tiles are different in my array):

But even at the (4, 10) coordinates, I don’t see the tile I am using as a city marker. Is the tilemaplayer “behind” the background image or something? This is how I have the nodes organized so far:


Also, I can confirm that the atlas coordinates for the tile is correct. It’s source_id is 0 also. Below is my updated code so far:

extends TileMapLayer

var cities_array = [
	"Los Angeles",
	"San Fransisco"
]

var cities_location_array = [
	Vector2(78, 167),
	Vector2(75, 163)
]

func _ready():
	populate_cities()

func populate_cities():
	for a in cities_array.size():
		var map_loc: Vector2i = local_to_map(cities_location_array[a])
		print("New city @ (%d %d)" % [map_loc.x, map_loc.y])
		var tile: Vector2i = Vector2i(0, 0)
		set_cell(map_loc, 0, tile)

The TileMapLayer may be in front of or behind your background image, depending on z_index (see “Ordering->Z Index” in the inspector panel). Make sure your TileMapLayer is at a higher value for Z Index than your background image. I think you can also do this with ordering in the node tree as well, but explicit ordering should be more reliable.

1 Like

That solved it! I also did some more tinkering and found the “to_local” method placed the city markers where I intended.

2 Likes