Autohotkey 通过在代码中使用代码来减少我的代码的大小(我不记得它叫什么对不起!)

Autohotkey reducing the size of my code by using code within code (i cant remember what it's called sorry!)

我在 autohotkey 中做了一个大脚本来帮助我在闲置游戏中磨练物品,它需要为每个建筑重复很多次代码,所以我想将它压缩成一行,我认为它可能在常规编程中被称为变量或 类 但它似乎不是自动热键,我无法为我的生活弄清楚如何创建类似关键字的东西,其中包含代码,我什至不知道该怎么称呼它,所以我不能 google。好像是

实际代码是这样的,但有点长,我想稍后为不同的按钮按下选项添加多个 if 语句

;-----------------------------------------------------------------------Building 1
splashtexton,500,100,Building,1
if (Building1 = 1){
Sleep, 2001
send {R DOWN}
Sleep, 1000
send {R UP}
Sleep, 100
send {U}
}Sleep, 1001
send {Q}
;-----------------------------------------------------------------------Building 2
splashtexton,500,100,Building,2
if (Building2 = 1){
Sleep, 2001
send {E DOWN}
Sleep, 1000
send {E UP}
Sleep, 100
send {U}}
Sleep, 1001
send {Q}

但我更喜欢

(Reuse this code)
Sleep, 2001
send {R DOWN}
Sleep, 1000
send {R UP}
Sleep, 100
send {U}

;-----------------------------------------------------------------------Building 1
splashtexton,500,100,Building,1
if (Building1 = 1){
(Reuse this code)
}
Sleep, 1001
send {Q}
;-----------------------------------------------------------------------Building 2
splashtexton,500,100,Building,2
if (Building2 = 1){
(Reuse this code)
}
Sleep, 1001
send {Q}

然后每次我写“这个组”时它都会重复这些命令,而不是我每次都必须编写相同的代码。到目前为止,我已经做了所有的事情,我完全有能力复制粘贴相同的代码 100 次,但很快就会变得非常混乱!

任何人都可以指出正确的方向抱歉!

这在很多方面都是可行的,最好的方法完全取决于您要重复的代码。
如果代码像您的示例所示那样简单,那么 function 可能就是您想要的。

1::
    DoStuff()
return

2::
    DoStuff()
return

3::
    DoStuff()
return

DoStuff()
{
    Send, hello
    ToolTip, % "This computer has been up for " A_TickCount " ms."
    Sleep, 1000
    ToolTip
}

如@0x464e 所说:

splashtexton,500,100,Building,1
if Building1 = 1
    function_name_here()
Sleep, 1001
send {Q}
splashtexton,500,100,Building,2
if Building2 = 1
    function_name_here()

Sleep, 1001
send {Q}
Return

function_name_here() {
    Sleep, 2001
    send {R DOWN}
    Sleep, 1000
    send {R UP}
    Sleep, 100
    send {U}
}

在此代码中,您甚至可以不使用“if”,因为没有要传递的参数并且validate/used..所以只是:

splashtexton,500,100,Building,1
function_name_here()
Sleep, 1001
send {Q}
splashtexton,500,100,Building,2
function_name_here()

Sleep, 1001
send {Q}
Return

function_name_here() {
    Sleep, 2001
    send {R DOWN}
    Sleep, 1000
    send {R UP}
    Sleep, 100
    send {U}
}