Help Attempt to call function 'set_cell' in base 'null instance' on a null instance.

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

I’m new with godot. im trying to generate a world with perlin noise but i cant set the tiles of the tilemap because is like my tilemap is pointing to null or something

const ANCHO = 100
const ALTO = 100
var open_simplex_noise
const TILES = {"pasto":5 , "agua":0 , "tierra":6}
onready var grilla = $TileMap

func _ready():
	randomize()
	open_simplex_noise = OpenSimplexNoise.new()
	open_simplex_noise.seed = randi()
	open_simplex_noise.octaves = 4
	open_simplex_noise.period = 256
	open_simplex_noise.persistence = 0.349
	_generate_world()

func _generate_world():
	var tipo_celda
	for x in ANCHO:
		for y in ALTO:
			tipo_celda = _generate_cell_type(open_simplex_noise.get_noise_2d(float(x),float(y)))
			grilla.set_cell(x,y,tipo_celda)
func _generate_cell_type(noise_num):
	if noise_num < 0.5:
		return TILES.pasto
	elif noise_num < 0.6 && noise_num > 0.5:
		return TILES.tierra
	else:
		return TILES.agua

the var grilla keeps null and thats generating the error but i dont know why it keeps null after the assignment

Hi,
print(grilla)
See that it’s set to a Tilemap.
If it’s not… maybe use find_node to get the node.

deaton64 | 2020-08-18 23:04

it pointed null with find_node worked fined. thanks

Insanee | 2020-08-19 19:44

:bust_in_silhouette: Reply From: njamster

it pointed null with find_node worked fined. thanks

Note that find_node looks through all child-nodes, so (depending on your tree and how often you call the method) it can be a lot more inefficient than using get_node! If get_node returned null, that simply means you used the wrong path. You can use the following code snippet to get the correct path:

var node = find_node("<NodeName>")
print(node.get_path())           # prints the absolute path
print(self.get_path_to(node))    # prints the relative path