Managing tilemap autotiling and collisions.

boruokboruok Posts: 26Member
edited March 27 in Tutorials

By default, Godot Engine doesn't link autotiling between textures in same tilemap.

So, if you whant to achiveve that, you can use script below:

tool
extends TileSet


func _is_tile_bound(id, neighbour_id):
    return neighbour_id in get_tiles_ids()

Still, make sure that you have bitmasking on your textures:


luckily you can copy-paste bitmask.

1 Make a new script and copy-paste code.
2 Save it in you project folder.
3 Go into for your tilemap properties -> tileset -> script -> load -> and point where you saved that script.

Result will be like picture below.

BAD news, if you lazy like me, you didn't maked any collision of your tilemap. For example, drawing collision shapes in TileMap editor for 10+ textures is natural crazyness, plus, if you noticed, adding collision shapes increases your scene or tilemap (depends on your preference) file-size with continious lines, like:

[sub_resource type="ConvexPolygonShape2D" id=1]
points = PoolVector2Array( 16, 16, 0, 16, 0, 0, 16, 0 )

[sub_resource type="ConvexPolygonShape2D" id=2]
points = PoolVector2Array( 16, 16, 0, 16, 0, 0, 16, 0 )

so on... and that for EVERY ids(cells) in your tilemap. for example, my tilemap textures have around 120 ids(cells).
120 * 12 = INSTANITY

GOOD news, i have a solution you can batch collision adding by script:

extends TileMap


func _ready():
    for ts in tile_set.get_tiles_ids():           # for every textures in tileset.
        if tile_set.tile_get_shapes(ts).empty():  # if texture didn't have any collision shapes.
            var shape = ConvexPolygonShape2D.new()
            shape.set_points([Vector2(0, 0), Vector2(16, 0), Vector2(16, 16), Vector2(0, 16)])

            var c = tile_set.tile_get_texture(ts).get_width()  / int(cell_size.x)  # colums
            var r = tile_set.tile_get_texture(ts).get_height() / int(cell_size.y)  # rows

            for i in range(c * r):
                tile_set.tile_add_shape(ts, shape, Transform2D(), false, Vector2(i % c, i / c))

            tile_set.tile_set_shape(ts, ts, shape)

1 Make new script
2 Save it in you project folder
3 Attach script in your tilemap

whats script do? script loops for each cell in texture and add rectangle-shaped convexpolygon collision. And all this in game runtime (script doesn't modifying tilemap file).

Good Luck!


Tags :
Sign In or Register to comment.