我仍然在学习如何编写自己的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("Battlefield 2 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
- 如果有人看到我做错了什么,或者有一个链接到一个主题,其中包含按钮点击声明变量,总是感谢帮助。我发现的唯一主题是非常模糊,或者与我的目标略有不同
答案 0 :(得分:2)
如果要在函数外部使用变量值,则必须将其范围声明为全局。所以只需在许多函数声明之前添加一行Global $botnum
(可能带有默认值)......
与$skillset
显然相同。
由于您使用了Opt("MustDeclareVars", 1)
,因此引发了错误。但是,如果不将这些变量声明为全局变量,它将无法工作,因为该值在函数中设置,但在函数结束后再次丢弃。因此,当您在其他函数中使用未初始化时,您将始终收到未初始化变量的默认值。