Skip to content

The game's own code ​

When no rq.* call does what a mod needs, rq.ain reaches into the game's own code, the AIN (RanceQuest.ain): it calls the game's functions, runs Lua around them, reads and writes the game's globals, and changes constants in its code. Use a dedicated rq.* call when one exists: it is checked, documented and kept working across loader versions, while the game's code has none of that.

Names ​

Functions and globals go by the game's own names, most of them Japanese: クエスト_名前, C敵登録, ◆ランダムエンカウント歩数. A method is written Class@method, as in T_Player@checkEncount. Write them in UTF-8, as init.lua is, and copy them from the log rather than typing them: many use full-width letters and digits (C, 8), which look like the plain ones and are not.

Finding a function ​

rq.ain.find writes every function whose name contains a text to the log, one per line, with its number and its full name:

lua
rq.ain.find("Encount")
text
[rqmod] find 'Encount' fno=10717 T_Player@getEncountFlag
[rqmod] find 'Encount' fno=10733 T_Player@checkEncount
[rqmod] find 'Encount' fno=10734 T_Player@addEncount
[rqmod] find 'Encount' fno=14290 T_EncountBar@isSuspend
...

The Japanese names need the Japanese text: rq.ain.find("エンカウント") finds ランダムエンカウント_あり, which "Encount" does not. (Random fights have their own calls since 2.33.0, rq.encounters; these names only show what find gives.)

The match is on the game's spelling, so case and full-width letters count. It lists at most 40 names: a longer text narrows the list. The call works at the top level of init.lua; to try names while you play, put it on an F3 menu row. The name is what rq.ain.call and rq.ain.hook take. find gives names only, not what a function takes or does.

To learn that, read the game's code. Tools for the System 4 engine, such as alice-tools, turn your copy's RanceQuest.ain into readable source. That source is the game's own work: read it to understand the game, and do not share it or copy it into a mod.

Calling a function ​

lua
local name = rq.ain.call("クエスト_名前", 126)   -- the quest's name

The arguments are int, bool, float or string, and every one is needed. A function that takes a reference or a struct cannot be called this way. A method (Class@method) needs its object: rq.ain.method (2.16.0) calls it on the struct a global reaches.

lua
local rows = rq.ain.method({ "g_sSystemMenu", "m_sButton" }, "T_ButtonGroup@getButtonCount")

A scene command waits until it ends, so call it from a callback the game runs, not at the top level. See rq.ain.call for what happens when the call reaches a save or a load.

Running Lua around a function ​

A hook runs before and after every call of a function. before can change the arguments or skip the function and give its result; after can change the result:

lua
-- Every roll of 乱数(10) comes up 1, as long as the hook stays.
local remove = rq.ain.hook("乱数", { after = function(h)
  if h.args[1] == 10 then h.result = 1 end
end })
remove()   -- takes the hook away again

Hooks can sit at the top level of init.lua. Several mods may hook the same function. A hook on a function the game calls often, such as 乱数, runs every time, so keep its work small, and remove it once it is no longer needed.

When only some calls matter, only (2.55.0) names them by their int arguments: argument position to a value or a list of values. The engine checks them before any Lua runs, so the other calls cost nothing. The game checks a character's skills thousands of times a frame in a fight; this hook sees only the checks for Immature (373), the second argument of CS番号取得:

lua
rq.ain.hook("CS番号取得", { only = { [2] = 373 }, after = function(h)
  rq.log("Immature asked for", h.args[1], "row", h.result)
end })

Up to 4 positions, each with up to 16 values; a call runs the callbacks when every position named holds one of its values.

Globals ​

lua
local fa = rq.ain.global("FA", 87)              -- FA[87]
local name = rq.ain.global("Q", 126, "名前")   -- Q[126].名前
rq.ain.set_global("◆クエスト戦闘回数", 0)      -- the fights of this quest so far

Indexes and field names follow the global's name, mixed as the game nests them. rq.ain.global_len gives an array's size. Many of the game's arrays start at 1 and leave 0 unused. What set_global writes is the game's own state and goes into the next save, so put back what you change for a test.

rq.ain.set_global_len (2.18.0) makes an array longer or shorter, as the game's own Realloc does, so the game's code that reads it by its size reads more rows. The new size goes into the save, and a save made before brings back the old one, so grow it where it is used, for example in a before hook of the function that fills it. rq.skilllist grows the skill buy list this way.

Changing constants in the code ​

rq.ain.patch_int and rq.ain.patch_float replace a number written in a function, such as a skill number or a multiplier. rq.ain.patch_int_after finds one by the global it follows. Each returns how many it replaced: check that count, since a game update can move or remove the number.

lua
-- 毒計算 never poisons Suzume; make that check match no character.
if rq.ain.patch_int("毒計算", rq.ids.chara.SUZUME, -1) ~= 1 then
  rq.log("毒計算 changed; the patch is off")
end

To change the number again while the game runs, as a setting on a Config page, keep the places patch_int_after gives back (since 2.22.0) and write them with rq.ain.set_code_int. The search runs once, at load, so a later value can never match the wrong constant. Custom TADA Mode does this.

lua
local n, places = rq.ain.patch_int_after("T戦闘後の取得経験値算出", "◆2倍モード", 2, 2)
local function set_mult(m)
  for _, place in ipairs(places) do rq.ain.set_code_int(place, m) end
end

rq.ain.skip_msgbox takes the game's message boxes that hold a text out of its code.

Take care ​

  • The names and the code are the game's, not ours. They hold for this version of Rance Quest Magnum, and a mod that leans on them should check what it gets (a count, a nil) and say so in the log when it does not fit.
  • A bad index passed to a game function is a fatal error in the game, and rq.ain.call cannot check it.
  • Something many mods would do through rq.ain is a call rq.* is missing. The money, charisma, random fights, EXP and levels became calls that way in 2.33.0 (Gold, encounters and levels), and the stun, the turn list and the battle log that the shipped mods hooked in 2.37.0 (Combat rules). The roadmap lists the ones planned, such as rq.ain.call with reference and struct arguments.