ImGui in GTA: San Andreas – interfaces for Lua scripts

Where SA:MP scripts get their windows and menus, and how they are built.

Author: Published: Updated: 1 min read

Dear ImGui is a popular immediate-mode GUI library. Unlike classic GUI frameworks, here a window is described by code that runs every frame: "draw a button; if it was clicked, do X". That suits game mods very well, because the interface lives right inside the game loop.

ImGui in MoonLoader

MoonLoader has Lua bindings for Dear ImGui: imgui came first, then mimgui, built on LuaJIT FFI. With them SA:MP scripts got real windows with settings, menus, tables, tabs and search.

A simple mimgui window:

local imgui = require("mimgui")
local show = imgui.new.bool(true)
imgui.OnFrame(function() return show[0] end, function()
  imgui.SetNextWindowSize(imgui.ImVec2(320, 160), imgui.Cond.FirstUseEver)
  imgui.Begin("Pakulichev", show)
  imgui.Text("Hello from ImGui!")
  if imgui.Button("Close") then show[0] = false end
  imgui.End()
end)

Common widgets

Widget ImGui function Used for
Window Begin / End Any script menu
Button Button Actions, binds
Checkbox Checkbox Toggling features
Text input InputText Nicknames, commands, RP text
Tabs BeginTabBar / BeginTabItem Settings sections
Child region BeginChild Lists, scrolling

The manual layout problem

Sizes, paddings, positions and styles of every window are tuned by hand. On a large interface that takes a long time. That's why I built ImGUI Editor: you assemble the UI with the mouse and get ready code as output.

Tips

  • Keep state (checkboxes, input buffers) outside the render function.
  • Put styles and colours into a separate theme function.
  • Use imgui.Cond.FirstUseEver so users can move the window.
  • Don't run heavy logic inside the frame – rendering only.

Frequently asked questions

What is Dear ImGui?

Dear ImGui is an immediate-mode GUI library for C++ used to build interfaces: windows, buttons, inputs, tabs. It is widely used in games, tools and debug panels.

What is mimgui?

mimgui is a Dear ImGui binding for MoonLoader built on LuaJIT FFI. It lets a Lua script draw ImGui interfaces on top of GTA: San Andreas.

Why use a visual ImGui editor?

Laying out windows in code is slow: you restart the script again and again to tune sizes and positions. A visual editor such as ImGUI Editor lets you assemble the interface with the mouse and get ready-to-use Lua code.