Why is my character not moving?
I am copying code from a YouTube tut about smooth grid-based movement and our code is the exact same but his character moves and mine doesn’t. His version is 4.1.1, which isn’t far apart from mine so I don’t think that would make a difference but if it is the reason lmk. This is the tut if anyone is interested: https://www.youtube.com/watch?v=9u1Dq6h7sGU&t=529s
I think there’s a crazy reason. The tile_map.get_custom_data("walkable") is something like null, which will be treated as false. I don’t know whether this is possible in GDScript or not, this often happens in C++.
Other things to be noted is that some improvements could be adopted.
# Notice that the direction itself is a Vector2i (an int Vector2)
func move(Vector2i direction) -> void:
# ...
# We can simply add up current_tile (a Vector2i)
# and direction (also a Vector2i), since both are Vector2i
var target_tile: Vector2i = current_tile + direction
# ...
Yeah whenever I try moving upwards out of bounds it gives me this error: Cannot call method ‘get_custom_data’ on a null value. Do you know how to fix this?? Also thanks for the improvements.
# When tile_data is null, which is a value treated as false (or a falsy value),
# the value of "not tile_data" will be true, we skip
if not tile_data:
return
# Else, the tile_data isn't null. Then if the walkable data is false, we also skip
elif not tile_data.get_custom_data("walkable"):
return
I hope you already know what the not keyword does, it flips true to false, and false to true.
This almost works flawlessly! My character can move now, but he bypasses some walls and doesn’t on others. It almost like it’s ignoring the tileset completly and is instead trapped in an invisible box. Changing his starting position also changes the boxes perimeters. It’s super weird.