Improve this script?

Ask gaming related questions (AHK v1.1 and older)
WizyTheNinja
Posts: 3
Joined: 28 Sep 2022, 23:45

Improve this script?

Post by WizyTheNinja » 28 Sep 2022, 23:51

Hi, New to AHK, and I am just looking for some tips on my code. It runs a bit slow, and I have to run Click twice, because sometimes it doesn't register in game.

I tried using GuiControl to change the text, instead of making and deleting multiple, but it wouldn't replicate the entire text for some reason. Instead of reading "V Formation" It would just say "V"

My main goal is to improve the rate at which clicks occur. I assume MouseMove is a bit intensive, so it takes longer than instantly clicking it, but the easiest way I was able to create the formations needed for my troops was to use relative positioning.

Any thoughts/tips would be appreciated. Thanks!

Code: Select all

RelativeClick(rX := 0, rY := 0) {
    MouseMove, %rX%, %rY%,0, R
	Click
	Click
}

Form = Circle
Active = false
Moving = false

+left::
if (Active = false)
{
Form = V
Active = true
Gui, -Caption +AlwaysOnTop +Owner +LastFound +E0x20
WinSet, TransColor, 1
Gui, Color, 1
Gui, Font, s25 w700 q4, Times New Roman
Gui, Add, Text, vFormationVar, %Form% Formation
Gui, Show, NoActivate 
WinMove,0,0
sleep, 1500
Gui, Destroy
GuiControl,, FormationVar, %Form% Formation
Active = false
}
return

+up::
if (Active = false)
{
Form = Wedge
Active = true
Gui, -Caption +AlwaysOnTop +Owner +LastFound +E0x20
WinSet, TransColor, 1
Gui, Color, 1
Gui, Font, s25 w700 q4, Times New Roman
Gui, Add, Text, vFormationVar, %Form% Formation
Gui, Show, NoActivate 
WinMove,0,0
sleep, 1500
Gui, Destroy
GuiControl,, FormationVar, %Form% Formation
Active = false
}
return

+right::
if (Active = false)
{
Form = Square
Active = true
Gui, -Caption +AlwaysOnTop +Owner +LastFound +E0x20
WinSet, TransColor, 1
Gui, Color, 1
Gui, Font, s25 w700 q4, Times New Roman
Gui, Add, Text, vFormationVar, %Form% Formation
Gui, Show, NoActivate 
WinMove,0,0
sleep, 1500
Gui, Destroy
GuiControl,, FormationVar, %Form% Formation
Active = false
}
return

+down::
if (Active = false)
{
Form = Circle
Active = true
Gui, -Caption +AlwaysOnTop +Owner +LastFound +E0x20
WinSet, TransColor, 1
Gui, Color, 1
Gui, Font, s25 w700 q4, Times New Roman
Gui, Add, Text, vFormationVar, %Form% Formation
Gui, Show, NoActivate 
WinMove,0,0
sleep, 1500
Gui, Destroy
GuiControl,, FormationVar, %Form% Formation
Active = false
}
return

up::
if (Moving = false)
Moving = true
if (Form = "Wedge")
{
	MouseGetPos, X, Y
	SendInput, {1}
	RelativeClick(-60, -120)
	SendInput, {2}
	RelativeClick(120, 0)
	SendInput, {3}
	RelativeClick(-180, 120)
	SendInput, {4}
	RelativeClick(120, 0)
	SendInput, {5}
	RelativeClick(120, 0)
	SendInput, {6}
	RelativeClick(-360, 120)
	SendInput, {7}
	RelativeClick(120, 0)
	SendInput, {8}
	RelativeClick(120, 0)
	SendInput, {9}
	RelativeClick(120, 0)
	SendInput, {0}
	RelativeClick(120, 0)
	SendInput, {0}
    MouseMove, X, Y,
}
else if (Form = "Square")
{
	MouseGetPos, X, Y
	SendInput, {1}
	RelativeClick(-120, -120)
	SendInput, {2}
	RelativeClick(240, 0)
	SendInput, {3}
	RelativeClick(-360, 120)
	SendInput, {4}
	RelativeClick(240, 0)
	SendInput, {5}
	RelativeClick(240, 0)
	SendInput, {6}
	RelativeClick(-480, 120)
	SendInput, {7}
	RelativeClick(120, 0)
	SendInput, {8}
	RelativeClick(120, 0)
	SendInput, {9}
	RelativeClick(120, 0)
	SendInput, {0}
	RelativeClick(120, 0)
	SendInput, {0}
    MouseMove, X, Y,
}
else if (Form = "V")
{
	MouseGetPos, X, Y
	SendInput, {1}
	RelativeClick(-60, -60)
	SendInput, {2}
	RelativeClick(60, 0)
	SendInput, {3}
	RelativeClick(-120, 60)
	SendInput, {7}
	RelativeClick(180, 0)
	SendInput, {5}
	RelativeClick(-240, 60)
	SendInput, {6}
	RelativeClick(300, 0)
	SendInput, {4}
	RelativeClick(-360, 60)
	SendInput, {8}
	RelativeClick(420, 0)
	SendInput, {9}
	RelativeClick(-480, 60)
	SendInput, {0}
	RelativeClick(540, 0)
	SendInput, {0}
    MouseMove, X, Y,
}
else if (Form = "Circle")
{
	MouseGetPos, X, Y
	SendInput, {4}
	RelativeClick(0, -30)
	SendInput, {8}
	RelativeClick(0, 60)
	SendInput, {1}
	RelativeClick(0, 90)
	SendInput, {2}
	RelativeClick(0, -240)
	SendInput, {3}
	RelativeClick(120, 120)
	SendInput, {5}
	RelativeClick(-240, 0)
	SendInput, {6}
	RelativeClick(35.147, 84.853)
	SendInput, {7}
	RelativeClick(169.706, 0)
	SendInput, {9}
	RelativeClick(0, -169.706)
	SendInput, {0}
	RelativeClick(-169.706, 0)
	SendInput, {0}
    MouseMove, X, Y,
}
Moving = false
return






down::
if (Moving = false)
Moving = true
if (Form = "Wedge")
{
	MouseGetPos, X, Y
	SendInput, {1}
	RelativeClick(-60, 120)
	SendInput, {2}
	RelativeClick(120, 0)
	SendInput, {3}
	RelativeClick(-180, -120)
	SendInput, {4}
	RelativeClick(120, 0)
	SendInput, {5}
	RelativeClick(120, 0)
	SendInput, {6}
	RelativeClick(-360, -120)
	SendInput, {7}
	RelativeClick(120, 0)
	SendInput, {8}
	RelativeClick(120, 0)
	SendInput, {9}
	RelativeClick(120, 0)
	SendInput, {0}
	RelativeClick(120, 0)
	SendInput, {0}
    MouseMove, X, Y,
}
else if (Form = "Square")
{
	MouseGetPos, X, Y
	SendInput, {1}
	RelativeClick(-120, 120)
	SendInput, {2}
	RelativeClick(240, 0)
	SendInput, {3}
	RelativeClick(-360, -120)
	SendInput, {4}
	RelativeClick(240, 0)
	SendInput, {5}
	RelativeClick(240, 0)
	SendInput, {6}
	RelativeClick(-480, -120)
	SendInput, {7}
	RelativeClick(120, 0)
	SendInput, {8}
	RelativeClick(120, 0)
	SendInput, {9}
	RelativeClick(120, 0)
	SendInput, {0}
	RelativeClick(120, 0)
	SendInput, {0}
    MouseMove, X, Y,
}
else if (Form = "V")
{
	MouseGetPos, X, Y
	SendInput, {1}
	RelativeClick(-60, 60)
	SendInput, {2}
	RelativeClick(60, 0)
	SendInput, {3}
	RelativeClick(-120, -60)
	SendInput, {7}
	RelativeClick(180, 0)
	SendInput, {5}
	RelativeClick(-240, -60)
	SendInput, {6}
	RelativeClick(300, 0)
	SendInput, {4}
	RelativeClick(-360, -60)
	SendInput, {8}
	RelativeClick(420, 0)
	SendInput, {9}
	RelativeClick(-480, -60)
	SendInput, {0}
	RelativeClick(540, 0)
	SendInput, {0}
    MouseMove, X, Y,
}
else if (Form = "Circle")
{
	MouseGetPos, X, Y
	SendInput, {4}
	RelativeClick(0, -30)
	SendInput, {8}
	RelativeClick(0, 60)
	SendInput, {1}
	RelativeClick(0, 90)
	SendInput, {2}
	RelativeClick(0, -240)
	SendInput, {3}
	RelativeClick(120, 120)
	SendInput, {5}
	RelativeClick(-240, 0)
	SendInput, {6}
	RelativeClick(35.147, 84.853)
	SendInput, {7}
	RelativeClick(169.706, 0)
	SendInput, {9}
	RelativeClick(0, -169.706)
	SendInput, {0}
	RelativeClick(-169.706, 0)
	SendInput, {0}
    MouseMove, X, Y,
}
Moving = false
return







left::
if (Moving = false)
Moving = true
if (Form = "Wedge")
{
	MouseGetPos, X, Y
	SendInput, {1}
	RelativeClick(-120, -60)
	SendInput, {2}
	RelativeClick(0, 120)
	SendInput, {3}
	RelativeClick(120, -180)
	SendInput, {4}
	RelativeClick(0, 120)
	SendInput, {5}
	RelativeClick(0, 120)
	SendInput, {6}
	RelativeClick(120, -360)
	SendInput, {7}
	RelativeClick(0, 120)
	SendInput, {8}
	RelativeClick(0, 120)
	SendInput, {9}
	RelativeClick(0, 120)
	SendInput, {0}
	RelativeClick(0, 120)
	SendInput, {0}
    MouseMove, X, Y,
}
else if (Form = "Square")
{
	MouseGetPos, X, Y
	SendInput, {1}
	RelativeClick(-120, -120)
	SendInput, {2}
	RelativeClick(0, 240)
	SendInput, {3}
	RelativeClick(120, -360)
	SendInput, {4}
	RelativeClick(0, 240)
	SendInput, {5}
	RelativeClick(0, 240)
	SendInput, {6}
	RelativeClick(120, -480)
	SendInput, {7}
	RelativeClick(0, 120)
	SendInput, {8}
	RelativeClick(0, 120)
	SendInput, {9}
	RelativeClick(0, 120)
	SendInput, {0}
	RelativeClick(0, 120)
	SendInput, {0}
    MouseMove, X, Y,
}
else if (Form = "V")
{
	MouseGetPos, X, Y
	SendInput, {1}
	RelativeClick(-60, -60)
	SendInput, {2}
	RelativeClick(0, 60)
	SendInput, {3}
	RelativeClick(60, -120)
	SendInput, {7}
	RelativeClick(0, 180)
	SendInput, {5}
	RelativeClick(60, -240)
	SendInput, {6}
	RelativeClick(0, 300)
	SendInput, {4}
	RelativeClick(60, -360)
	SendInput, {8}
	RelativeClick(0, 420)
	SendInput, {9}
	RelativeClick(60, -480)
	SendInput, {0}
	RelativeClick(0, 540)
	SendInput, {0}
    MouseMove, X, Y,
}
else if (Form = "Circle")
{
	MouseGetPos, X, Y
	SendInput, {4}
	RelativeClick(-30, 0)
	SendInput, {8}
	RelativeClick(60, 0)
	SendInput, {1}
	RelativeClick(90, 0)
	SendInput, {2}
	RelativeClick(-240, 0)
	SendInput, {3}
	RelativeClick(120, 120)
	SendInput, {5}
	RelativeClick(0, -240)
	SendInput, {6}
	RelativeClick(84.853, 35.147)
	SendInput, {7}
	RelativeClick(0, 169.706)
	SendInput, {9}
	RelativeClick(-169.706, 0)
	SendInput, {0}
	RelativeClick(0, -169.706)
	SendInput, {0}
    MouseMove, X, Y,
}
Moving = false
return




right::
if (Moving = false)
Moving = true
if (Form = "Wedge")
{
	MouseGetPos, X, Y
	SendInput, {1}
	RelativeClick(120, -60)
	SendInput, {2}
	RelativeClick(0, 120)
	SendInput, {3}
	RelativeClick(-120, -180)
	SendInput, {4}
	RelativeClick(0, 120)
	SendInput, {5}
	RelativeClick(0, 120)
	SendInput, {6}
	RelativeClick(-120, -360)
	SendInput, {7}
	RelativeClick(0, 120)
	SendInput, {8}
	RelativeClick(0, 120)
	SendInput, {9}
	RelativeClick(0, 120)
	SendInput, {0}
	RelativeClick(0, 120)
	SendInput, {0}
    MouseMove, X, Y,
}
else if (Form = "Square")
{
	MouseGetPos, X, Y
	SendInput, {1}
	RelativeClick(120, -120)
	SendInput, {2}
	RelativeClick(0, 240)
	SendInput, {3}
	RelativeClick(-120, -360)
	SendInput, {4}
	RelativeClick(0, 240)
	SendInput, {5}
	RelativeClick(0, 240)
	SendInput, {6}
	RelativeClick(-120, -480)
	SendInput, {7}
	RelativeClick(0, 120)
	SendInput, {8}
	RelativeClick(0, 120)
	SendInput, {9}
	RelativeClick(0, 120)
	SendInput, {0}
	RelativeClick(0, 120)
	SendInput, {0}
    MouseMove, X, Y,
}
else if (Form = "V")
{
	MouseGetPos, X, Y
	SendInput, {1}
	RelativeClick(60, -60)
	SendInput, {2}
	RelativeClick(0, 60)
	SendInput, {3}
	RelativeClick(-60, -120)
	SendInput, {7}
	RelativeClick(0, 180)
	SendInput, {5}
	RelativeClick(-60, -240)
	SendInput, {6}
	RelativeClick(0, 300)
	SendInput, {4}
	RelativeClick(-60, -360)
	SendInput, {8}
	RelativeClick(0, 420)
	SendInput, {9}
	RelativeClick(-60, -480)
	SendInput, {0}
	RelativeClick(0, 540)
	SendInput, {0}
    MouseMove, X, Y,
}
else if (Form = "Circle")
{
	MouseGetPos, X, Y
	SendInput, {4}
	RelativeClick(-30, 0)
	SendInput, {8}
	RelativeClick(60, 0)
	SendInput, {1}
	RelativeClick(90, 0)
	SendInput, {2}
	RelativeClick(-240, 0)
	SendInput, {3}
	RelativeClick(120, 120)
	SendInput, {5}
	RelativeClick(0, -240)
	SendInput, {6}
	RelativeClick(84.853, 35.147)
	SendInput, {7}
	RelativeClick(0, 169.706)
	SendInput, {9}
	RelativeClick(-169.706, 0)
	SendInput, {0}
	RelativeClick(0, -169.706)
	SendInput, {0}
    MouseMove, X, Y,
}
Moving = false
return

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

Re: Improve this script?

Post by mikeyww » 29 Sep 2022, 08:20

Just one idea to streamline a bit:

Code: Select all

form = Circle

Switch form {
 Case "Square": doubleClikAndSend(120, 120, 5)
 Case "Circle": doubleClikAndSend(220, 220, 3)
}

doubleClikAndSend(x, y, key) {
 MouseClick,, x, y, COUNT := 2, SLOW := 0,, R
 Send {%key%}
}

WizyTheNinja
Posts: 3
Joined: 28 Sep 2022, 23:45

Re: Improve this script?

Post by WizyTheNinja » 29 Sep 2022, 08:39

So, if I am understanding correctly,
When calling the switch form function, it will send the cords to the click and send function, along with which key to press? Is this to help performance, or to tidy the code up?
My main goal is to have 4 formations, and 4 directions. Pressing up/down/left/right would determine which direction the troops face, while using an if statement to determine which formation is currently active.
Thanks!

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

Re: Improve this script?

Post by mikeyww » 29 Sep 2022, 08:59

Yes. This idea does not change much but might ease the work of coding a bit. Enjoy!

WizyTheNinja
Posts: 3
Joined: 28 Sep 2022, 23:45

Re: Improve this script?

Post by WizyTheNinja » 29 Sep 2022, 09:05

Okay, that's what I thought, and was wondering about processes to shorten the code, as I do not like repeating code for no reason. Thanks again!

Post Reply

Return to “Gaming Help (v1)”