Godot Version
4.2.1
Question
Hi all,
Fairly new to Godot (and coding) here, and currently stuck with a project I’m working on.
I’m using a 2D TileMap, and I’m working on a “selection box”/indicator that shows the player which tile they’re currently hovering over. I managed to get the selection indicator working fine, however I want to add a feature to it where it switches to another sprite animation if the tile you’re hovering on can’t be accessed by the player.
I’m attempting to do this using a custom data layer that I’ve named “solid” (which I use in layers 1 & 2 of my TileSet). Essentially, what I would like to happen is that if the cursor hovers over a tile with the data layer “solid”, that it would switch sprites, then switch back to the regular sprite when the cursor moves off of the tile.
This part of the code is responsible for translating my selection sprite onto the map, which is working fine on its own:
func _process(delta):
#Places tile selection sprite on tile where mouse is
var cursor_position = tile_map.local_to_map(get_global_mouse_position())
var selected_tile = tile_map.map_to_local(cursor_position)
tile_select.global_position = selected_tile
However here, in the same function, is where it’s giving me issues:
var solid_objects = tile_map.get_cell_tile_data(1, selected_tile)
var solid_walls = tile_map.get_cell_tile_data(2, selected_tile)
if solid_objects.get_custom_data("solid") == true or solid_walls.get_custom_data("solid") == true:
tile_select.play("cannot_select")
else:
tile_select.play("can_select")
For context, “tile_map” here is my TileMap node, and “tile_select” is an AnimatedSprite2D with an animated indicator sprite.
Whenever I run the game, I get the error “Attempt to call function ‘get_custom_data’ in base ‘null instance’ on a null instance.”
If I’m understanding this correctly, both the variables “solid_objects” and “solid_walls” are returning null, but I’m confused as to why they’re doing that.
Any help here would be appreciated!
