Usage of array members in function calls

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
avernikov
Posts: 5
Joined: 19 Jun 2021, 19:46

Usage of array members in function calls

Post by avernikov » 19 Jun 2021, 20:02

Hello,
I have several instances of a struct(assoc array) that I want to pass to a function, which shall use its values in a call to ImageSearch function. Can you please help me defining the right syntax for that?
Here is not working code:

Code: Select all

global gBonusBttn :=	{x:0,y:0,X1:1670,Y1:0295,X2:1820,Y2:0450,dev:40,img:"red_new.png"}
global gSystemBttn:=	{x:0,y:0,X1:1640,Y1:0820,X2:1810,Y2:1010,dev:40,img:"system_bttn.png"}

if(PeekObject(gSystemBttn))
	MsgBox Success!!!!
Else
	MsgBox Error!!!!

PeekObject(obj)
{
    CoordMode, Pixel, Window
    ; that line below works, i want to replace it with my "obj" parameters
    ImageSearch, x, y, 1395, 684, 1835, 1037, *40 F:\bot\Screen_20210518225507.png
 
    ImageSearch, obj.x, obj.y, obj.X1, obj.Y1, obj.X2, obj.Y2, *obj.dev obj.img     ; THAT LINE DOES NOT WORK...
    return (ErrorLevel = 0)
}
Thanks in advance!

User avatar
mikeyww
Posts: 26435
Joined: 09 Sep 2014, 18:38

Re: Usage of array members in function calls

Post by mikeyww » 19 Jun 2021, 20:46

I believe that your output variables cannot be array elements. It seems that you also do not need them in this script. The following is OK.

Code: Select all

gBonusBttn  := {x: 0, y: 0, X1: 1670, Y1: 295, X2: 1820, Y2:  450, dev: 40, img: "red_new.png"}
gSystemBttn := {x: 0, y: 0, X1: 1640, Y1: 820, X2: 1810, Y2: 1010, dev: 40, img: "system_bttn.png"}

If PeekObject(gSystemBttn)
     MsgBox, 64, Success, Success!
Else MsgBox, 48, Failure, Error!

PeekObject(obj)
{
 ImageSearch,,, obj.X1, obj.Y1, obj.X2, obj.Y2, % "*" obj.dev " " obj.img
 Return !ErrorLevel
}

avernikov
Posts: 5
Joined: 19 Jun 2021, 19:46

Re: Usage of array members in function calls

Post by avernikov » 19 Jun 2021, 21:01

Hello mikeyww,
Thanks, it works!

User avatar
boiler
Posts: 16705
Joined: 21 Dec 2014, 02:44

Re: Usage of array members in function calls

Post by boiler » 19 Jun 2021, 21:55

@avernikov, if you did want to put the resulting coordinates into those array elements, you could easily do it since the function is actually working with the same array object. What gets passed as a parameter is a pointer to the array object, so you can change its contents in the function:

Code: Select all

gBonusBttn  := {x: 0, y: 0, X1: 1670, Y1: 295, X2: 1820, Y2:  450, dev: 40, img: "red_new.png"}
gSystemBttn := {x: 0, y: 0, X1: 1640, Y1: 820, X2: 1810, Y2: 1010, dev: 40, img: "system_bttn.png"}

If PeekObject(gSystemBttn)
     MsgBox, 64, Success, % "Success!  Image found at " gSystemBttn.x "," gSystemBttn.y
Else MsgBox, 48, Failure, Error!

PeekObject(obj)
{
 ImageSearch, x, y, obj.X1, obj.Y1, obj.X2, obj.Y2, % "*" obj.dev " " obj.img
 obj.x := x
 obj.y := y
 Return !ErrorLevel
}

avernikov
Posts: 5
Joined: 19 Jun 2021, 19:46

Re: Usage of array members in function calls

Post by avernikov » 20 Jun 2021, 06:53

Thanks @boiler !
I am now doing exactly that. And you actually answered my another question about how to print the array parameters - that weird % syntax is still a mystery for me...
And I'm still struggling here - can I concatenate strings in parameter list of a function call? Like below :

Code: Select all

LogWrite(message)
{
	FileAppend, %message%`n, %gLogName%
}

if(!PeekObject(obj))
      LogWrite ( "PeekObject " obj.name " failed: not found") ; DOESN'T compile
And that one doesn't work either - compiles but complains about object value 1234.00000 in runtime:

Code: Select all

LClickOn(obj)
{
	local cx=(obj.X1+obj.X2)/2
	local cy=(obj.Y1+obj.Y2)/2
	LogWrite(Clicking on (%cx%,%cy%)) ; displays error during runtime
    Click, %cx%, %cy% Left, 1
}
I will really appreciate some guidance on both.
Thanks!
Last edited by avernikov on 20 Jun 2021, 07:08, edited 1 time in total.

User avatar
mikeyww
Posts: 26435
Joined: 09 Sep 2014, 18:38

Re: Usage of array members in function calls

Post by mikeyww » 20 Jun 2021, 07:02

Yes, but a function name must always be followed by a "(" rather than a space.

avernikov
Posts: 5
Joined: 19 Jun 2021, 19:46

Re: Usage of array members in function calls

Post by avernikov » 20 Jun 2021, 07:13

mikeyww wrote:
20 Jun 2021, 07:02
Yes, but a function name must always be followed by a "(" rather than a space.
:headwall: Omg... Thanks, @mikeyww, another lesson learnt the hard way!

User avatar
boiler
Posts: 16705
Joined: 21 Dec 2014, 02:44

Re: Usage of array members in function calls

Post by boiler » 20 Jun 2021, 07:52

Do I have that somewhere in what I posted? I can’t find it, and I just copied and pasted from what was already posted. I only added the part about the found location.

User avatar
mikeyww
Posts: 26435
Joined: 09 Sep 2014, 18:38

Re: Usage of array members in function calls

Post by mikeyww » 20 Jun 2021, 08:03

I think OP deleted a post that showed a bug with a function call.

User avatar
boiler
Posts: 16705
Joined: 21 Dec 2014, 02:44

Re: Usage of array members in function calls

Post by boiler » 20 Jun 2021, 08:20

Ah, I see. Thanks.

avernikov
Posts: 5
Joined: 19 Jun 2021, 19:46

Re: Usage of array members in function calls

Post by avernikov » 20 Jun 2021, 17:07

sorry, i made the edit of the post and it disappeared for few hours while in moderator approval queue...

User avatar
mikeyww
Posts: 26435
Joined: 09 Sep 2014, 18:38

Re: Usage of array members in function calls

Post by mikeyww » 21 Jun 2021, 06:46

OK. A tip for future: if a post was added after yours, try to avoid editing; usually better to add a new post, to avoid confusion.

Post Reply

Return to “Ask for Help (v1)”