r/AutoHotkey • u/Creative_Shoulder265 • 12h ago
v2 Script Help Help with changing variable using menu
I'm trying to change a string variable to a user submitted one through a menu. The idea is to use the menu to select which variable to change then have a function to take the user input and change the specified variable. But for some reason I can't get the variable to change. Code is below:
NamesFile := FileOpen('.\txt files\Names.txt', 'r')
Name0 := NamesFile.ReadLine()
Name1 := NamesFile.ReadLine()
Name2 := NamesFile.ReadLine()
Name3 := NamesFile.ReadLine()
NamesFile.Close()
+Esc::MainMenu.Show()
MainMenu := Menu()
ChangeNameMenu := Menu()
MainMenu.Add('Change Player Names', ChangeNameMenu)
MainMenu.Add('Close Macro', (*)=>CloseMacro())
ChangeNameMenu.Add('Change Player 0', (*)=>ChangeName(&Name0))
ChangeNameMenu.Add('Change Player 1', (*)=>ChangeName(&Name1))
ChangeNameMenu.Add('Change Player 2', (*)=>ChangeName(&Name2))
ChangeNameMenu.Add('Change Player 3', (*)=>ChangeName(&Name3))
CloseMacro() {
if(MsgBox('Close Macro?',,4) == 'Yes') {
NamesFile := FileOpen('.\txt files\LethalCompanyNames.txt', 'w')
NamesFile.Write(Name0 . '`n')
NamesFile.Write(Name1 . '`n')
NamesFile.Write(Name2 . '`n')
NamesFile.Write(Name3)
ExitApp
}
}
ChangeName(&NameVar) {
Input := InputBox('New Name:')
if(Input.Result == 'OK') {
NameVar := Input.Value
}
}
0
Upvotes
•
u/xyzzy0 8h ago edited 7h ago
Try this. (I changed your file paths, and read and write from/to the same file.)
The trick here is to (1) switch to an array so you don't have to fiddle with ByRef parameters (the function can change the array's members without global/byref in AHKv2), and (2) switch the callback menu func from a fat-arrow function to one with .Bind().
.Bind() runs during the loop, so A_index is read and resolved on each pass (1, 2, 3, 4) and each number is stored with its own menu item. The fat-arrow function: (*) => ChangeName(A_Index) stores only the expression, so it reads A_index at the click (after the loop has already run), not the integer value that it held when the loop ran, so it returns 0.
But then there's another problem. The callback is wrapped up in the menuitem and what it passes along (item name, its position and the menu object) like a GUI control passes ctrl and info. You have to accept and disregard these params because we got rid of the fat-arrow function. So ChangeName has to disregard those parameters with ChangeName(i, *) like a GUI callback does with its * from an OnEvent call.
You might say: well, I didn't want to use a loop in the first place. That's fine, too, and this also works, since you're using actual unchanging integers and not variables for the arguments.
ChangeNameMenu.Add('Change Player 1' , (*) => ChangeName(1))ChangeNameMenu.Add('Change Player 2' , (*) => ChangeName(2))ChangeNameMenu.Add('Change Player 3' , (*) => ChangeName(3))ChangeNameMenu.Add('Change Player 4' , (*) => ChangeName(4))This should work:
•
u/CharnamelessOne 2h ago
The issue is that the VarRefs passed to
ChangeName()in the fat arrow functions don't refer to globals. They are references to uninitialized local variables:https://www.autohotkey.com/docs/v2/Functions.htm#Global
•
u/KozVelIsBest 8h ago
I dont think it needs the if check for checking input.result
also dont need input.value, the variable is just Input
either that is your issue or the pass by reference isnt being used correctly.
try adding some debuging messages in this block to see if the conditions are actually being triggered or not. like include a tool tip message inside the if check block