Hi,
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))))
worldTileMap.update_bitmask_region()
worldTileMap.update_dirty_quadrants()
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
else:
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():
add_child(Manifest.world.get("mainTileMap"))
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.