From The Battle for Wesnoth Wiki
< LuaAPI‎ | types

The map userdata provides direct access to the Wesnoth map. Functions in the map module that take a map as the first parameter can also be called as methods on the map userdata.


  • map[location] ↔ terrain code
  • map[location] ← code)
  • map[location] ← code)
  • map[location] ← code)
  • map[location] ← code, mode)

Look up or modify the terrain on the given hex. When assigning a terrain code, the replacement method is determined by the ^ character in the code. If the terrain code starts with ^, only the overlay will be replaced. If the code ends with ^, only the base will be replaced. If it has ^ in the middle or does not have ^ at all, then both layers will be replaced (in the latter case, any existing overlay is removed). If the assignment ends up producing an invalid combination, it will be ignored.

The functions replace_base, replace_overlay, and replace_both simply add or remove a ^ to get the intended effect, so you don't need to remember the above rules. It's especially useful if the input terrain isn't under your control.

Assigning terrain using the replace_if_failed function modifies the behaviour when an invalid combination is produced. In this case you should not use a terrain code that begins or ends with ^. The mode can be either "base" or "overlay", and if the replacement fails because of an invalid combination, it will replace both layers instead. You can also pass "both" as the mode, though this is not very useful.

local replacement_terrain = 'Gg^Ve'
-- On (3,3), replace both layers[{3,3}] =
-- On (4,7), replace only the overlay[{4,7}] =
-- On (21,12), replace only the base[{21,12}] =
-- On (15,9), replace only the overlay if possible, otherwise both[{15,9}] =, 'overlay')

Map Size

  • map.widthsize
  • map.heightsize
  • map.playable_widthsize
  • map.playable_heightsize
  • map.border_sizesize

Get the size of the map. These satisfy the following identities:

width = playable_width + 2 * border_size
height = playable_height + 2 * border_size

Special Locations

  • map.special_locations[id] ↔ location

This is a table containing all the special locations set in the map data, including side starting locations, indexed by their key. (Side starting locations are indexed by the side number.) It's writable, so you can modify them or add new ones on the fly.

-- Get the starting position of side 3
local enemy_start =[3]
-- Get a named location, "spire", from the map data
local destination =

Map Data

  • map.datastring

Convert the map to a string. This is usually needed by map generation scripts to pass the map on to the engine once the generation has completed.

This page was last edited on 2 July 2021, at 06:26.