Object MethodScanning and Detection

Region:find(templateOrBuilder, param?)

Searches this region for a template (image, colour or text) and returns the first match as a Result, or nil. With a FindParam timeout it keeps scanning at the scan rate until the target appears or the time runs out.

Documented for app version 1.0.51Updated:

Detailed Explanation

This section explains when to use the API, how to call it, and which structures it works best with in production flow.

How To Call It

This is an object method: call it with a colon on a Region object created with Region(x, y, w, h), or Region() for the whole screen (region:<method>(...)). The global Region table does not have this method. The new image engine keys (auto_image, gray_image, silhouette_image, orientation_image, pixel_exact_image, rotation_image, sift_image) and the tol, angle, angleStep, contrast, edge and transparent options require Macro Handler 1.0.51.

When To Use It

Use it in every step that decides from something on screen. Call Snap.screenRefresh() first when the screen may have changed and limit the search to the smallest Region the target can be in: a narrow region scans faster and matches fewer look-alikes.

Parameters and Return

templateOrBuilder is a template from Asset.image(...), Asset.color(...) or Asset.text(...). Returns a Result (getX, getY, getScore, getEngine, getAngle...) or nil; the match is also available as region:getLastMatch().

Options and Limits

param is a FindParam or an option table. Table keys: timeout (ms; the region is scanned until the target appears or the time runs out), mScore (0-1, default 0.7; larger values are clamped to 1), scale (0.5-4), sRate (scans per second, 1-30, default 3), engine, method, mode, excludeRegion (a Region or {x, y, w, h} the scan skips) and, for text searches, whitelist, blacklist, case, segment and ocrScript. Image engine keys for engine from 1.0.51: auto_image, gray_image, silhouette_image, orientation_image, pixel_exact_image, rotation_image and sift_image; the earlier keys (balanced_image, strict_image, quick_image, color_aware_image, precision_image, small_object_image, edge_image, feature_image) keep their meaning. smart_reference is a Lua-only appearance matcher: it gathers candidates from several engines over a wider scale range and keeps the one whose whole appearance matches best; an Image block runs this key as balanced_image. Mode settings: pixel_exact_image takes tol 0-64 (default 24), needs the template at its native size and defaults mScore to 0.98; rotation_image takes angle 0-180 (default 30, 180 is a full turn) and angleStep 2-45 (default 10), and angles times scales may not exceed 48; silhouette_image takes contrast and orientation_image takes edge, each "low", "normal" or "high"; sift_image needs a detailed template whose shorter side is at least 64 pixels; transparent = false turns the transparency mask off for this search. auto_image, silhouette_image and rotation_image run from Lua but are not offered in the Image block panel yet. Result:getEngine() reports the engine that actually ran and Result:getAngle() the matched rotation.

Best Combined With

It is typically combined with Snap, Region, Asset, and FindParam; you can feed the result into quickTap(), click(), or a follow-up OCR step.

Example Usage

The snippet below is a starter pattern that can be applied directly in runtime flow.

Snap.screenRefresh()
local region = Region()
local match = region:find(Asset.image("start_button"), FindParam.timeout(3000):mScore(0.85))
if match then
  click(match)
end

Copyable Progressive Examples

From foundation to combined usage, each level is provided as a separate code block so you can copy the level you need and adapt it directly.

Foundation

Shows the shortest direct way to call the API.

Foundation
Snap.screenRefresh()
local region = Region()
local match = region:find(Asset.image("start_button"), FindParam.timeout(3000):mScore(0.85))
if match then
  click(match)
end

Simple

Wraps the base call with minimal flow control.

Simple
local stepOk = true
Snap.screenRefresh()
local region = Region()
local match = region:find(Asset.image("start_button"), FindParam.timeout(3000):mScore(0.85))
if match then
  click(match)
end
if stepOk then
  wait(200)
end

Practical Flow

A practical pattern for real macros with pcall, logging, and guards.

Practical Flow
local ok, err = pcall(function()
  Snap.screenRefresh()
  local region = Region()
  local match = region:find(Asset.image("start_button"), FindParam.timeout(3000):mScore(0.85))
  if match then
    click(match)
  end
end)

if not ok then
  print("API step failed: Region:find: " .. tostring(err))
  requestStop()
end

Detailed

This level packages the API into a reusable helper with error reporting.

Detailed
-- In detection flow, refresh the screen with Snap.screenRefresh() first, then narrow the search area and confidence using Region and FindParam.
local function run_find_step()
  Snap.screenRefresh()
  local region = Region()
  local match = region:find(Asset.image("start_button"), FindParam.timeout(3000):mScore(0.85))
  if match then
    click(match)
  end
end

local ok, err = pcall(run_find_step)
if not ok then
  toast("Step failed")
  print(err)
end

Combined

Combines the API with related structures to form a more realistic workflow.

Combined
-- Rotated search from Lua (Macro Handler 1.0.51 or later)
Snap.screenRefresh()
local region = Region()
local match = region:find(Asset.image("arrow_icon"), { engine = "rotation_image", angle = 180, angleStep = 30, timeout = 3000 })
if match then
  print("engine: " .. tostring(match:getEngine()) .. ", angle: " .. match:getAngle())
  click(match)
end