What is wrong with my procedural generation code?

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

The set_cellv function returns the error “Invalid type in function ‘set_cellv’ in base ‘TileMap’. Cannot convert argument 2 from Nil to int”

Here is my code:

extends Node2D

const WIDTH = 500
const HEIGHT = 500

const TILES = {
‘grass’: 0,
‘water’: 1,
‘dirt’: 2,
‘tree’: 3,
‘bush’: 4
}

var grass_simplex_noise
var river_simplex_noise

func _ready():
randomize()
#Generate Seed
var Seed = randi()
#Create grass & bushes perlin map
grass_simplex_noise = OpenSimplexNoise.new()
grass_simplex_noise.seed = Seed

grass_simplex_noise.octaves = 4
grass_simplex_noise.period = 8
grass_simplex_noise.lacunarity = 2
grass_simplex_noise.persistence = 0.5
#Replace Tiles
_generate_world()
#Create River perlin worms
river_simplex_noise = OpenSimplexNoise.new()
river_simplex_noise.seed = Seed

river_simplex_noise.octaves = 1
river_simplex_noise.period = 1
river_simplex_noise.lacunarity = 1
river_simplex_noise.persistence = 1
#Replace Tiles
_generate_rivers()

func _generate_world():
for x in WIDTH:
for y in HEIGHT:
$TileMap.set_cellv(Vector2(x - WIDTH / 2, y - HEIGHT / 2), place_grass_and_bush(grass_simplex_noise.get_noise_2d(float(x), float(y))))
$TileMap.update_bitmask_region()

func place_grass_and_bush(noise_sample):
if noise_sample < 0.4:
return TILES.grass
return TILES.bush

func _generate_rivers():
for x in WIDTH:
for y in HEIGHT:
$TileMap.set_cellv(Vector2(x - WIDTH / 2, y - HEIGHT / 2), place_water(river_simplex_noise.get_noise_2d(float(x), float(y))))
$TileMap.update_bitmask_region()

func place_water(noise_sample):
if noise_sample < 0.7:
return TILES.water

The grass_simplex_noise works fine but the problem lies in the river_simplex_noise

Thank you in advance

:bust_in_silhouette: Reply From: Cody Sass

setcellv function returns the error “Invalid type in function ‘setcellv’ in base ‘TileMap’. Cannot convert argument 2 from Nil to int”

setcellv was expecting an int. But it found NIL (an empty nothing that wasn’t set) the only values you have in setcellv is a vector and an int. So the problem MUST be in placewater(riversimplexnoise.getnoise2d(float(x), float(y)))
it did not return a int.

How can placewater not return an int?
well first off, if noise_sample is > .7 It doesn’t return anything, so that’s a problem. if noisesample is not a number, we won’t return anything. (because if it’s not a number it won’t be lower than .7)
right there is 2 potential problems.

you say grass is working fine. I can’t help but notice that if it fails its check it always returns TILES.bush. Really I don’t think you wanna split placegrassandbush and placewater in separate functions