Logical looping for speed

Asked by Eric G

My script looks through the defined region for 18 unique "targets" that move around very fast.
My previous method was to lump all the pictures together and using if reg.exists() + reg.getLastMatch()
I feel like it is way too slow. So I wanted to group the targets and loop for them in sections.

What I have:

mmd = Settings.MoveMouseDelay = 0
Settings.ObserveScanRate = 9

##Region stuff

...

##Grouping
group1=False
group2=False
group3=False

while 1:
     if reg_range.exists("Target1.PNG", 0) or reg_range.exists("Target2.PNG", 0):
         group1 = True
         group2 = False
         group3 = False
         if reg_cast.exists("ActionButton.png", 0):
            click(reg_range.getLastMatch())
     if not group1:
         if reg_range.exists(""Target3.PNG", 0) or reg_range.exists("Target4.PNG", 0):
             group2 = True
             group1 = False
             group3 = False
             if reg_cast.exists("ActionButton.png", 0):
                 click(reg_range.getLastMatch())
     if not group2:
         if reg_range.exists("Target5.PNG", 0) or reg_range.exists("Target6.PNG", 0):
             group3 = True
             group1 = False
             group2 = False
             if reg_cast.exists("ActionButton.png", 0):
                 click(reg_range.getLastMatch())
##etc...

The targets can appear all together or random clusters and random intervals. I would like the script to loop until one of the targets appears then stop looping to find the other targets, click on the target it identified. I would like it to do all of that repeatedly until I stop running the script.

I cannot tell if there is any difference in speed between this method and my previous method...

Is my approach appropriate for what I'm describing?

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
RaiMan
Solved:
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

you should have a look at the feature findAny(), which detaches the find op for each given image to a different thread.
hence the searches are done in parallel internally.

constructs like yours are done sequentially one after the other.

I guess that reg_range is already as small as possible.

https://sikulix-2014.readthedocs.io/en/latest/region.html#Region.findAny

Revision history for this message
Eric G (gamemaster181) said :
#2

So with findAny I have
##stuff
regTest=Region(0,0,1200,50)

images = ["img1.png","img2.png","img3.png"]

while 1:
    matches = regTest.findAny(images)
    if match in matches:
        while exists("Actionbutton.png"):
            click(match.getIndex())

Am I using match.getIndex() correctly? I only need to click the first instance that matched

Revision history for this message
Eric G (gamemaster181) said :
#3

I also have another problem where I am start sikuli from cmd administrator but the hotkeys still don't register over an application. I have to click out of the application for sikuli to respond to hotkeys like shift alt c.

Revision history for this message
Best RaiMan (raimund-hocke) said :
#4

while 1:
    matches = regTest.findAny(images)
    if matches:
        while exists("Actionbutton.png"):
            click(matches[0])

Revision history for this message
RaiMan (raimund-hocke) said :
#5

... getIndex is only needed if only some of the images matched and you want to know which on:

for match in matches:
    print images[match.getIndex()]

Revision history for this message
RaiMan (raimund-hocke) said :
#6

--- I also have another problem where I am start sikuli from cmd administrator but the hotkeys still don't register over an application.

no idea. sorry.

In my tests it works.

Revision history for this message
Eric G (gamemaster181) said :
#7

Thank you RaiMan! You're are a huge help

Revision history for this message
Eric G (gamemaster181) said :
#8

Thanks RaiMan, that solved my question.