Help with script running two functions at once

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
BOOMSPL4T
Posts: 3
Joined: 11 Apr 2021, 20:12

Help with script running two functions at once

11 Apr 2021, 20:26

Hi there,

I've given my best shot at this, piecing together bits of code to try and make something work... And so far I've it it to do what I want, just not at the same time.

I've got two things that need to happen:
1. Spam 2, 3 and 4 with a random range (40 to 70)
2. Right click every 16 seconds.

So far I've managed to get it so I can toggle these two things on and off, but I can't get them to run at the same time. I tried to play around with a label1, label2 sort of thing.... But it really didn't work well (I had to reboot my PC because it kept right clicking everywhere). I tried fixing this several times, but pretty sure I just butchered an already pieced together code.

Any pros out there able to help?

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
#InstallMouseHook
#MaxThreadsPerHotkey 2
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

toggle = 0
chatopen = 0
voiceopen = 0

_ran(min, max)
 {
   random, ran, %min%, %max%
   return ran
 }

#IfWinActive, Diablo III
$~Enter::
    If toggle {
        chatopen := !chatopen
    }
return

#IfWinActive, Diablo III
$~/::
    If !chatopen and toggle {
        chatopen := !chatopen
    }
return

#IfWinActive, Diablo III
F8::
    toggle := !toggle
    While toggle {
	If !chatopen {
	            ControlSend, , {Blind}2, Diablo III
	            ControlSend, , {Blind}3, Diablo III
	            ControlSend, , {Blind}4, Diablo III
	            MouseClick Left
	            sleep, % _ran(40, 70)
        }
    }
return

#IfWinActive, Diablo III
F7::
    toggle := !toggle
    While toggle {
	If !chatopen {
	            MouseClick Left, Diablo III
	            sleep, 16000
        }
    }
return


Escape::
ExitApp
Return
User avatar
mikeyww
Posts: 26939
Joined: 09 Sep 2014, 18:38

Re: Help with script running two functions at once

11 Apr 2021, 21:30

Would use one or more timers instead of the sleep. This may help the problem with the threads.
BOOMSPL4T
Posts: 3
Joined: 11 Apr 2021, 20:12

Re: Help with script running two functions at once

12 Apr 2021, 02:58

Forgive me, like I said that above code is crudely cobbled together, could you explain in layman's terms?

Any sort of simple example would help me understand better.

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

Re: Help with script running two functions at once

12 Apr 2021, 05:05

Code: Select all

#IfWinActive Diablo
F3::SetTimer, R1, % (r1tog := !r1tog) ? 200 : "Off"
F4::SetTimer, R2, % (r2tog := !r2tog) ? 300 : "Off"
#IfWinActive

R1:
Send x
Return

R2:
Send -
Return
Explained: SetTimer

Since your script used the same variable for multiple routines, one routine could toggle the other, because variables are global by default. The script here uses two different toggles.
BOOMSPL4T
Posts: 3
Joined: 11 Apr 2021, 20:12

Re: Help with script running two functions at once

14 Apr 2021, 04:50

Okay, I'm back at this with a fresh mind. I think I understand a little better now.

Code: Select all

F3::SetTimer, R1, % (r1tog := !r1tog) ? 200 : "Off"
F4::SetTimer, R2, % (r2tog := !r2tog) ? 16000 : "Off"

R1:
Send, 2
Send, 3
Send, 4
MouseClick Left
Return

R2:
MouseClick Right
Return
Am I correct in thinking that R1 and R2 are labels? I've stripped out some things I don't believe I need, including the active window as this is not for Diablo 3 (It was just left over from the code I repurposed from another forums).

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: ht55cd3 and 269 guests