Character will not move at all

Godot Version

4.2.2

Question

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

line 28 seems to be point to an error, do you get an error and the game halts? Could you show that error message?

1 Like

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 to move upwards out of bounds it gives me this error.

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.

是的,我做了输入映射,并仔细检查了一遍.

You could use

# 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.

1 Like

the tiledata outside the tile is null, which gives the error. either do not let player move outside of tile or change line 28 format:

if !tile_data: return

2 Likes

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.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.