How to troubleshoot script stoppages

Ask gaming related questions (AHK v1.1 and older)
GoneFishing
Posts: 126
Joined: 20 May 2022, 16:17

How to troubleshoot script stoppages

Post by GoneFishing » 25 Nov 2022, 13:23

I have a fairly straight forward script that works without an issue most of the time. It executes a series of clicks, sends, sleeps (see below) but at random times it stops executing the first click (so none of the others will execute). Is there some way I can debug it to see why this happens (why the click is misfiring) ?

Code: Select all

IfWinActive, Game Name
\::reload#
$[::
loop
{
pixelGetColor, pixel1, 75, 48, RGB 
sleep 800
if pixel1 = 0xFCD69C
{
 pixelGetColor, pixel2, 2211, 85, RGB
 sleep 800
 if pixel2 = 0x83082F
 {
  run, C:\AHk\Scripts\moose_and_squirrel.wav
  pause
 }
 else
   click 2667, 138            ; <------ This sometimes fails to execute. What debug statement(s) can I use to see what is happening here
   sleep 4000
   send ^{click, 1708, 818} 
   sleep 1000
   click 865, 598
   sleep 1000
}
else
sleep 80
}
return

gregster
Posts: 9067
Joined: 30 Sep 2013, 06:48

Re: How to troubleshoot script stoppages

Post by gregster » 25 Nov 2022, 13:27

That click line only gets executed when that else branch gets executed. Since you don't use a { }-block, the rest of that section (sleep 4000 and so on) gets executed anyway (unconditionally). That means, the indenting is misleading in this case. I guess you forgot the { }.
Of course, this is all part of the outer if-branch, so it's no wonder that this part sometimes doesn't get executed at all, but the outer else-branch (sleep 80) instead.

GoneFishing
Posts: 126
Joined: 20 May 2022, 16:17

Re: How to troubleshoot script stoppages

Post by GoneFishing » 25 Nov 2022, 14:09

gregster wrote:
25 Nov 2022, 13:27
That click line only gets executed when that else branch gets executed. Since you don't use a { }-block, the rest of that section (sleep 4000 and so on) gets executed anyway (unconditionally). That means, the indenting is misleading in this case. I guess you forgot the { }.
Of course, this is all part of the outer if-branch, so it's no wonder that this part sometimes doesn't get executed at all, but the outer else-branch (sleep 80) instead.
My mistake - it's actually the second piece - the ctrl+click, that stutters sometimes. What should my code look like ?

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

Re: How to troubleshoot script stoppages

Post by mikeyww » 26 Nov 2022, 06:12

Beyond the block bug, here are more ideas for debugging:
1. See Debugging a script.
2. Insert MsgBox lines as needed to help you see what is happening.
3. Display the values of your variables, functions, & conditional statements, as well as ErrorLevel from the searches.
4. Determine whether your Pause command is pausing the script.
5. Examine the KeyHistory to understand what happens with the keys.
6. Remove the Loop command, so that you can test a single iteration and get it working.
7. Remove deprecated commands such as IfWinActive.
8. Read about #If directive, and see the examples, so that you understand how to use it. Avoid using it during your initial testing.
9. Fix coding errors such as reload#. During testing, remove extraneous hotkeys like this that play no role in the test.
10. Understand your code. Do you need or want $? If so, why?
11. I recommend using expressions with If, with parentheses in most cases, instead of legacy syntax. https://www.autohotkey.com/docs/commands/IfExpression.htm

Post Reply

Return to “Gaming Help (v1)”