Many codes with little differences between them Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
frd
Posts: 42
Joined: 19 Jul 2018, 10:01

Many codes with little differences between them

23 Aug 2018, 11:31

I have 20 codes like these:

Code: Select all

F1:
(Huge code)
Send, 001
(Huge code)
Return

F2:
(Huge code)
Send, 002
(Huge code)
Return

F3:
(Huge code)
Send, 001
(Huge code)
Return
It was driving me crazy that each time I had to change to code, I had to change it twenty times. So I started doing this:

Code: Select all

F1:
Gosub, HUGECODE01
Send, 001
Gosub, HUGECODE02
Return

Hugecode01:
Bla bla bla
Return

Hugecode02:
Bla bla bla
Return
Is there a better way to do this?
Thanks!
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Many codes with little differences between them

23 Aug 2018, 11:56

the most straight forward way of doing it if all thats ever gonna change is what ure sending, is to use a template method:

Code: Select all

F1::customSend("001")
F2::customSend("002")
F3::customSend("003")

customSend(key) {
	hugeCode()
	Send % key
	hugeCode2()
}

hugeCode() {
	/*
		real
		huge
		stuff
		up
		in
		here
	*/
}

hugeCode2() {
	/*
		some
		other
		huge
		stuff
	*/
}
frd
Posts: 42
Joined: 19 Jul 2018, 10:01

Re: Many codes with little differences between them

24 Aug 2018, 12:35

Thanks a lot! I will sign that this is a solved issue. Could you please only do me a last favor? Would you show me how can I do it if I also have to do a imagesearch for different images (but all the rest is exactly the same)?

Thanks in advance!!!
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Many codes with little differences between them  Topic is solved

24 Aug 2018, 15:07

idk what ur actual code looks like. just add whatever other parameters u need to the function

Code: Select all

F1::customCommand("001", "1.png")
F2::customCommand("002", "2.png")
F3::customCommand("003", "3.png")

customCommand(key, imgFile) {
	hugeCode()
	Send % key
	ImageSearch x, y, 0, 0, A_ScreenWidth, A_ScreenHeight, % imgFile
	hugeCode2()
}

hugeCode() {
	/*
		real
		huge
		stuff
		up
		in
		here
	*/
}

hugeCode2() {
	/*
		some
		other
		huge
		stuff
	*/
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Anput, Google [Bot], jameswrightesq and 279 guests