我仍在学习如何编写自己的 GUI 脚本。Koda 帮了我大忙,我一直在摆弄它,一点一点地学习。然而,我遇到了一个似乎无法解决的错误。我正在开发的当前 GUI 是一个用于更改游戏机器人设置的简单工具。这个 GUI 应该有 5 个按钮用于设置难度,以及一个用于设置机器人数量的复选框列表。
但是,当我选择难度级别、机器人编号并点击保存时,出现此错误:
'Variable used without being declared'
错误发生在这一行(末尾的 botSAVE 按钮):
FileWriteLine($file2, "aiSettings.setMaxNBots " & $botnum)
我以为按钮函数“Func numClick()”设置了这个变量,但显然我忽略了一些东西。希望这是一个简单的修复。有人知道是什么导致了这个错误吗?我已经在 autoitcsript 论坛上发布了这个问题,但他们似乎回避游戏自动化脚本,所以我在那里没有得到太多帮助。
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <TabConstants.au3>
#include <WindowsConstants.au3>
Opt("GUIOnEventMode", 1)
#Region ### START Koda GUI section ### Form=c:\users\admin\desktop\form1.kxf
$Form1 = GUICreate("BF2 Bot-Tool", 418, 499, 759, 83)
GUISetOnEvent($GUI_EVENT_CLOSE, "Form1Close")
GUISetOnEvent($GUI_EVENT_MINIMIZE, "Form1Minimize")
GUISetOnEvent($GUI_EVENT_MAXIMIZE, "Form1Maximize")
GUISetOnEvent($GUI_EVENT_RESTORE, "Form1Restore")
$Pic1 = GUICtrlCreatePic("C:\Users\admin\Desktop\bflogo.jpg", 0, 0, 417, 233)
$Settings = GUICtrlCreateTab(8, 240, 401, 249)
GUICtrlSetOnEvent(-1, "SettingsChange")
$Bot = GUICtrlCreateTabItem("Bots")
$botSAVE = GUICtrlCreateButton("Save these changes", 220, 433, 169, 33)
GUICtrlSetOnEvent(-1, "botSAVEClick")
$botDEFAULT = GUICtrlCreateButton("Restore default settings", 28, 433, 169, 33)
GUICtrlSetOnEvent(-1, "botDEFAULTClick")
$botskill = GUICtrlCreateGroup(" Bot skill level", 28, 265, 169, 145)
$skill1 = GUICtrlCreateRadio(" RECRUIT", 44, 289, 113, 17)
GUICtrlSetTip(-1, "Easy opposition, a damn turkey shoot")
GUICtrlSetOnEvent(-1, "skill1Click")
$skill2 = GUICtrlCreateRadio(" TRAINED", 44, 313, 113, 17)
GUICtrlSetTip(-1, "Default setting, opponents are deadly at close range")
GUICtrlSetOnEvent(-1, "skill2Click")
$skill3 = GUICtrlCreateRadio(" HARDENED", 44, 337, 113, 17)
GUICtrlSetTip(-1, "Enemies are a force to be reckoned with")
GUICtrlSetOnEvent(-1, "skill3Click")
$skill4 = GUICtrlCreateRadio(" VETERAN", 44, 361, 113, 17)
GUICtrlSetTip(-1, "Enemies are experienced, and dangerous")
GUICtrlSetOnEvent(-1, "skill4Click")
$skill5 = GUICtrlCreateRadio(" ELITE", 44, 385, 113, 17)
GUICtrlSetTip(-1, "Crack shot opponents will strategize against you. You will not survive")
GUICtrlSetOnEvent(-1, "skill5Click")
GUICtrlCreateGroup("", -99, -99, 1, 1)
$botnumber = GUICtrlCreateGroup(" Number of bots ", 220, 265, 169, 145)
$num1 = GUICtrlCreateRadio("2", 236, 289, 25, 17)
GUICtrlSetOnEvent(-1, "num1Click")
$num2 = GUICtrlCreateRadio("4", 236, 313, 25, 17)
GUICtrlSetOnEvent(-1, "num2Click")
$num3 = GUICtrlCreateRadio("6", 236, 337, 25, 17)
GUICtrlSetOnEvent(-1, "num3Click")
$num4 = GUICtrlCreateRadio("8", 236, 361, 25, 17)
GUICtrlSetOnEvent(-1, "num4Click")
$num5 = GUICtrlCreateRadio("10", 236, 385, 33, 17)
GUICtrlSetOnEvent(-1, "num5Click")
$num6 = GUICtrlCreateRadio("12", 284, 289, 33, 17)
GUICtrlSetOnEvent(-1, "num6Click")
$num7 = GUICtrlCreateRadio("14", 284, 313, 33, 17)
GUICtrlSetOnEvent(-1, "num7Click")
$num8 = GUICtrlCreateRadio("16", 284, 337, 33, 17)
GUICtrlSetOnEvent(-1, "num8Click")
$num9 = GUICtrlCreateRadio("18", 284, 361, 33, 17)
GUICtrlSetOnEvent(-1, "num9Click")
$num10 = GUICtrlCreateRadio("20", 284, 385, 33, 17)
GUICtrlSetOnEvent(-1, "num10Click")
$num11 = GUICtrlCreateRadio("24", 340, 289, 33, 17)
GUICtrlSetOnEvent(-1, "num11Click")
$num12 = GUICtrlCreateRadio("28", 340, 313, 33, 17)
GUICtrlSetOnEvent(-1, "num12Click")
$num13 = GUICtrlCreateRadio("32", 340, 337, 33, 17)
GUICtrlSetOnEvent(-1, "num13Click")
$num14 = GUICtrlCreateRadio("36", 340, 361, 33, 17)
GUICtrlSetOnEvent(-1, "num14Click")
$num15 = GUICtrlCreateRadio("40", 340, 385, 33, 17)
GUICtrlSetOnEvent(-1, "num15Click")
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Server = GUICtrlCreateTabItem("Server")
$serverDEFAULT = GUICtrlCreateButton("Restore default settings", 28, 433, 169, 33)
$serverSAVE = GUICtrlCreateButton("Save these changes", 220, 433, 169, 33)
GUICtrlCreateTabItem("")
GUISetState(@SW_SHOW)
Opt("MustDeclareVars",1)
#EndRegion ### END Koda GUI section ###
While 1
Sleep(100)
WEnd
Func Form1Close()
If @GUI_WINHANDLE = $Form1 Then
Exit
EndIf
EndFunc
Func Form1Maximize()
EndFunc
Func Form1Minimize()
EndFunc
Func Form1Restore()
EndFunc
Func num1Click()
$botnum = "2"
EndFunc
Func num2Click()
$botnum = "4"
EndFunc
Func num3Click()
$botnum = "6"
EndFunc
Func num4Click()
$botnum = "8"
EndFunc
Func num5Click()
$botnum = "10"
EndFunc
Func num6Click()
$botnum = "12"
EndFunc
Func num7Click()
$botnum = "14"
EndFunc
Func num8Click()
$botnum = "16"
EndFunc
Func num9Click()
$botnum = "18"
EndFunc
Func num10Click()
$botnum = "20"
EndFunc
Func num11Click()
$botnum = "24"
EndFunc
Func num12Click()
$botnum = "28"
EndFunc
Func num13Click()
$botnum = "32"
EndFunc
Func num14Click()
$botnum = "36"
EndFunc
Func num15Click()
$botnum = "40"
EndFunc
Func SettingsChange()
EndFunc
Func skill1Click()
$skillset = "0.1"
EndFunc
Func skill2Click()
$skillset = "0.3"
EndFunc
Func skill3Click()
$skillset = "0.6"
EndFunc
Func skill4Click()
$skillset = "0.8"
EndFunc
Func skill5Click()
$skillset = "1.0"
EndFunc
Func botDEFAULTClick()
EndFunc
Func botSAVEClick()
Local $file = FileOpen("AIDefault.ai", 1)
If $file = -1 Then
MsgBox(0, "Protected file", "Please ensure that the file 'AIDefault.ai' is not set to Read Only.")
exit
EndIf
FileWriteLine($file, "aiSettings.setNSides 2 ")
FileWriteLine($file, "aiSettings.setAutoSpawnBots 1 ")
FileWriteLine($file, "aiSettings.setMaxNBots 64 ")
FileWriteLine($file, "aiSettings.maxBotsIncludeHumans 1")
FileWriteLine($file, "aiSettings.setBotSkill 0.4 ")
FileWriteLine($file, "run BotNames.ai ")
FileWriteLine($file, "aiSettings.setInformationGridDimension 32")
FileWriteLine($file, "run AIPathFinding.ai")
FileWriteLine($file, "run AIBotChanger.ai ")
FileClose($file)
$file2 = FileOpen("AIBotChanger.ai", 1)
If $file2 = -1 Then
MsgBox(0, "Protected file", "Please ensure that the file 'AIBotChanger.ai' is not set to Read Only.")
Exit
EndIf
FileWriteLine($file2, "aiSettings.overrideMenuSettings 1")
FileWriteLine($file2, "aiSettings.setMaxNBots " & $botnum)
FileWriteLine($file2, "aiSettings.setBotSkill " & $skillset)
FileWriteLine($file2, "aiSettings.maxBotsIncludeHumans 0")
FileClose($file2)
EndFunc
-如果有人发现我做错了什么,或者有关于如何通过单击按钮声明变量的主题的链接,我非常感谢您的帮助。我找到的唯一主题非常模糊,或者与我所寻找的略有不同。
答案1
解决方案:我已经解决了变量问题,似乎“Func numClick()”中的变量需要设置为全局变量。当然需要。:P
无论如何,我最终通过改变这个解决了它:
$botnumber = "2"
$skillset = "0.2"
对此:
Global $botnumber = "2"
Global $skillset = "0.2"
EndFunc
对其余功能执行了相同的操作,现在一切正常。