Help with script

Ask gaming related questions (AHK v1.1 and older)
Artorias
Posts: 2
Joined: 07 Aug 2022, 11:05

Help with script

Post by Artorias » 07 Aug 2022, 11:11

Hello everyone. Just gotten introduced to autohotkey and I'm loving it so far! Would like some assistance with a pretty simple script of mine using imagesearch whereas I would be notified when the image shows up while walking back and forth using the a and d keys.

Code: Select all

#IfWinActive ahk_class GLFW30

dir = %A_ScriptDir%
!2::
continueWalk = True
while continueWalk
{
	Send, {a down}
	Sleep, 1000
	Send, {a up}
	Send, {d down}
	Sleep, 1000
	Send, {d up}
}
Loop
{
CoordMode, Pixel, Window
ImageSearch OutputVarX, OutputVarY, 149, 157, 197, 173, %dir%\test.png
	If ErrorLevel = 0
		MsgBox, test completed
		continueWalk = False
}

return

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

Re: Help with script

Post by boiler » 07 Aug 2022, 11:20

There are a few issues with your code.

One is that you set continueWalk to False (sort of — more on that below) in the second loop no matter what the condition of the if statement is because only the line immediately following it is executed conditionally unless you use braces { } to group multiple lines afterwards. Indenting doesn’t mean anything in AHK other than for visualization.

Also, the condition of if continueWalk will evaluate as true even when the value is the string False. To set the variable to the built-in variables of True/False that have the values of 1/0 (as opposed to the strings True/False), use the expression assignment operator :=, not the legacy assignment operator =.

You have no mechanism for exiting the second loop once it gets there, so you would need to break that loop after the image is found.

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

Re: Help with script

Post by boiler » 07 Aug 2022, 11:37

Another problem is that even if you fix the above issues, the first loop would never exit to reach the second loop because the value of the variable can't become false while it's in that loop. The second loop does not have a chance to execute. To perform two loops sort of in parallel, use SetTimer for at least one of them.

By the way, you should edit your first post to add a more descriptive subject for this thread. Everyone posting in "Ask For Help" needs help with a script.

Post Reply

Return to “Gaming Help (v1)”