Looping script eventually starts malfunctioning

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Uncle Iroh
Posts: 18
Joined: 22 Oct 2019, 13:04

Looping script eventually starts malfunctioning

10 Nov 2019, 18:44

I have a looping script that only takes a few seconds to complete, and continues until I tell it to stop. However, for whatever reason, it will eventually malfunction and just start looping only 3 of the various clicks. And not even 3 that are in a row at any point in the script. This may take an hour to occur, but most times only 15-30 minutes. Sometimes even only 5 minutes. Any thoughts? I also tried it today on my old laptop with windows 7, and it kept happening on there as well.

Code: Select all

F1::
Loop {
	PixelSearch,,, 987, 130, 1014, 148, 0xFF7C00, 50, Fast RGB
	if    (ErrorLevel = 0) {
		;blahblahblah
	}
	else {                                                                   
		Sleep, 100
        ControlClick, x665 y300, BlueStacks,, Left, , Down Pos NA 
        RandSleep(75,125)
        ControlClick, x665 y300, BlueStacks,, Left, , Up Pos NA
        RandSleep(400,500)
        ControlClick, x660 y474, BlueStacks,, Left, , Down Pos NA 
        RandSleep(75,125)
        ControlClick, x660 y474, BlueStacks,, Left, , Up Pos NA
        RandSleep(350,400)
        ControlClick, x668 y306, BlueStacks,, Left, , Down Pos NA 
        RandSleep(75,125)
        ControlClick, x668 y306, BlueStacks,, Left, , Up Pos NA
        RandSleep(450,500)
        PixelSearch,,, 550, 150, 585, 177, 0xFFFF00, 3, Fast RGB
        if    (Errorlevel = 0) {
			Sleep, 50
            ControlClick, x661 y477, BlueStacks,, Left, , Down Pos NA 
            RandSleep(75,125)
            ControlClick, x661 y477, BlueStacks,, Left, , Up Pos NA
            RandSleep(400,450)
            ControlClick, x668 y306, BlueStacks,, Left, , Down Pos NA 
            RandSleep(75,125)
            ControlClick, x668 y306, BlueStacks,, Left, , Up Pos NA
            RandSleep(400,450)
            ControlClick, x665 y389, BlueStacks,, Left, , Down Pos NA 
            RandSleep(75,125)
            ControlClick, x665 y389, BlueStacks,, Left, , Up Pos NA
            RandSleep(400,450)
            ControlClick, x663 y307, BlueStacks,, Left, , Down Pos NA 
            RandSleep(75,125)
            ControlClick, x663 y307, BlueStacks,, Left, , Up Pos NA
            RandSleep(400,450)
            ControlClick, x666 y383, BlueStacks,, Left, , Down Pos NA 
            RandSleep(75,125)
            ControlClick, x666 y383, BlueStacks,, Left, , Up Pos NA
            RandSleep(600,700)
        }
        else {
			Sleep, 50
            ControlClick, x665 y389, BlueStacks,, Left, , Down Pos NA 
            RandSleep(75,125)
            ControlClick, x665 y389, BlueStacks,, Left, , Up Pos NA
            RandSleep(400,450)
            ControlClick, x663 y307, BlueStacks,, Left, , Down Pos NA 
            RandSleep(75,125)
            ControlClick, x663 y307, BlueStacks,, Left, , Up Pos NA
            RandSleep(400,450)
            ControlClick, x666 y383, BlueStacks,, Left, , Down Pos NA 
            RandSleep(75,125)
            ControlClick, x666 y383, BlueStacks,, Left, , Up Pos NA
            RandSleep(600,700)
        }
	}	
}
return
Last edited by Uncle Iroh on 10 Nov 2019, 21:44, edited 1 time in total.
gregster
Posts: 8921
Joined: 30 Sep 2013, 06:48

Re: Looping script eventually starts malfunctioning

10 Nov 2019, 19:18

That would indicate that none of the colors you are looking for, can still be found, at some point.

Note that Pixelsearch - by default - uses coordinates that are relative to the active window unless CoordMode was used to change that. That means, the result of your Pixelsearches depends on what window is active. Also, Pixelsearch can only work on visible windows, while your ControlClicks are not dependent on this condition.

So, you should make sure that the Bluestacks window is always visible and active, when Pixelsearch executes. If you set Coordmode to Pixel, Screen, you could also work with screen coordinates and an inactive window, but the window still needs to be visible.
Uncle Iroh
Posts: 18
Joined: 22 Oct 2019, 13:04

Re: Looping script eventually starts malfunctioning

10 Nov 2019, 19:25

It malfunctions while BlueStacks is active and visible. And I have it established for what it's supposed to do when the colour isn't found. Which is actually the default state. It behaves exactly like it should for a random period of 5-90 minutes, then starts looping only a small part of the whole loop, that's at the end.
gregster
Posts: 8921
Joined: 30 Sep 2013, 06:48

Re: Looping script eventually starts malfunctioning

10 Nov 2019, 19:29

No, an else-branch cannot simply loop by itself (insert a msgbox in the first else-branch to verify that it gets also executed). There must be something else going on so that the colors cannot be found anymore.
Perhaps something interferes on your computer and de-activates the window...

You'll have to do some debugging, I guess...
Uncle Iroh
Posts: 18
Joined: 22 Oct 2019, 13:04

Re: Looping script eventually starts malfunctioning

10 Nov 2019, 19:37

gregster wrote:
10 Nov 2019, 19:29
No, an else-branch cannot simply loop by itself. There must be something else going on so that the colors cannot be found anymore.
Perhaps something interferes on your computer and de-activates the window...
And yet, it is. Somehow. This happens on both computers, when I literally have nothing else running, and don't touch anything after making BlueStacks visible and active, then starting the script and watching tv. And it just happened now on my laptop, while typing this on my desktop. Nothing else is running on the laptop.
gregster
Posts: 8921
Joined: 30 Sep 2013, 06:48

Re: Looping script eventually starts malfunctioning

10 Nov 2019, 19:39

Insert a msgbox in the first else-branch to verify that it gets also executed and do some systematic debugging.

But perhaps it's a Bluestacks-Bug that causes the loss of focus...
Uncle Iroh
Posts: 18
Joined: 22 Oct 2019, 13:04

Re: Looping script eventually starts malfunctioning

10 Nov 2019, 19:48

I considered it being a BlueStacks issue, but I know others have written ahk scripts for it without this issue. And it definitely is somehow only doing the bottom else without doing the top else, because, not only would there not be an issue, it also wouldn't be visible as it is, if somehow there could be an issue while still doing the top and bottom part. The top part clicks further up, and every time a click is made, there is a large red circle that pulses around it for a second. It doesn't appear, and neither does the menu that would pop up -- where the part that is somehow repeating on its own would click specific parts of said menu. So, it just keeps clicking where the menu would be, but there's no menu to click, because somehow it suddenly starts ignoring the parts above it.
gregster
Posts: 8921
Joined: 30 Sep 2013, 06:48

Re: Looping script eventually starts malfunctioning

10 Nov 2019, 20:06

Well, then you should have even more reasons for systematic debugging.

Or do you some strange things in RandSleep() ?
Uncle Iroh
Posts: 18
Joined: 22 Oct 2019, 13:04

Re: Looping script eventually starts malfunctioning

10 Nov 2019, 20:17

RandSleep is simple as it gets, sadly. Can't be the issue.

Code: Select all

RandSleep(min,max){
Random, rand, %min%, %max%
Sleep %rand%
}
gregster
Posts: 8921
Joined: 30 Sep 2013, 06:48

Re: Looping script eventually starts malfunctioning

10 Nov 2019, 20:21

Welp, I also thought that simple elses couldn't loop.
gregster
Posts: 8921
Joined: 30 Sep 2013, 06:48

Re: Looping script eventually starts malfunctioning

10 Nov 2019, 20:37

Question, are you sure that your ControlClicks really do what you intend?

Because I read for the Options parameter:
https://www.autohotkey.com/docs/commands/ControlClick.htm#Parameters wrote:Options

A series of zero or more of the following option letters. For example: d x50 y25.

NA [v1.0.45+]: May improve reliability. See reliability below.

D: Press the mouse button down but do not release it (i.e. generate a down-event). If both the D and U options are absent, a complete click (down and up) will be sent.

U: Release the mouse button (i.e. generate an up-event). This option should not be present if the D option is already present (and vice versa).

Pos: Specify the word Pos anywhere in Options to unconditionally use the X/Y positioning mode as described in the Control-or-Pos parameter above.

Xn: Specify for n the X position to click at, relative to the control's upper left corner. If unspecified, the click will occur at the horizontal-center of the control.

Yn: Specify for n the Y position to click at, relative to the control's upper left corner. If unspecified, the click will occur at the vertical-center of the control.

Use decimal (not hexadecimal) numbers for the X and Y options.
I don't see any literal Down or Up option 🤷‍♂️. This looks like it would create additional short clicks while RandSleep is not really helping like intended with the pressduration. I could imagine that this could throw your script off under the right (wrong) conditions.
Uncle Iroh
Posts: 18
Joined: 22 Oct 2019, 13:04

Re: Looping script eventually starts malfunctioning

10 Nov 2019, 20:39

Down and Up are the D and U. It's just optional to write more than the first letter, as it is with L and R click.
gregster
Posts: 8921
Joined: 30 Sep 2013, 06:48

Re: Looping script eventually starts malfunctioning

10 Nov 2019, 20:45

Okay, for L(eft) and R(ight) the docs explicitly mention it, though.
But if you are sure, I don't have to check... in AHK you never know, but seems legit. But I would look into the else.
Uncle Iroh
Posts: 18
Joined: 22 Oct 2019, 13:04

Re: Looping script eventually starts malfunctioning

10 Nov 2019, 20:55

This is what's happening: https streamable.com /ok4yx Broken Link for safety

Whoops. Now that I finally decided to just record it, this time -- or maybe every time -- it wasn't actually just looping the last else bit. Rather, it seems to be doing *mostly* the bottom else, but with 1 of the clicks from the the above else thrown in replacement. Bloody weird. And it just cycles like that, so I stopped recording pretty soon after.
Last edited by Uncle Iroh on 10 Nov 2019, 21:36, edited 2 times in total.
gregster
Posts: 8921
Joined: 30 Sep 2013, 06:48

Re: Looping script eventually starts malfunctioning

10 Nov 2019, 21:03

Edit: Well, that's the only thing that makes sense re else.

For some reason, the colors are not to be found anymore. Perhaps not enough variation in the Pixelsearch options.
Or certain Randsleeps are just too short under certain circumstances, perhaps it's just a small hickup of the Bluestacks App.

Perhaps some of our actual gamers have an idea about the likely timing issue. I just couldn't believe the supposed else-problem.
Uncle Iroh
Posts: 18
Joined: 22 Oct 2019, 13:04

Re: Looping script eventually starts malfunctioning

10 Nov 2019, 21:18

All those actions you saw it properly repeating, though, were when it *wasn't* finding the colour. And started malfunctioning while it still wasn’t. It’s very strange.
Uncle Iroh
Posts: 18
Joined: 22 Oct 2019, 13:04

Re: Looping script eventually starts malfunctioning

10 Nov 2019, 21:19

I’m currently recording again until it malfunctions again, to see if it’s in the exact same way. Not that it will take the same amount of time to occur, though...
Uncle Iroh
Posts: 18
Joined: 22 Oct 2019, 13:04

Re: Looping script eventually starts malfunctioning

10 Nov 2019, 21:47

“I just couldn’t believe the supposed else-problem”. Haha, well, it’s not like randomly repeating only 3 of the clicks from the whole loop, and which aren’t even 3 that are consecutive in the script, makes any more sense...
gregster
Posts: 8921
Joined: 30 Sep 2013, 06:48

Re: Looping script eventually starts malfunctioning

10 Nov 2019, 21:55

I assume that certain clicks only register under certain conditions, for example, if a certain window pops up. So perhaps not every click is visible for you. Or, certain clicks get lost because the sleep is too short. But I am still convinced that an else-branch doesn't loop by itself.

My interest (and experience) in automating games is close to 0, so I won't try to reproduce this. But there are some experienced gamers in the forum - they might have some tips.
Uncle Iroh
Posts: 18
Joined: 22 Oct 2019, 13:04

Re: Looping script eventually starts malfunctioning

10 Nov 2019, 22:02

Yeah, but it happens while BlueStacks is still the app constantly active, and even though the shortest of 75ms definitely isn’t too fast, if it was, subsequent ones wouldn’t always be that low. Especially because one of the ones that’s always excluded when it starts looping malfunctioned clicks, is one that has a 700ms sleep before it. Way larger than all the other ones that are always registered before and during the malfunctioning.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot] and 161 guests