Until is not working for loop Topic is solved

Ask gaming related questions (AHK v1.1 and older)
PassTheMayo
Posts: 3
Joined: 20 Mar 2017, 15:57

Until is not working for loop

20 Mar 2017, 16:02

So I am making an auto-AFK script for Minecraft. The script currently just does the following:

Code: Select all

Hold W
Wait 1 second
Release W
Wait 1 second
Hold A
Wait 1 second
...
I want it to continue doing that until the keybind has been pressed again, but it stays in the loop even if I press the bind a second time. Here's my code:

Code: Select all

toggled=0

^L::
    if (toggled=0)
    {
        toggled=1
        Loop
        {
            Send {w down}
            Sleep 1000
            Send {w up}
            Sleep 1000
            Send {a down}
            Sleep 1000
            Send {a up}
            Sleep 1000
            Send {s down}
            Sleep 1000
            Send {s up}
            Sleep 1000
            Send {d down}
            Sleep 1000
            Send {d up}
        } Until (toggled=0)
    }
    else
    {
        toggled=0
    }
return
Any help, please?
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: Until is not working for loop

20 Mar 2017, 16:54

Code: Select all

#MaxThreadsPerHotkey 2 ; This setting is used to control how many "instances" of a given hotkey or hotstring subroutine are allowed to exist simultaneously
and here 2 is usefull since we want stop the subroutine using the same hotkey

expression which alongside untilis evaluated after the inner loop is reached see: https://autohotkey.com/docs/commands/Until.htm
so you have to check at each send, whith this aim in view you better off simplificate you multiline send by an expression with a loop

Code: Select all

Loop, 4
MsgBox % ["w", "a", "s", "d"][a_index]
is the same as:

Code: Select all

MsgBox, w
MsgBox, a
MsgBox, s
MsgBox, d
and since with have a loop in aloop with have to call break, 2 because if an inner loop is enclosed by an outer loop, the inner loop takes precedence
(see: https://autohotkey.com/docs/commands/Loop.htm)

Code: Select all

#MaxThreadsPerHotkey 2
toggled=0

^l::
    if (toggled=0)
    {
        toggled=1
		Loop
		{
        Loop, 4
        {
			
           ;  Send % "{" . ["w", "a", "s", "d"][a_index] . A_Space . (mod(a_index, 2) ? "up" : "down") . "}"
           ; EDIT:
            Send % "{" . ["w", "w", "a", "a", "s", "s", "s", "d", "d"][a_index] . A_Space . (mod(a_index, 2) ? "up" : "down") . "}"
            Sleep 1000
			if not toggled
			break, 2
        } 
		}
    }
    else
    {
        MsgBox, exit
		toggled=0
    }
return
my scripts
PassTheMayo
Posts: 3
Joined: 20 Mar 2017, 15:57

Re: Until is not working for loop

20 Mar 2017, 17:31

@A_AhkUser I've used your code and it did this on me:

http://prntscr.com/emg1i9
User avatar
Masonjar13
Posts: 1555
Joined: 20 Jul 2014, 10:16
Location: Не Россия
Contact:

Re: Until is not working for loop  Topic is solved

20 Mar 2017, 17:36

They simply forgot to update the loop count. Just as well, there is one too many s's in that array.

Code: Select all

Loop 8
Send % "{" . ["w", "w", "a", "a", "s", "s", "d", "d"][a_index] . A_Space . (mod(a_index, 2) ? "up" : "down") . "}"
Edit: instead of embedded loops, why not use SetTimer?

Code: Select all

^l::setTimer,MCBot,% (t:=!t)?1000:"off"

MCBot:
i:=1
loop 8
    send % "{" . ["w", "a", "s", "d"][mod(a_index,2)?i:i++] . A_Space . (mod(a_index, 2) ? "up" : "down") . "}"
return
OS: Windows 10 Pro | Editor: Notepad++
My Personal Function Library | Old Build - New Build
PassTheMayo
Posts: 3
Joined: 20 Mar 2017, 15:57

Re: Until is not working for loop

20 Mar 2017, 17:41

Masonjar13 wrote:They simply forgot to update the loop count. Just as well, there is one too many s's in that array.

Code: Select all

Loop 8
Send % "{" . ["w", "w", "a", "a", "s", "s", "d", "d"][a_index] . A_Space . (mod(a_index, 2) ? "up" : "down") . "}"
Thank you, it worked!
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: Until is not working for loop

20 Mar 2017, 17:56

Masonjar13 wrote:They simply forgot to update the loop count. Just as well, there is one too many s's in that array.
sssorry... :oops:

Masonjar13 wrote:instead of embedded loops, why not use SetTimer?
niice solution
and never thinked about [a_index>4?a_index]thanks for sharing the tips ;)
my scripts
User avatar
Masonjar13
Posts: 1555
Joined: 20 Jul 2014, 10:16
Location: Не Россия
Contact:

Re: Until is not working for loop

20 Mar 2017, 18:48

That was wildly incorrect, actually.. I've edited my code, which is now working. You could very well stick with having the full array still (with all the letters doubled); the difference between that and my fixed method are negligible at best, really.
OS: Windows 10 Pro | Editor: Notepad++
My Personal Function Library | Old Build - New Build
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: Until is not working for loop

21 Mar 2017, 09:47

Code: Select all

[a_index>4?a_index]
How couldn't see it!!
Masonjar13 wrote:That was wildly incorrect
for sure!

that's we call argument from authority: I saw Masonjar13, Россия, 828 posts so I said into myself :
русский программист it's an other level, святая речь it's true! It cannot be a badly written code!
my scripts

Return to “Gaming Help (v1)”

Who is online

Users browsing this forum: No registered users and 33 guests