Fully functioning node shows as Nil

Godot Version

4.2.1. stable

Question

I get this error:

Invalid call. Nonexistent function ‘get_tile’ in base ‘Nil’.

Here is the function:

func get_adjacent_buildings(coords: Vector2i, distance: int) -> Array[Building]:
var adjacent_buildings: Array[Building] = []
for i in range(-distance, distance + 1):
	for j in range(-distance, distance + 1):
		var tile = $"../TerrainMap".get_tile(Vector2i(coords) + Vector2i(i, j))

I’ve tried with unique name, @onready, even get_parent().get_node(“TerrainMap”), tried assigning it in _ready() and more:

When I do get_parent().get_node(“TerrainMap”), this error claims that Main is null:

Cannot call method ‘get_node’ on a null value.

If i put the function in TerrainMap and run it from there instead, I get:

Attempt to call function ‘get_adjacent_buildings’ in base ‘null instance’ on a null instance.

The node with the script and TerrainMap are siblings under Main. Both nodes are created at game start, but get_adjacent_buildings() is called later, after user input (placing a specific building). TerrainMap is found working fine in Remote scene tree at all times. So is Main, needless to say. The rest of the game is working fine.

Why can’t I access TerrainMap?! I’ve done it a million times before, and I’m doing it the same way here as far as I can tell…

Show a print of your scene organization

I’ve included TaggerComponent, as this is what calls get_adjacent_buildings(), like this:

func _ready():
	adjacency_mng = AdjacencyManager.new()
	tag_valid()

func tag_valid() -> void:
	for building in adjacency_mng.get_adjacent_buildings(coords, range):
        ...

AdjacencyManager is the node with get_adjacent_buildings(). I’ve also tried it like this:

func _ready():
	adjacency_mng = %AdjacencyManager
	tag_valid()

Other nodes are not involved, things like Camera, Player, UI

Where are you setting it to use a unique name?

In the scene tree. I guess it doesn’t show in the Remote view of the scene tree, and I forgot to mention that the screenshot was from Remote view. Here is the Local view:

When are you calling this?

You might try storing the TerrainMap in an exported variable

Oops, apparently I’ve overlooked this little detail about same-scene limitation:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.