Copyable Example Code
You can paste this into the code editor; still verify target image, region, color, text, and timing values in your own device flow.
-- OCR score reader
-- Region:readAsString + Num.parseOcr tolerate OCR mistakes
local scoreRegion = Region(800, 100, 280, 80)
local bestKey = "best_score"
Snap.screenRefresh()
-- Read the area and convert it to a number
local rawText = scoreRegion:readAsString()
local score = Num.parseOcr(rawText, 0)
if score <= 0 then
toast("Could not read the score: '" .. rawText .. "'")
return
end
-- Compare with the record
local bestScore = KV.getNumber(bestKey, 0)
if score > bestScore then
KV.set(bestKey, score)
KV.increment("record_count", 1)
System.vibrate(300)
System.noti("New record: " .. score .. " (previous " .. bestScore .. ")")
else
toast("Score: " .. score .. " (record: " .. bestScore .. ")")
end
-- Keep a short history
local history = KV.get("score_history", "")
local newEntry = score .. ","
if #history > 200 then
history = history:sub(-100)
end
KV.set("score_history", history .. newEntry)