Guide

Lua map errors: FourCC, the percent sign and integers

Lua maps fail differently from JASS maps: a bad call stops the trigger quietly instead of refusing to compile. These three cover most reports.

What you see

  • bad argument #2 to 'CreateUnit' (integer expected, got string)
  • Saving a Lua map crashes the editor, even with a % only in a comment (before 2.0.4).
  • bad argument #1 to 'I2S' (number has no integer representation)
  • Without an error handler, the trigger just stops partway.

Why it happens

FourCC. In JASS, 'hfoo' is an integer. In Lua it is a string, so object ids must be converted with FourCC('hfoo').

The percent sign. Before 2.0.4 the editor passed Lua code through a formatter that treats % specially, so a single % anywhere could crash it. Separately, DisplayTimedTextFromPlayer fills in only the first %s.

Integers. Lua's / always produces a decimal, so 7/2 is 3.5, and passing it where a native wants an integer fails. JASS integer division used to truncate.

Lua also needs patch 1.31 or newer, and the classic 1.29 client cannot run Lua maps at all.

How to fix it

  1. Wrap every object id in FourCC('hfoo'), and cache them at the top of the script.
  2. Update the editor to 2.0.4 or newer. Avoid % in map Lua: use math.fmod for remainders and .. to build strings, or write %% if you must.
  3. Use // for integer division, and math.floor before calling a native that wants an integer.
  4. While testing, add an error handler such as Debug Utils so a failing call shows a message.

How WC3 Map Doctor detects it

Rules that look for this
  • Severe
    A Lua call passes a four-character string where an object id (integer) is expected, so the call errors and the trigger stops.
    LUA-fourcc-missing
  • Severe
    The Lua script contains a percent sign (even in a comment or string), which crashes the editor before 2.0.4.
    LUA-percent-pre-204
  • Errors
    The map uses Lua as its script language, which needs 1.31 or later.
    LUA-min-patch
  • Warnings
    DisplayTimedTextFromPlayer replaces only the first %s, and a second one prints garbage or crashes Lua maps on 1.32.
    CRASH-displaytimedtext-percent-s
  • Errors
    The Lua script does not parse, and the game then shows no player slots and no log line.
    GATE-lua-parse

Sources

Written in our own words from our lab notes; these are the pages behind them.