Skip to content

How It Works

This page explains what happens under the inspector: how an autotile picks its picture, how the map is drawn and stored, and how tile positions map to world positions. Read it when you want to write scripts against the map, or when something behaves in a way the other pages do not explain.

Three pieces

Three classes hold the tool together.

  • AutoTileset is an index over one atlas texture. It knows where every tile's picture sits in the atlas, which tiles have transparent pixels, and each tile's collision type. It stores no pixels itself.
  • AutoTileMapData is the saved map: its size, its layers and their tiles.
  • AutoTileMap is the component in the scene. It loads the Map Data, works out autotiling, answers collision questions and runs fog of war. It does not draw anything itself. Meshes under a separate GameObject do the drawing.

RpgMapHelper wraps the common questions for your own scripts. See Scripting.

Tile numbers

Every tile in a tileset has one number, counted across all its sub-tilesets in slot order. The first sub-tileset has 0 to 255, the next 256 to 511, and so on. An empty cell holds -1. Tilesets shows how the 256 tiles of a sub-tileset are laid out.

Autotiling

An autotile does not have one picture. Each time it or a neighbour changes, the map works out its picture again from the cells around it.

The cell is split into four quarters. Each quarter looks at three neighbours on its own side: the one above or below it, the one to its left or right, and the diagonal between them. The top left quarter, for example, looks up, left and up-left.

Each neighbour either holds the same tile or does not. From those three answers the quarter picks one of five shapes:

Up or down Left or right Diagonal Shape of the quarter
same same different Inner corner
different different any Outer corner
same same same Interior
different same any Horizontal edge
same different any Vertical edge

The quarter then takes the matching quarter-tile piece from the autotile's block on the RPG Maker sheet. Four pieces, chosen separately, make the finished tile. That is why a single RPG Maker autotile block can draw a lake of any shape.

Two details decide how edges look:

  • Same means the same tile number. Two different grass autotiles do not join each other. Each draws its border against the other, as it would against empty ground.
  • The map edge counts as the same tile. An autotile painted against the border of the map continues past it and draws no edge there. An empty cell counts as different, so an autotile next to one does get its border.

Plain tiles, the A5 sheet and the B to E sheets, skip all of this and draw their whole picture.

Building (A3) and wall (A4) autotiles follow RPG Maker's own layouts for those sheets, which use fewer pieces than water and ground.

When you paint a tile, the map recalculates it and its eight neighbours, because each of them may need a different shape now.

Animated tiles

A1 sheets hold animated autotiles. Two kinds animate:

  • Water style tiles have three frames side by side on the sheet and cycle through them.
  • Waterfall tiles scroll their picture through four steps, like falling water.

Animated Tile Speed on the map sets the frames per second, 6 by default. Animation runs in play mode. The map does not rebuild any mesh for it: when the frame changes, each visible chunk only moves the texture coordinates of its animated tiles.

Chunks and meshes

The map is drawn with ordinary meshes, grouped by layer and by area.

When the map loads, it creates a GameObject at the root of the scene named after the map plus Data, for example AutoTileMap Data. Under it there is one child per layer, placed at that layer's Depth on the Z axis. Under each layer node there are chunks: GameObjects with a MeshRenderer that each draw a block of 62 by 62 tiles of that layer.

A few facts follow from that layout:

  • Every tile is drawn as four quads, one per quarter, even a plain tile. A full chunk is 62 x 62 x 16 = 61,504 vertices, just under the 65,535 a mesh can index with 16 bit indices. That is where 62 comes from.
  • Chunks are created on demand, when a tile in their block is written or refreshed. An area of a layer that never held a tile has no chunk.
  • Changing a tile rebuilds the whole mesh of its chunk. If the tile sits on a chunk edge, the neighbouring chunk rebuilds too, since a neighbour's autotile shape may change.
  • All chunks use the tileset's atlas material. Each chunk renderer gets its layer's sorting layer and order, so layers sort like sprites. See Layers.
  • Unity culls chunks outside the camera like any renderer, so the number of draws depends on how many chunks are on screen, not on the size of the map.
  • Fog of war layers are drawn by their own chunks with a plain material instead of the atlas. Each quarter of a cell gets its own darkness. See Fog of War.

The meshes are never saved. They are rebuilt from the Map Data every time the map loads.

When the map loads

In play mode the map loads in Awake. In the editor, the Auto Tile Map Editor Behaviour component checks whenever the scene is drawn whether the map is loaded, and loads it if not. That is how a map comes back after scripts recompile or a scene opens.

Loading reads every tile out of the Map Data, works out every autotile, then builds all the chunks, all in one go. On a large map the editor pauses for a moment when this happens. Assigning a different Tileset or Map Data in the inspector also reloads the map.

Map data format

The tiles are stored per layer as one list of numbers, read row by row from the top left cell, left to right.

To keep the list short, runs are packed: two or more identical numbers in a row are written as the count, negated, followed by the tile number. A single tile is written as just its number. Empty cells are -1.

For example, this row of seven cells:

5 5 5 5 -1 -1 7

is stored as:

-4 5 -2 -1 7

A number below -1 is always a count, never a tile. Fog of war layers store the darkness of each quarter instead of a tile number, packed so the value never goes negative.

Next to the tiles, each layer stores its name, type, visibility, sorting layer, order and depth. The map also stores its width, its height and Metadata.version, which is the version of this data format (1.2.4), not the version of the asset.

The same structure is written as XML by Export Map... and read back by Import Map.... See Saving and Loading.

Warning

A Map Data asset stores tile numbers, not pictures. It only makes sense with the tileset it was painted with, laid out the same way. See the note on slot order in Tilesets.

World units and tile positions

The map's top left corner is at the map GameObject's position, which should be the world origin. Tiles run right along +X and down along -Y.

  • Y goes down in tile space. Tile row 0 is the top row. Converting a world position to a tile row negates Y.
  • A tile is Cell Size world units wide, not 1. The default is the tile size in pixels divided by 100, so 0.32 for 32 pixel tiles.
  • A cell's flat index is x + y * map width, counting from the top left.

So tile (3, 2) on a map with a cell size of 0.32 covers world X from 0.96 to 1.28 and world Y from -0.64 to -0.96. Tiles and Positions lists the helper methods that do these conversions for you.

The minimap

The map keeps a minimap texture with one pixel per tile. Each tile's colour is the average of the middle of its picture, so a water autotile reads as water rather than as its grassy border. The Ground and Overlay layers are blended from the bottom up, and fog of war darkens the result.

The in-game map editor draws this texture as its minimap. In the editor, Show Minimap in the Settings tab draws it in the Scene view and keeps it up to date while you paint. Revealing fog of war in play mode updates the part of the minimap around the reveal.