0 votes

i was messing around with open simplex noise and i had functional code but when i restart the project (after 4 or 5 attempts) it says "invalid type in function "set_cell" in base "tilemap". cannor convert argument 3 from nil to int"

extends Node2D

var mapsize = Vector2()
const minsize = 100
const maxsize = 250
var currentseed
var generator = RandomNumberGenerator.new()
var terrain = {
    "water" : 0,
    "sand" : 1,
    "dirt" : 2,
    "grass" : 3,
    "mountain" : 4,
    "snow" : 5,
    "deepwater" : 6
    }
var noise

func RNG():
    generator.randomize()
    currentseed = generator.get_seed()

func mapsizegen():
    randomize()
    mapsize.x = generator.randi_range(minsize, maxsize)
    mapsize.y = generator.randi_range(minsize, maxsize)

func deepwatergen():
    for a in mapsize.x + 20:
        for b in mapsize.y + 20:
            $tilemap.set_cell(a + 20, b + 20, terrain.deepwater)

func watergen():
    for c in mapsize.x + 10:
        for d in mapsize.y + 10:
            $tilemap.set_cell(c + 25, d + 25, terrain.water)

func sandgen():
    for e in mapsize.x + 4:
        for f in mapsize.y + 4:
            $tilemap.set_cell(e + 28, f + 28, terrain.sand)

func mapgen():
    for g in mapsize.x:
        for h in mapsize.y:
            $tilemap.set_cell(g + 30, h + 30,
            _get_tile_index(noise.get_noise_2d(float(g), float(h))))

func _get_tile_index(noise_sample):
    if noise_sample < -0.4:
        return terrain.water
    if noise_sample < -0.2:
        return terrain.sand
    if noise_sample < -0.1:
        return terrain.dirt
    if noise_sample < 0.4:
        return terrain.grass
    if noise_sample < 0.6:
        return terrain.mountain
    if noise_sample < 0.8:
        return terrain.snow

func _ready():
    RNG()
    mapsizegen()
    deepwatergen()
    watergen()
    sandgen()
    randomize()
    noise = OpenSimplexNoise.new()
    noise.seed = currentseed
    noise.octaves = 2
    noise.period = 10
    noise.lacunarity = 1
    noise.persistence = 0.75
    mapgen()

it shows the error at:

$tilemap.set_cell(g + 30, h + 30,
            _get_tile_index(noise.get_noise_2d(float(g), float(h))))

i used set_cellv too but is the same

in Engine by (20 points)

1 Answer

+1 vote
Best answer

Your function

_get_tile_index

can end without a return. (if noise_sample > 0.8)

You could return a default value if it reaches the end of the function.

If you define your function/method like this:

func _get_tile_index(noise_sample : float) -> int:

the editor assumes, that you have to return something (an integer int this case), and will warn you, if nothing could be returned.

(Static Typing)

by (1,534 points)
selected by

thank you very much for your answer

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.