Godot Version
4.2.2
Question
Hi! I’m new to Godot and I’m trying to make a basic maze game. I have different color tiles in TileMap for the path you can go on and the walls and I’m trying to have the player check the color of each tile before going on it, to see if its a wall, in which case it cant go there, or a path. I started doing this by adding a custom data layer called “color” and I entered “wall” for the wall blocks and “path” for the others.
However, when I use get_cell_tile_data it returns null. I’ve checked that the tile coords inputted into get_cell_tile_data are real, and I don’t think I have multiple layers on the TileMap.
Here’s my code. It’s in the player scene:
extends CharacterBody2D
@onready var tilemap
const tile_size = 64
var input_direction
var moving = false
func _ready():
var player = get_parent()
var main = player.get_parent()
tilemap = main.get_node("TileMap")
if tilemap == null:
print("TileMap is null, check path!")
else:
print("TileMap successfully found!")
func _physics_process(delta):
#move
input_direction = Vector2.ZERO
if Input.is_action_just_pressed("down"):
input_direction = Vector2.DOWN
move()
elif Input.is_action_just_pressed("up"):
input_direction = Vector2.UP
move()
elif Input.is_action_just_pressed("right"):
input_direction = Vector2.RIGHT
move()
elif Input.is_action_just_pressed("left"):
input_direction = Vector2.LEFT
move()
#screen clamping
global_position.x=clamp(global_position.x,32,1120)
global_position.y=clamp(global_position.y,32,608)
func move():
print(tilemap)
if tilemap != null:
var target_pos = global_position+tile_size*input_direction
var tile_coords = tilemap.local_to_map(target_pos)
var tile_data = tilemap.get_cell_tile_data(0,tile_coords)
print(tile_coords)
print(tile_data)
# Get the tile's metadata (color property)
var tile_color = tile_data.get("color")
print(tile_color)
if input_direction and not moving and tile_color=='path':
moving = true
print("move")
var tween= create_tween()
tween.tween_property(self,"global_position",target_pos,0.1).set_trans(Tween.TRANS_LINEAR).set_ease(Tween.EASE_IN_OUT)
tween.tween_callback(move_false)
else:
print("wall")
else:
print("null")
func move_false():
moving = false
Main scene:
Player scene:
Please help, I’m very new so I have basically no idea what I’m doing and I’ve been trying to find an answer for hours. Thanks!