Page 1 of 1

Functions not calling

Posted: 18 Jan 2019, 05:21
by Smilie
Hi,
I have a very basic script, that simply moves and clicks with the mouse in order to drop some items, but for some reason, nothing happens when i press ctrl + d, or ctrl + s.

Code: Select all

YCoords := [370, 398, 432, 456, 488]
DropCoordX := 234
DropCoordY := 313
DropItem(x, y)
{
	MouseMove, x, y
	Click
	MouseMove, DropCoordX, DropCoordY
	Click
}
DropFirst()
{
	x := 490
	while (x < 640)
	{
		for index, element in YCoords
		{
			DropItem(x,element)
		}
		x += 30
	}
}
DropSecond()
{
	x := 640
	while (x <= 790)
	{
		for index, element in YCoords
		{
			DropItem(x,element)
		}
		x += 30
	}
}

^d::
DropFirst()
return

^s::
DropSecond()
return

Re: Functions not calling

Posted: 18 Jan 2019, 06:12
by swagfag
functions have scope, YCoords at the top and YCoords in ur for-loops dont refer to the same thing
u need to make YCoords available to the function. u can use the global keyword or pass it to the function as an argument
read more: https://www.autohotkey.com/docs/Functions.htm

Re: Functions not calling  Topic is solved

Posted: 18 Jan 2019, 16:54
by mast4rwang
Yeah, each function you create is local by default so it can only access values you define in function itself.
as swag said do this:

myFunction(x,y)
{
global
my code
return
}

Now it can access not only x,y but all other variables you created (except variables from functions that are local).

Re: Functions not calling

Posted: 18 Jan 2019, 17:37
by Smilie
Thanks man, that did the trick! :D