create a variable out of a child node?

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

I get an error, set cell on null instance:

extends Node2D

var randNumber : int 

func _ready():
	randomize()
	randNumber = randi() % 100 + 1
	
	var wall = get_node("../TileMap")
	var grass = get_node("../TileMap2")
	if randNumber > 50:
		wall.set_cell(0,4,-1)
		grass.set_cell(0,4,0)
		
		wall.update_bitmask_area(Vector2(0,4))
		grass.update_bitmask_area(Vector2(0,4))

I also tried with: - after extends Node2D, same error

onready var wall = get_node("../TileMap")
onready var grass = get_node("../TileMap2")

so… how to put a child node in a variable?
I know, I could just go in and do $TileMap.set cell and $TileMap.update_bitmask_area and it would work but I’m used to put everything in a variable.

If TileMap and TileMap2 are childnodes of the node with this script, you should leave out the ../ and use get_node("TileMap") and get_node("TileMap2").

omggomb | 2021-07-28 19:19