Godot Version
4.5 (Steam)
Question
So, I’m trying to create chunk loading code for a game. I have the chunks loading, but they are on different, but identically named, TileMapLayers. Instead of the standard move via move_and_slide or move_and_collide, I have opted for a somewhat different and more direct approach (which may be my first mistake), which involves checking a tile for tile data (specifically the boolean “walkable”) and then manually setting its position so that it moves to the exact center of the tile each time - instead of stopping next to collision objects as I have found it to do otherwise.
My problem arises when I try to move from one chunk, or one TileMapLayer, to another. I know it’s possible to detect a TileMapLayer with collisions. I have that working on the initial TileMapLayer, but I am unable to get it working for tertiary TileMapLayers.
My code is essentially this:
Character with a CharacterBody2D, a CollisionShape2D, two RayCast2D (one for entity detection, the other for TileMapLayer detection), a Sprite2D, and a Camera2D.
Before my “move” function moves the player, I have it check if the RayCast2D (responsible for detecting TileMapLayers) detects a TileMapLayer (which it should always do), and assign the current layer to that layer.
Code:
var oldLand = land
tile_map_layer_detector.force_raycast_update()
print("Is colliding: ",tile_map_layer_detector.is_colliding())
if tile_map_layer_detector.is_colliding():
var col = tile_map_layer_detector.get_collider()
print("'col' is =: ",col)
if col is TileMapLayer:
land = col
print("'land' is =: ",land, " 'oldLand' is +: ", oldLand)
I think the problem arises when it is getting the new tile:
Code:
var current_tile: Vector2i = land.local_to_map(global_position)
# Get target tile Vector2i
var target_tile: Vector2i = Vector2i(
current_tile.x + heading.x,
current_tile.y + heading.y
)
As I am coming from off the TileMapLayer, but where it always crashes is at
Code:
var tile_data: TileData = land.get_cell_tile_data(target_tile)
if tile_data.get_custom_data("walkable") == false:
return
Ultimately, at this point, I’ve no idea what’s wrong or how to fix this. Perhaps use a single TileMapLayer and paint to its values instead, but that would eventually get to the end of the TileMapLayer’s available space.
If anyone knows what I’m doing wrong, or has a link to a Chunk loading (and saving) tutorial that I can follow, it would be much appreciated.
P.S. I’ve been testing throughout writing this, and am fairly sure that the above is accurate and correct. By this I mean that I’ve verified what I believe to be the issues, but as I am new to this, I ‘could be wrong’… Also, I could not figure out if “preformatted text” was “code” text, and since this doesn’t have a “preview” I’m making an assumption…