Data Layer on TileMap: Cannot call method 'get_custom_data' on a null value error

Godot Version

4.2.1

Question

Hello my good friends, I am new to Godot, and I am trying to get my CharicterBody2d to move across a TileMap. I wanted there to be tiles that they could walk across and tiles that they can’t, so I created a Custom Data Layer called ‘Walkable’ (set to bool). However, when I run the scene and try moving, it crashes and I get the error “Cannot call method ‘get_custom_data’ on a null value” I think this has something to do with the TileMap/CharBody not being properly initialized before trying to use it, but I am not sure and new to Godot so I’m unsure how to fix this issue.

This is the code that I have attached to the CharBody2d

extends CharacterBody2D

@onready var tile_map = $".."

func _process(_delta):
	if Input.is_action_pressed("up"):
		move(Vector2.UP)
	elif Input.is_action_pressed("down"):
		move(Vector2.DOWN)
	elif Input.is_action_pressed("left"):
		move(Vector2.LEFT)
	elif Input.is_action_pressed("right"):
		move(Vector2.RIGHT)


func move(direction: Vector2):
	#Get current tile
	var current_tile: Vector2i = tile_map.local_to_map(global_position)
	# Get target tile
	var target_tile: Vector2i = Vector2i(
		int(current_tile.x + direction.x),
		int(current_tile.y + direction.y),
	)
	prints(current_tile, target_tile)
	#Get custom data layer
	var tile_data: TileData = tile_map.get_cell_tile_data(0, target_tile)

	if tile_data.get_custom_data("Walkable") == false:
		return
	#move plr
	global_position = tile_map.map_to_local(target_tile)

Thank you in advance for anything that could help!

Without seeing your scene tree, my guess is that your character’s global position and local position may be different? If the tilemap wasn’t ready, you would have gotten an error trying to access it in the first place!

Maybe try

	var current_tile: Vector2i = tile_map.local_to_map(tile_map.to_local(global_position))

image

Here is my scene tree, I used your code to replace my ‘get current tile’ and it did cause the sprite to move, but it would jump an unpainted tile (regardless of what key pressed, and the same tile each time) and then crash and give the same message. I tried to paint the tile it jumped too and run it again to see what would happen, but the sprite would then just jump to another unpainted tile, (this time seemingly at random, always in a (-x,+y) direction) and crash.

My ‘plr’ scene also has a CollisionShape2D, Sprite, and Camera.

I also have a script that prints the coordinates of a tile when you click on it, and in doing this I noticed it would print coordinates different than what the coordinates would be of the same tile in the 2d view of the scene, not sure if that’s worth noting but maybe, sorry I’m kina clueless.

Huh, strange. I ran it on mine and didn’t have any issues until I ran into a tile that didn’t exist.

Do you perhaps have multiple layers in your tilemap?

1 Like

I ended up getting it to work, but I had to create a new scene and make sure the TileMap was a child node of a reguler Node2D, and the Player node was a child of the TileMap, like as follows, doing do seemed to fix my problem. Thank you very much for your help!

image

1 Like