The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

0 votes

The setcellv function returns the error "Invalid type in function 'setcellv' 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 grasssimplexnoise
var riversimplexnoise

func ready():
randomize()
#Generate Seed
var Seed = randi()
#Create grass & bushes perlin map
grass
simplexnoise = 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 generateworld():
for x in WIDTH:
for y in HEIGHT:
$TileMap.setcellv(Vector2(x - WIDTH / 2, y - HEIGHT / 2), placegrassandbush(grasssimplexnoise.getnoise2d(float(x), float(y))))
$TileMap.updatebitmaskregion()

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

func generaterivers():
for x in WIDTH:
for y in HEIGHT:
$TileMap.setcellv(Vector2(x - WIDTH / 2, y - HEIGHT / 2), placewater(riversimplexnoise.getnoise2d(float(x), float(y))))
$TileMap.updatebitmaskregion()

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

The grasssimplexnoise works fine but the problem lies in the riversimplexnoise

Thank you in advance

Godot version v.3.2.2 Stable
in Engine by (12 points)

1 Answer

0 votes

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

by (84 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.