Loop two different actions in Sikuli forever

Asked by Hashtag123

I have a task in Sikuli and I just can't figure out how to accomplish it.

I want to start a script and with it the LEFT Arrow key should be pressed repeatedly. As soon as a certain image (Pic_1) appears the RIGHT Arrow key should be pressed repeatedly. As soon a a different Image (Pic_2) appears the LEFT Arrow key should be pressed again and the whole loop schould start again.

So basically I just have 2 different Images and 2 different actions. But I just can't figure out how to loop them. An I don't know how a Key can be pressed repeatedly with Sikuli. If possible can I set a certain speed? If not I have to press the LEFT Arrow key as long as Pic_1 "does not exists".

Can anyone help me?

Question information

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

So here is my approach and it works like I want it to (but too slow):

setAutoWaitTimeout(0)
wait(10)
while True:
    while True:
        if not exists(Pattern("02_right.png").similar(0.85)):
            type(Key.LEFT)
        else:
            type(Key.RIGHT)
            break
    while True:
        if not exists(Pattern("04_left.png").similar(0.85)):
            type(Key.RIGHT)
        else:
            type(Key.LEFT)
            break

If the image exists, the actions comes very fast but if it doesnt exist it takes too long (should be as close to instant as possible).

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

what version of Sikuli?

Revision history for this message
Hashtag123 (hashtag123) said :
#3

SikulixIDE 1.1.0

I think I could speed up the process with regions but somehow I am unable to use them propably.
This is how I would define them:

reg1 = Region(817,676,44,228) #Region for "02_right.png"
reg2 = Region(1059, 676, 44, 228) # Region for "04_left.png".

Revision history for this message
Hashtag123 (hashtag123) said :
#4

*properly

Revision history for this message
Hashtag123 (hashtag123) said :
#5

When I use "exists" instead of "not exists" it is still very slow:

wait(10)
while True:
    while True:
        if exists(Pattern(Pattern("02_right-1.png").similar(0.75)),0):
            type(Key.RIGHT)
            break
        else:
            type(Key.LEFT)
    while True:
        if exists(Pattern(Pattern("04_left-1.png").similar(0.75)),0):
            type(Key.LEFT)
            break
        else:
            type(Key.RIGHT)

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

just for the records (nothing to do with your problem)

makes no sense:
Pattern(Pattern("02_right-1.png").similar(0.75))

this is enough:
Pattern("02_right-1.png").similar(0.75)

the version in comment #5 is the preferable one -- using exists(…, 0)

If your stuff is in a window, that probably not always is in the same place, then you should not work with absolute regions.

in a first step find something, that is always visible in the window.
then measure the offset to the place of the images.
then define regions, that contain the images and use them with exists.

ref = find(someStaticVisual)
offsetR = (x, y) # to be evaluated (topleft to topleft)
offsetL = (x, y) # to be evaluated
regRight = ref.offset(offsetR).setSize(44,228)

while True:
    while True:
        if regRight.exists(Pattern(Pattern("02_right-1.png").similar(0.75)),0):

BTW: are you simulating some scrolling?

Revision history for this message
Hashtag123 (hashtag123) said :
#7

Thanks so far =)

Actually the stuff IS in a windows and DOES appear always at the same position.

Subject:
I am testing a small game which is still in development.
Here is a sketch:

http://i.imgur.com/WZdqoor.jpg

The vertical squares are my images. If the left one appears then the Right Arrow Key should be pressed. If the right image appears, then the left Arrow key should be pressed.

So I could work with absolute regions if this could speed up the process.
But somehow as soon as I try to include regions the Images won't be found even if they appear.
TBH I have diffivulties with the Sikuli documentation and think I am doing something wrong.

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

ok, this visual setup is really one of the more difficult ones, if you are talking about the vertikal square with the thin border and the transparent content.

please confirm, before we talk further.

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

... a short video would help me a lot to understand the dynamics.

Revision history for this message
Hashtag123 (hashtag123) said :
#10

Sorry for the late Answer. I finally found time to proceed my project and solved my problem.
Using absolut regions made the script pretty fast.

reg = Region()
while True:
    while True:
        if reg.exists(Pattern,0):
            type(Key.RIGHT)
            break
        else:
            type(Key.LEFT)
    while True:
        if reg.exists(Pattern,0):
            type(Key.LEFT)
            break
        else:
            type(Key.RIGHT)

I have one final task though. How Can I loop this script for a specific duration?
For examples 5 minutes.

I guess I need to replace

while True:

with some sort oft function.

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

now = time.time()
in5min = now + 5 * 60 * 1000

while time.time() < in5min:

the granularity of time.time() is milliseconds. hence 5 * 60 * 1000 are 5 minutes

Revision history for this message
Hashtag123 (hashtag123) said :
#12

Thanks a lot =)

Revision history for this message
Hashtag123 (hashtag123) said :
#13

If I may correct you.

in5min = now + 5 * 60

worked for me.
Somehow * 1000 wasnt needed.

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

Thanks for the correction ;-)

I remember, that I was in a hurry and had a bad feeling exactly about that, but was too lazy to verify the time.time() return value, which indeed is a float representing seconds.

happy scripting.