Differene between targetOffset and find."direction" operations

Asked by Bharatendu SOUMIL on 2019-04-12

Hey guys,
I was wondering if there is a particular difference between these two lines:
because the effect is basically the same in each of the two cases, therefore I was wondering if one has an advantage/disadvantage over the other.

Thank you!

Question information

English Edit question
Sikuli Edit question
No assignee Edit question
Solved by:
Last query:
Last reply:
RaiMan (raimund-hocke) said : #1

Functionally no difference.

--- find."direction"
the direction is written in the code.
If you want to use the same somewhere else you have to repeat the code

--- .targetOffset
the click offset becomes an attribute of the Pattern Object.
If you want to use the same somewhere else, then you can follow the DRY principle(don't repeat yourself):
pat = Pattern("1231.png").targetOffset(30,3)

cheers RaiMan for the answer...but couldn't we still do: pat= find("1231.png").right(30)......and then do anything we like with pat, for e.g.: click(pat) ? We are using the DRY principle here as well.

RaiMan (raimund-hocke) said : #3

Yes, you are principally right, if the given image is in the same place as at time of assignment:
- with my click(pat) the find op is reevaluated
- with your version it is not (so a risk to click the wrong place)

Thanks for the prompt response. Sorry I am still a bit not 100% clear on this. Apologies for continuing this tiny doubt for so long.
Just to be sure: Even if the image is moved to a different place, won' t the find operation still detect the image and choose the point that is 30px right of this image, and therefore do the same thing as your suggestion with the targetOffset?

Thanks once again!

Best RaiMan (raimund-hocke) said : #5

no problem ;-)

pat = find(img).right(30)
will only be evaluated once in a scriptrun and then always be a the evaluated location

pat = Pattern(img).targetOffset(30, 0)
will lead to a repeated find op everytime pat is used.

This of course is only of interest, if the image might change its location during the same sciptrun.

Thanks RaiMan, that solved my question.