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

  • 本文关键字:代码 记得 对不起 Autohotkey autohotkey
  • 更新时间 :
  • 英文 :


我在autohotkey中制作了一个大脚本,帮助我在空闲的游戏中研磨物品,它需要为每栋建筑重复很多次一些代码,所以我想把它浓缩成一行,我认为它可能在常规编程中被称为变量或类,但对于autohotkey来说似乎不是这样,我一辈子都不知道如何创建一个里面有代码的关键词,我甚至不知道该怎么称呼它,所以我不能用谷歌搜索它。就像它是一样

实际的代码是这样的,但有点长,我想稍后为不同的按钮按下选项添加多个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次,但很快就会变得非常混乱!

谁能给我指个正确的方向吗对不起!

就像@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}
}

在这个代码中,您甚至可以使用不带";如果是";,因为没有要传递和验证/使用的参数。。所以只是:

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}
}

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

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
}

最新更新