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.
- Displaying helpful information on screen.
- Eliminating tedious repetition.
- Creating specific circumstances for testing.
- Brute forcing a solution.
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
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.
To become familiar with Lua, check out the Lua 5.1 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
--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 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).
- Registers - run functions during specific events, includes advancing a frame, state save/load, etc.
- Display - drawing, memory observation, etc.
- LuaScripting/TableKeys - controlling input, includes autohold and autofire)
- Memory Editing
- To be covered
- savestate.create , savestate.load , savestate.save
- io.write , io.output
Using MHS for emulators without lua embedded
- To be covered
- (Be sure to note that position values are most often floating point data types.)
- Lua Functions for BizHawk emulator
- Lua Functions for lsnes emulator
- Lua Functions for Snes9x emulator
- Check out for some more complicated Lua applications by Bisqwit.
- Snes9x Lua function library (old archive)
- VBA Lua function library
- MAME Lua function library
- Gens Lua function library (TODO: make a proper wiki documenting the functions)
- DeHackEd's sample Lua scripts
: This is known to occur in FCEUX, PCSX, and DeSmuME.