r/AutoHotkey • • 5h ago

v2 Script Help Help with changing variable using menu

0 Upvotes

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