Lua scripting is an extremely useful tool when TASing. Although some programming knowledge is required, the user has power over things like game screen display, inputs, and memory. The following are some common applications, though the possibilities are endless.
Snes9x was the first emulator modified to support Lua, back in 2008. Now, the following emulators support Lua: FCEU(X), BizHawk, lsnes, Snes9x, Gens, VBA, Final Burn Alpha, PCSX, DeSmuME, PCEjin, VBjin, JPC-RR, and MAME.
In order to integrate Lua, a number of emulator-specific functions were created, which form the emulator-specific Lua API, enabling control of the emulator. However, one is not limited only to emulator-specific Lua functions. Anyone can use any function in the main Lua API, including file I/O. Thus the emulator can be scripted to communicate with, say, text files!

How to run a Lua script

To run a Lua script, copy the code and place it in a text editor. Name the file as "file.lua" or anything ending in "lua". When saving from Notepad in Windows, you need to put quotes around the name "file.lua".
Then go to the emulator and run the Lua file. Some emulators have a Lua script window; the Lua script will automatically begin when you load it.

Basics

To become familiar with Lua, check out the Lua 5.4 manual. It covers how the Lua language works, and the main Lua API.
In the emulator, it is desirable to have a main execution loop. The main execution loop can take one of two forms (-- means the rest of the line is a comment):
--Declarations here
while true do
    --Statements
emu.frameadvance()
end
and
--Declarations here
function fn()
    --Statements
end

gui.register(fn)
The second one is technically a function which is called whenever there is an update to the display (which is supposed to be every frame but is also called in some emulators[1] during when the display is paused). The second form is required for DeSmuME since the first form is working in a different way than most other emulators (see this topic for more information).

Using MHS for emulators without lua embedded

More information


[1]: This is known to occur in FCEUX, PCSX, and DeSmuME.
Expand/Collapse collapse-content-_4b497944e19c4b4c9ebf48b7e84511fa

LuaScripting last edited by YoshiRulz on 1/11/2024 6:23 AM
Page History Latest diff List referrers View Source