Lua Scripting API

mGBA includes full Lua 5.4 scripting support, allowing you to create custom tools, trainers, automation scripts, and game modifications. This documentation provides a complete reference for the Lua scripting API.

Getting Started with Lua Scripting

Loading Scripts

To load a Lua script in mGBA:

  1. Go to Tools β†’ Scripting β†’ Load Script
  2. Navigate to your Lua script file (.lua format)
  3. Select the script file and click Open
  4. The script will be loaded and executed automatically
Script Location: Lua scripts are typically stored in the scripts directory within mGBA's configuration folder. You can also load scripts from any location on your system.

Memory Access Functions

read8(address)

Read an 8-bit value from memory.

Parameters:

  • address (number): Memory address (0x00000000 to 0xFFFFFFFF)

Returns: 8-bit value (0-255)

-- Example: Read 8-bit value from memory
value = mGBA:read8(0x02000000)
print("Value at 0x02000000: " .. value)

read16(address)

Read a 16-bit value from memory.

Parameters:

  • address (number): Memory address

Returns: 16-bit value (0-65535)

-- Example: Read 16-bit value
value = mGBA:read16(0x02000000)
print("16-bit value: " .. value)

read32(address)

Read a 32-bit value from memory.

Parameters:

  • address (number): Memory address

Returns: 32-bit value

-- Example: Read 32-bit value
value = mGBA:read32(0x02000000)
print("32-bit value: " .. value)

write8(address, value)

Write an 8-bit value to memory.

Parameters:

  • address (number): Memory address
  • value (number): 8-bit value (0-255)
-- Example: Write 8-bit value
mGBA:write8(0x02000000, 0xFF)

write16(address, value)

Write a 16-bit value to memory.

Parameters:

  • address (number): Memory address
  • value (number): 16-bit value (0-65535)

write32(address, value)

Write a 32-bit value to memory.

Parameters:

  • address (number): Memory address
  • value (number): 32-bit value

CPU and Register Functions

getRegister(registerName)

Get CPU register value.

Parameters:

  • registerName (string): Register name (e.g., "r0", "r1", "pc", "sp")

Returns: Register value (32-bit)

-- Example: Get PC (Program Counter) value
pc = mGBA:getRegister("pc")
print("Program Counter: " .. string.format("0x%08X", pc))

setRegister(registerName, value)

Set CPU register value.

Parameters:

  • registerName (string): Register name
  • value (number): 32-bit value

Frame and Timing Functions

frame()

Get current frame number.

Returns: Current frame number

-- Example: Get current frame
currentFrame = mGBA:frame()
print("Current frame: " .. currentFrame)

reset()

Reset the emulator.

-- Example: Reset emulator
mGBA:reset()

Save State Functions

saveState(slot)

Save state to a slot.

Parameters:

  • slot (number): Save slot number (1-10)

loadState(slot)

Load state from a slot.

Parameters:

  • slot (number): Save slot number (1-10)

Input Functions

pressButton(button)

Press a button programmatically.

Parameters:

  • button (string): Button name ("A", "B", "L", "R", "Start", "Select", "Up", "Down", "Left", "Right")
-- Example: Press A button
mGBA:pressButton("A")

Event Callbacks

mGBA Lua API supports event callbacks for game state changes:

-- Example: Frame callback
function onFrame()
    -- This function is called every frame
    local frame = mGBA:frame()
    if frame % 60 == 0 then
        print("Frame: " .. frame)
    end
end

-- Register callback
mGBA:onFrame(onFrame)

Common Script Examples

Health Trainer

-- Health trainer example
local healthAddress = 0x02000000  -- Replace with actual address

function infiniteHealth()
    mGBA:write16(healthAddress, 999)
end

-- Run every frame
mGBA:onFrame(infiniteHealth)

Frame Counter

-- Frame counter script
local startFrame = mGBA:frame()

function countFrames()
    local current = mGBA:frame()
    local elapsed = current - startFrame
    print("Elapsed frames: " .. elapsed)
end

mGBA:onFrame(countFrames)

Memory Addresses

Common memory addresses for Game Boy Advance games:

  • Internal WRAM: 0x03000000 - 0x03007FFF (32KB)
  • External WRAM: 0x02000000 - 0x0203FFFF (256KB)
  • Video RAM: 0x06000000 - 0x06017FFF (96KB)
  • Game Pak ROM: 0x08000000 - 0x0DFFFFFF (varies by game)
  • Game Pak SRAM: 0x0E000000 - 0x0E00FFFF (varies by game)
Note: Memory addresses vary by game. Use memory scanners or debuggers to find specific addresses for your game.

Best Practices

  • Always validate memory addresses before reading/writing
  • Use appropriate data types (8-bit, 16-bit, 32-bit) for memory operations
  • Be careful with frame callbacks to avoid performance issues
  • Test scripts thoroughly before using in important gameplay
  • Backup save files before using scripts that modify game state

Limitations

  • Lua scripts run in the emulator's main thread (may affect performance)
  • Some operations may not be available depending on emulator state
  • Memory addresses may change between game versions or regions

Related Articles

For more information about mGBA configuration and development: