How to store variables outside a function ? Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
WalkerOfTheDay
Posts: 484
Joined: 24 Mar 2016, 03:01

How to store variables outside a function ?

13 Jun 2019, 08:48

Hi Guys,

I want to store the MouseGetPos coordinates outside of the function so I can use
them later on in the script to click the position. How do I do this ??

I tried putting global on the first line, but this returned empty variables.

Code: Select all

ClickPosition("Invoicenumber", "Please click the invoice number", %PosX%, %PosY%)

ClickPosition(title, text, OVarX, OVarY)
{
	;global <------------ tried this, but it doesn't work
	MsgBox, 4096, %title%, %text%, 1
	KeyWait, LButton, D
	MouseGetPos, OVarX, OVarY
	return
}

MsgBox, 4096, X = %PosX% Y = %PosY%

colt
Posts: 135
Joined: 04 Aug 2014, 23:12
Location: Portland Oregon

Re: How to store variables outside a function ?

13 Jun 2019, 09:09

Is this how it should work?

Code: Select all

;do not put % around input vars
ClickPosition("Invoicenumber", "Please click the invoice number", PosX, PosY)

;use byref. that way the function will be operating on the input variable directly
ClickPosition(title, text, byref OVarX,byref OVarY)
{	
	MsgBox, 4096, %title%, %text%, 1
	KeyWait, LButton, D
	MouseGetPos, OVarX, OVarY
	return 
}

MsgBox, 4096,Result, X = %PosX% Y = %PosY%
awel20
Posts: 193
Joined: 19 Mar 2018, 14:09

Re: How to store variables outside a function ?

13 Jun 2019, 09:17

Here are some ways to do it.

This uses ByRef parameters. The function can change the variables that are passed to it.

Code: Select all

ClickPosition("Invoicenumber", "Please click the invoice number", PosX, PosY)
MsgBox, 4096, , X = %PosX% Y = %PosY%

ClickPosition(title, text, ByRef OVarX, ByRef OVarY)
{
	MsgBox, 4096, %title%, %text%
	MouseGetPos, OVarX, OVarY
	return
}
This returns an object from the function:

Code: Select all

Pos := ClickPosition("Invoicenumber", "Please click the invoice number", PosX, PosY)
MsgBox, 4096, , % "X = " Pos.X " Y = " Pos.Y

ClickPosition(title, text, OVarX, OVarY)
{
	MsgBox, 4096, %title%, %text%
	MouseGetPos, OVarX, OVarY
	return {X: OVarX, Y: OVarY}
}
This uses global:

Code: Select all

Pos := ClickPosition("Invoicenumber", "Please click the invoice number")
MsgBox, 4096, , % "X = " OVarX " Y = " OVarY

ClickPosition(title, text)
{
    global
	MsgBox, 4096, %title%, %text%
	MouseGetPos, OVarX, OVarY
	return
}
A_AhkUser
Posts: 1079
Joined: 06 Mar 2017, 16:18
GitHub: AAhkUser
Location: France

Re: How to store variables outside a function ?  Topic is solved

13 Jun 2019, 09:17

HI WalkerOfTheDay,
WalkerOfTheDay wrote:
13 Jun 2019, 08:48
I want to store the MouseGetPos coordinates outside of the function so I can use
them later on in the script to click the position. How do I do this ??
Parameters are always implicitly locals, simply remove their declaration in the function definition:

Code: Select all

ClickPosition("Invoicenumber", "Please click the invoice number")
MsgBox, %PosX%,%PosY%

ClickPosition(title, text)
{
	global
	MsgBox, 4096, %title%, %text%, 1
	KeyWait, LButton, D
	MouseGetPos, PosX, PosY
}
Parameters behaves the same as local variables unless they are defined as ByRef:

Code: Select all

ClickPosition("Invoicenumber", "Please click the invoice number", PosX, PosY)
MsgBox, 4096,, % "X = " PosX " Y = " PosY

ClickPosition(title, text, ByRef x:="", ByRef y:="") {
	MsgBox, 4096, % title, % text, 1
	KeyWait, LButton, D
	MouseGetPos, x, y
}
you can also return an associative array from the function:

Code: Select all

oPos := ClickPosition("Invoicenumber", "Please click the invoice number")
MsgBox, 4096,, % "X = " oPos.x " Y = " oPos.y

ClickPosition(title, text) {
	MsgBox, 4096, % title, % text, 1
	KeyWait, LButton, D
	MouseGetPos, x, y
return {"x": x, "y": y}
}
Hope this helps.


[EDIT] @@awel20 One at a time! Validate the classification after viewing the photo finish. :lol:
WalkerOfTheDay
Posts: 484
Joined: 24 Mar 2016, 03:01

Re: How to store variables outside a function ?

13 Jun 2019, 09:24

Thanks for the very good explanations guys !

Unfortunately I cannot check all your answers as solved so I just picked one :lol:

Return to “Ask For Help”

Who is online

Users browsing this forum: azik0244, felipe93, marqqer, Robbl and 66 guests