How to get an autotile on GDScript?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By EmmanFK9503

I worder if there’s a way to get a autotile just like a single tile using GDScript.
Someone can help me, please?

:bust_in_silhouette: Reply From: kidscancode

There’s not an official tutorial for autotile, but this may help you:
http://kidscancode.org/godot_recipes/2d/autotile_intro/

I don’t think this is what he meant to ask.
Currently, when you are trying to set tiles via code, you have to manually specify a vector representing a bitmask index required to select a sprite corresponding to autotile.
There is no way to put a sprite in the valid fashion from the very start, but what you can do, is to put all the sprites in the corresponding positions and call update_bitmask_region after that.

Here is an example from my code:

private void InstanciateTilemap(Node scene)
    {
        var set = GD.Load<TileSet>("res://TileSets/Default.tres");

        var map =
            new TileMap
            {
                TileSet = set,
                CellSize = new Vector2(32, 32),
                CellQuadrantSize = 16
            };
        
        var size = OS.WindowSize;
        
        for(var horizontalIndex = 0; horizontalIndex < 16; horizontalIndex++)
         for(var verticalIndex = 0; verticalIndex < 16; verticalIndex++)
            map.SetCell(
                horizontalIndex,
                verticalIndex,
                1
            );

        map.UpdateBitmaskRegion(
            new Vector2(),
            size
        );

        scene.AddChild(map);
    }
}

Source: Autotile set_cellv & set_cell while game is running must trigger autotile adjustments · Issue #13960 · godotengine/godot · GitHub

Aemilius | 2020-03-22 22:19