assembling two scripts in one with same functions

Get help with using AutoHotkey and its commands and hotkeys
Jgorski31
Posts: 5
Joined: 11 Jan 2020, 06:13

assembling two scripts in one with same functions

11 Jan 2020, 06:34

Hello, i'm using Ahk scripts with mouse events in video games.
in some case i need to run 2 different scripts at the same time using the same keys and functions and i need to :

run the first script; disbale it with an hotkey; run the second script and then use the same hotkey to switch between them.

i was wondering if i could put the two scripts in one with a simple key to switch between them.

running this first script

Code: Select all

   _bla := true
  ~LButton::click()

    ~v::_bla := ! _bla

    
    click()
    {
    global _bla
    if _bla
    {
   loop
   {

Send, {i Down}
sleep 10
mouseXY(0,1)
sleep 10
mouseXY(0,1)
sleep 10
mouseXY(0,2)
sleep 10
mouseXY(0,2)
sleep 10
mouseXY(-0,1)
Send, {i up}
If (GetKeyState("LButton","P")=0)
Break

    } ;; loop
    } ;; if
    } ;; click()

    mouseXY(x,y)
    {
    DllCall("mouse_event",uint,1,int,x,int,y,uint,0,int,0)
    }
then switching to this function by pressing V

Code: Select all

       
    _auto := true
    
    ~Lbutton & ~RButton::autofire()
    ~Rbutton & ~LButton::autofire()
    ~v::_auto := ! _auto

    

    autofire()
    {
    global _auto
    if _auto
    {
   loop
   {


GetKeyState, state, Lbutton, P
if state=u
break

sleep 10	
mouseXY(0,4)
If (GetKeyState("LButton","P")=0)
Break
sleep 30
mouseXY(0,4)
If (GetKeyState("LButton","P")=0)
Break
sleep 30
mouseXY(0,3)
If (GetKeyState("LButton","P")=0)
Break
sleep 25



If (GetKeyState("LButton","P")=0)
Break
    } ;; loop
    } ;; if
    } ;; autofire()

    mouseXY(x,y)
    {
    DllCall("mouse_event",uint,1,int,x,int,y,uint,0,int,0)
    }
So far i tried to use the toggle hotkey but could get the whole script to work cause the two functions use the same events and hotkeys.

thanks for your help
Rohwedder
Posts: 3091
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: assembling two scripts in one with same functions

11 Jan 2020, 07:54

Hallo,
untested:

Code: Select all

Global _bla
~v::_bla:=!_bla
#IF _bla
~LButton::click()
#IF
~Lbutton & ~RButton::autofire()
~Rbutton & ~LButton::autofire()
click()
{
	While, _bla And GetKeyState("LButton","P")
	{
		Send, {i Down}
		sleep 10
		mouseXY(0,1)
		sleep 10
		mouseXY(0,1)
		sleep 10
		mouseXY(0,2)
		sleep 10
		mouseXY(0,2)
		sleep 10
		mouseXY(-0,1)
		Send, {i up}
	} ; While
} ;; click()
autofire()
{
	While, !_bla
	{
		If !GetKeyState("LButton","P") Or _bla
			Break
		sleep 10
		mouseXY(0,4)
		If !GetKeyState("LButton","P") Or _bla
			Break
		sleep 30
		mouseXY(0,4)
		If !GetKeyState("LButton","P") Or _bla
			Break
		sleep 30
		mouseXY(0,3)
		If !GetKeyState("LButton","P") Or _bla
			Break
		sleep 25
		If !GetKeyState("LButton","P") Or _bla
			Break
	} ;; While
} ;; autofire()
mouseXY(x,y)
{
	DllCall("mouse_event",uint,1,int,x,int,y,uint,0,int,0)
}
User avatar
telanx
Posts: 68
Joined: 10 Jan 2020, 14:31

Re: assembling two scripts in one with same functions

11 Jan 2020, 07:57

If I understand correctly, try this code and modify it.
A simple example, you can make one key map two functions and switch through hotkey.

Code: Select all

; default value
switch:="1"

; Change the value and switch functions with hotkey
v::
    if (switch != "2") {
        switch:="2"
    } else {
        switch:="1"
    }
    return

;same key different functions
#IF switch = "1"
~LButton & ~RButton::
    msgbox switch=%switch%; This is function1 code segment
    return
#IF
;another funcion
#IF switch = "2"
~LButton & ~RButton::
    msgbox switch=%switch%; This is function2 code segment
    return
#IF
Jgorski31
Posts: 5
Joined: 11 Jan 2020, 06:13

Re: assembling two scripts in one with same functions

11 Jan 2020, 07:59

Rohwedder model seems to work.
i'll test it right now
thank you guys
Evil-e
Posts: 146
Joined: 04 Sep 2018, 11:09

Re: assembling two scripts in one with same functions

11 Jan 2020, 12:28

@Jgorski31

Something you may consider that works fantastic for me. I play MX vs ATV: Reflex and has motorcycles, ATV's, buggies and trucks. There are key functions for leaning forward, backward, left and right, but for some odd reason, these controls VARY between motorcycles and other vehicles. So, I program LButton to "lean forward" on motorcycles, but it "leans backward" when a new race starts with trucks or buggies. This plays hell on muscle memory, so I have two AHK.exe files and exchange them while playing the game.

I use "CapsLock" to hot-swap between the two. So both run for 250ms, but then the initial one is closed by the replacement. I can swap in 1/4 second and do so during an actual race. I have both "Keymapping_MX_vs_ATV_Reflex_A.exe" & Keymapping_MX_vs_ATV_Reflex_B.exe" files within the installation directory of my game. This is 100% reliable and works puurrrfectly :)

When I have JUST finished a race with motorcyles and start a new race with buggies or trucks, I hit "CapsLock"

Code: Select all

SetCapsLockState Off
CapsLock::
{
        Run, "D:\Games\MX vs ATV\Reflex\Keymapping_MX_vs_ATV_Reflex_B.exe
        sleep, 250
        Process, Close, Keymapping_MX_vs_ATV_Reflex_A.exe
        sleep, 500
}
return
When I have JUST finished a race with buggies or trucks and start a new race with a motorcycle, I hit "CapsLock"

Code: Select all

SetCapsLockState Off
CapsLock::
{
        Run, "D:\Games\MX vs ATV\Reflex\Keymapping_MX_vs_ATV_Reflex_A.exe 
        sleep, 250
        Process, Close, Keymapping_MX_vs_ATV_Reflex_B.exe
        sleep, 500
}
return
The scripts are identical in both AHK.exe files, with the exception of the above and "Lean Forward" & "Lean Backward" are swapped. This is an absolutely fantastic and simple solution to hot-swap scripts and may be an easier way to solve your needs.

:morebeard:
I have a bit of experience opening and sending commands to game console and CMD.exe... just ask :)
Jgorski31
Posts: 5
Joined: 11 Jan 2020, 06:13

Re: assembling two scripts in one with same functions

14 Jan 2020, 08:46

hello again !

So i made some test and @Rohwedder Script seems to work fine to me.
But i still encounter some issues.

Some time i would switch mode by accident and end up runing the undesired function. so i would like to know if i could set 2 hotkeys to switch between the functions.

Something like

V:: enable function1 and disable Function2 (only if function 2 is active)
B:: Enable function2 and disable Function1 (only if function1 is active)

This would be the ultimate script to me i guess.

So far i tried to do this but i'm certainly doing it wrong . maybe some of you know better

Code: Select all




~*v::_bla:=!_bla
~*b::_rat:=!_rat

#IF _bla
~LButton::click()

#IF _rat
~Lbutton & ~RButton::autofire()
~Rbutton & ~LButton::autofire()


click()
{
	While, _bla And GetKeyState("LButton","P")
	{
                sleep 10
		FUNCTION 1
                Sleep 10

	} ; While
} ;; click()
autofire()
{
	While, _rat and  And GetKeyState("LButton","P")
	{
		 sleep 10
		FUNCTION 2
                Sleep 10

	} ;; While
} ;; autofire()
mouseXY(x,y)
{
	DllCall("mouse_event",uint,1,int,x,int,y,uint,0,int,0)
User avatar
telanx
Posts: 68
Joined: 10 Jan 2020, 14:31

Re: assembling two scripts in one with same functions

14 Jan 2020, 09:31

Use this structure.

Code: Select all

test1() {
    msgbox test1
    return
}

test2() {
    msgbox test2
    return
}

; t default is flase, so it is '!t'
~v::t:=!t

#IF t
~RButton & ~LButton::test1()
#IF

#IF !t
~LButton::test2()
#IF
Jgorski31
Posts: 5
Joined: 11 Jan 2020, 06:13

Re: assembling two scripts in one with same functions

14 Jan 2020, 09:42

hi @telanx
i like the idea and written that way the script is easier to understand for a noob like me.
but now i'm trying to replace the switch key ( ~v::t:=!t ) by 2 keys to select choose the two "modes"
User avatar
telanx
Posts: 68
Joined: 10 Jan 2020, 14:31

Re: assembling two scripts in one with same functions

14 Jan 2020, 09:58

You mean?

Code: Select all

~v::t:=true
~b::t:=false

#IF t
~LButton::click()
#IF

#IF !t
~Lbutton & ~RButton::autofire()
~Rbutton & ~LButton::autofire()
#IF
Jgorski31
Posts: 5
Joined: 11 Jan 2020, 06:13

Re: assembling two scripts in one with same functions

14 Jan 2020, 10:01

And this was so simple :lol:

Thank you sir. i'll run some tests

Return to “Ask For Help”

Who is online

Users browsing this forum: Bad husband, boiler, gecko, Google [Bot], Ian, PuzzledGreatly and 340 guests