When building a TileMap in code with set_cell or set_cellv returns TileMap without tile indexes

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By domiq

I am trying to use perlin noise to setup a TileMap for a game world, however, the below code returns a properly set up TileMap that is empty ( doesn’t have any tiles assigned to the grid ).
Been trying a lot of things, no errors occur, the TileMap gets set up properly but I cannot understand why it is empty. The loops and the method called are also checked and work as is written.
The following code generates the map:

# setup tilemap
	worldTileMap = TileMap.new()
	worldTileMap.mode = TileMap.MODE_CUSTOM
	worldTileMap.cell_size = Vector2(220,127)
	# buid trimetric grid
	var trans = Transform2D( Vector2(float(172), float(-31)), Vector2(float(47), float(103)), Vector2(0.0, 0.0))
	worldTileMap.cell_custom_transform = trans
	worldTileMap.cell_quadrant_size = 16
	worldTileMap.tile_set = load("res://Assets/Images/Tiles/tilesV1bs.tres")
	# generate tileset out of noise
	for x in sizeX:
		for y in sizeY:
			worldTileMap.set_cellv(Vector2(sizeX, sizeY), get_tile_index(noise.get_noise_2d(float(sizeX),float(sizeY))))
	world["mainTileMap"] = worldTileMap
# returns corresponding index for a tileset
func get_tile_index(sample):
	# setting up tiles depending on world type
	var tiles = {
		"grass": 2,
		"rock": 0,
		"sand": 3,
		"water": 1,
		"mountain": 4
if sample < 0.2:
		return tiles.water
	elif sample < 0.3:
		return tiles.sand
	elif sample < 0.6:
		return tiles.grass
	elif sample < 0.8:
		return tiles.rock
		return tiles.mountain

Afterwards, I add a scene to root and in the scene the following code adds a child TileMap from above:

extends Node2D

var currentState

func _ready():

Sorry if the code is a little badly formatted from pasting, but its syntax is proper and working.

So the end result is a nicely setup TileMap node within scene tree in-game, but it doesn’t draw any tiles. After exporting a runtime TileMap, I can see that all of its indexes are null: [0,0, null], [1,0, null] and so on.

The code for map generation is separate so it can during a loading screen and threaded, it shares the world{} to the Manifest, an autoloaded object.

Note that even if replace the method call in the nested for loop with a static index, it does not work.

:bust_in_silhouette: Reply From: Zylann
for x in sizeX:
    for y in sizeY:
        worldTileMap.set_cellv(Vector2(sizeX, sizeY)

You are not using x and y.



You are not using x and y either, it will return always the same value.

Oh, that’s a really silly mistake, thank you for the heads up.

domiq | 2019-10-30 16:36