Вопрос

I am still in the process of learning how to script my own GUIs. Koda has been a big help, I have been messing around with it, learning little bits at a time. However, I have encountered an error that I can not seem to get around. The current GUI I am working on is a simple tool to change bot settings for a game. This gui should have 5 buttons for difficulty setting, and a list of check boxes for the number of bots.

BUT, when I select a difficulty level, bot number, and hit save I get this error:

'Variable used without being declared'  

The error is occuring on this line (botSAVE button at the end):

FileWriteLine($file2, "aiSettings.setMaxNBots " & $botnum)    

I thought the button functions 'Func numClick()' set this variable based on the button that was pressed, but obviously I have overlooked something. Hopefully it is an easy fix. Does anybody see what might be causing this error? I have posted this question on autoitcsript forums but they seem to shy away from game automation scripts so I didn't really get much help over there.

#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  

-If anybody sees what I have done wrong, OR has a link to a topic that covers declaring variables with a button click, Help is always appreciated. The only topics I have found are incredibly vague, or differ slightly from what I am look for

Это было полезно?

Решение

When you want to use a variable value outside of a function as well, you must declare its scope to be global. So just add a line Global $botnum (probably with a default value) before your many function declarations...

And obviously the same with $skillset.

The error is raised because you used Opt("MustDeclareVars", 1). But it wouldn't work anyways when you wouldn't declare these variables as global, because the value is set in the function but discarded again as soon as the function ends. So when you'd use them uninitialized in another function then, you would always receive the default value of an uninitialized variable.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top