Aller au contenu principal

Dialogs in Lua

Everything the dialog blocks do is available in Lua. A dialog script has three parts: start a new dialog, add the questions, show it. Then read the answers by id.

Dialog:init()
Dialog:setTitle("Farm helper")
Dialog:add(TextView("Set it up once. You can change it later."))
Dialog:add(CheckBox(1, "Collect daily rewards", true))
Dialog:add(NumberInput(2, "Runs", 10):setRange(1, 999))

local mode = RadioGroup(3, 1)
mode:add(RadioButton(1, "Farm"))
mode:add(RadioButton(2, "Arena"))
Dialog:add(mode)

Dialog:add(TapPicker(4, "Tap the Attack button"):showWhen(3, 2))
Dialog:add(GestureRecorder(5, "Play your route once"))

local advanced = Section("Advanced", true)
advanced:add(Slider(6, "Delay (ms)", 0, 1000, 300):setStep(50):setHelp("Wait between actions"))
Dialog:add(advanced)

Dialog:showIfNeeded()

local runs = Dialog:getValueById(2)
local delay = Dialog:getValueById(6)
for i = 1, runs do
if Dialog:getValueById(3) == 2 then
Screen:click(Dialog:getValueById(4), ClickParams())
end
Record:play(Dialog:getValueById(5))
sleep(delay)
end

This is the dialog it shows:

The Farm helper dialog

The parts​

StepFunctions
Start a new dialogDialog:init(), Dialog:setTitle(), Dialog:setMessage()
Add questionsDialog:add() with TextView, CheckBox, EditText, NumberInput, Slider, RadioGroup, TapPicker, TapListPicker, GestureRecorder
Arrange themPage, Section, Row, Column
Fine-tune a questionshowWhen, hideWhen, setHelp, setRequired, setRange, setStep, showAsDropdown
Show itDialog:show() or Dialog:showIfNeeded()
Read the answersDialog:getValueById()

Good to know​

  • Call Dialog:init() first. It starts a new, empty dialog, so a macro can show more than one.
  • The setters return the question, so they chain: Slider(6, "Delay", 0, 1000):setStep(50):showWhen(1).
  • Containers are filled first and added after: page:add(...), then Dialog:add(page).
  • Ids must be different for every question in the macro, because answers are saved by id.
  • Dialog:show() stops the macro if the user presses Cancel.
  • Read answers after show() or showIfNeeded().