Mathew James Sideris
Mathew James Sideris

Reputation: 34

Lua Loop with error detection not working correctly

I scripted a simple bot program a week or two ago, and have been building on it with new knowledge as I broaden my understanding.

local B = 1 --Boss check 1 = true, 2 = false

repeat
  function bossCheck()
    local rgb1 = getColor(x,y)
    if rgb1 == (rgb) then
      touchDown(x,y)
      usleep(time)
      touchUp(x,y)
    end

    local D = 1 --Delay, corrective action when script is out of sync with loading times
    repeat
      if rgb1 ~= (rgb) then
        D = D + 1
        usleep(time)
      end
    until D == 5
  end
  if D == 5 then
    B = B + 1
  end
until B == 2

if B == 2 then
  alert("No Boss")
end

This actually worked in a loop until I added the correction check delay. If the function bossCheck() failed, then in my mind it should repeat. Am I wrong in assuming this is workable, or have I misplaced some loop statements?

Prior to this new code I implemented with local D = 1 --for delay I would attempt to touch at my IOS screen twice, it would return not true results and then my loop would end. But as of now, I run my script and nothing happens and it would appear that the script runs indefinately.

It's very confusing. I don't expect a verbatim line I should include here, but kind of hint me into the right direction.

Edit - Example

'function bossCheck () if (getColor(x,y) == "color1") then return true; end return false; end

function onBoss () touch(x,y) usleep(time) return true; end

function fightBoss () touch(x2,y2) usleep(time) return true; end

function bossReturn () touch (x3,y3) usleep(time) return true; end

function bossLoop () while (bossCheck) do onBoss (); fightBoss (); bossReturn (); end end

repeat bossLoop (); until (bossCheck == false)

if (bossCheck == false) then alert("Boss Loop End") end

'

Upvotes: 0

Views: 1196

Answers (1)

das
das

Reputation: 557

Ok, repeat until executes the given script over and over until it reaches a statement.

Your script, re-define bossCheck as a function and checks if D equal to 5 (D is nil).

You do not call anywhere bossCheck, and because of that B remains 1.

This script should work.

local noBoss = false;
function bossCheck()
    noBoss = false;
    local rgb1 = getColor(x,y);
    if (rgb1 == rgb) then
      touchDown(x,y)
      usleep(time)
      touchUp(x,y)
      return -- if it's true, stop the function here;
    end
    noBoss = true;
    usleep(time * 5); -- no point delaying application action 5 times, when we call just multiply the sleep amount by 5;
end

repeat
    bossCheck();
until noBoss;

if (noBoss) then
    alert("No Boss");
end

Edit:

An example how I'd call several functions in sequence

function bossCheck()
    if (getColor(x,y) == rgb) -- instead doing rgb1= ..
        touchDown(x,y)
        usleep(time)
        touchUp(x,y)
        return true;
    end
    return false;
end
while true do
    if (not bossCheck()) then
        alert("No Boss");
        break; -- break out of the loop
    end
    if ((function () return true)()) then
        alert("This local function return true.. so alert message is shown\nYou can do the same with any other functions");
    end
end

According to your example you can just do this

function bossCheck()
    if (getColor(x,y) == rgb) -- instead doing rgb1= ..
        return true;
    end
    return false;
end

while true do
    if (bossCheck()) then
        touch(x,y)
        usleep(time)
        touch(x2,y2)
        usleep(time)
        touch(x3,y3)
        usleep(time)
    else
        alert("No Boss");
        break; -- break out of the loop
    end
end

Upvotes: 0

Related Questions