Mouse stuck in loop

Asked by Eric G on 2020-01-30

Trying to teach myself programming, having a lot of issues with looping...can someone please help me out? I've been stumped for a while.

this is the part I'm having trouble with

if selected == Comps[0]:
    while True:
        if Reg.exists("pic1",0) or Reg.exists("pic2",0) or Reg.exists("pic3",0) or Reg.exists("pic4",0)or Reg.exists("pic 5",0):
            click(Reg.getLastMatch())

It is for a game, I need to click the units that pop up on my screen, they automatically vanish and appear on my board which has 10 slots. However, every now and then my board becomes full and cannot take another unit until I sell one. But since the units are still "available" my mouse gets stuck in an infinite loop trying to click on them. I don't want to put a wait(2) or something at the end because it would compromise a lot of speed. I also cannot use hotkeys or shift alt c when I constantly click in the game window.

I can't really think of a solution to end the infinite mouse clicks other than using waitVanish(Reg.getLastMatch()) to set while loop to false...but that didn't work out

please help haha

Question information

Language:
English Edit question
Status:
Solved
For:
Sikuli Edit question
Assignee:
No assignee Edit question
Solved by:
RaiMan
Solved:
2020-01-31
Last query:
2020-01-31
Last reply:
2020-01-31
RaiMan (raimund-hocke) said : #1

Hope you using the latest version (2.0.2 sice yesterday) ;-)

Depending on the size of the region (BTW: Reg should be reg by convention: names starting with lowercase are variable names, whereas upppercase signals a class name), the avarage 3 searches (max 6) in a row might take too long.

Using onAppear or waitVanish would not help either, since the events internally are handled in a row too.

Have a look at findAny (https://sikulix-2014.readthedocs.io/en/latest/region.html#Region.findAny), which runs the searches in parallel internally

Eric G (gamemaster181) said : #2

Thanks for such a fast response I'll definitely look into findAny!

Eric G (gamemaster181) said : #3

Hi again,

I've been messing around with findAll and other similar functions from the documentation page...

while True:
        testpics = ("1580408084658.png","1580408094920.png","1580408102639.png")
        matches = findAll(testpics)
        if matches:
               click(Last.Match)
        else:
            print "not found"

also tried stuff like

    while True:
        Match match = reg.findAll("pic1","pic2","pic3")
if Match in match:
    click()

I just want the program to continuously look for and click pic1,2,3 but stop when a region is occupied or click only once per match, something along those lines. I'm not sure if I'm even close to being on the right track, I'm still too much of a novice to understand how to do things properly. Can you please recommend some tutorials or video guides that aren't outdated? I really would like to get my stuff to work.

RaiMan (raimund-hocke) said : #4

findAny not findAll !!!!

Eric G (gamemaster181) said : #5

in pyautogui there is a failsafe feature that terminates the program if the mouse cursor is detected at (0,0) is there a way I could implement that in Sikuli? I cannot make use of shift alt c during the infinite click loop.
I've tried replacing findAll with findAny but I get errors "no viable alternative at input 'match'"

RaiMan (raimund-hocke) said : #6

- failsafe feature that terminates the program

add this to the top of your script:

def terminateHandler(event):
    exit(1)

Env.addHotkey("c", KeyModifier.SHIFT + KeyModifier.ALT, terminateHandler) # example

... use any suitable key combination.

Now pressing the key combo during scriptrun will terminate the scriptrun.

Best RaiMan (raimund-hocke) said : #7

- how to use findAny()

... based on your first exists example.

matches = reg.findAny("pic1", "pic2", "pic3", ... more pictures)
if (len(matches) == 0):
    print "none of them found"
    # in a loop: use continue to repeat
found = matches[0].getIndex()
print "found: pic%d" % found +1

Eric G (gamemaster181) said : #8

Thank you so much, I get it now. You really helped me out a lot with the findany example!

Eric G (gamemaster181) said : #9

Thanks RaiMan, that solved my question.