Errors accessing TileMap custom data layers

Godot Version

4.2.2

Question

I have an isometric tileset set up in my game that I have added a Custom Data layer to (layer 0 called TileExits) to hold an integer representing the possible ‘exits’ from that tile - up right / down right / down left / up left or any combination.

I have been trying to access this custom data but I am encountering the following error

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

which also shows the error

Player.gd:135 @ _get_tileData(): Parameter “version” is null.

So I am assuming that the null instance either means that the tile map itself hasn’t yet completed being ‘loaded’ (hence the call_deferred) or that my link to the tile map isn’t correct.

So, my code in regards to this is as follows:

@onready var tile_map: TileMap = $"../TileMapRedraw"

then in the ready function:

func _ready():
call_deferred("_get_tileData")

and lastly in the called function:

func _get_tileData():
var hoveredtile = tile_map.local_to_map(self.position)
var tile_data = tile_map.get_cell_tile_data(0, hoveredtile)
var tile-data2 = tile_data.get_custom_data("TileExits")

Can anyone tell me why I get the errors I get?

Oddly this was working successfully until I heavily edited the ‘TileMapRedraw’ tile set/settings. Would editing the TileMap break something once it’s been linked to?

Ok, after another few hours I found there was one option for the ‘null’ messages that I initially didn’t think of.

That was that the code was working correctly but the location the code was trying to pull the tile’s custom data from didn’t have a tile there, so no data could be pulled and this was generating the ‘null’ error.

With the use of plentiful ‘print_debug’ commands I was finally able to spot that the tile location I was accessing didn’t hold a tile at all, and I now have a better understanding of the layout of the isometric tile map and it’s coordinate system.

To add to this. If you want o access a tile like get_custom_data use this before hand:

if cell_coords in tile_map.get_used_cells():
    "code here"

This checks if the cell is being used.

2 Likes