r/AutoHotkey • • 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

10 comments sorted by

•

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:

#Requires AutoHotkey v2.0
#SingleInstance Force

NamesFile := FileOpen('Names.txt', 'r')
Names := Array()
Loop {
  name := NamesFile.ReadLine()
  if (name) {
    Names.Push(name)
  } else {
    break
  }
} 

NamesFile.Close()

+Esc::MainMenu.Show()

MainMenu := Menu()
ChangeNameMenu := Menu()

MainMenu.Add('Change Player Names', ChangeNameMenu)
MainMenu.Add('Close Macro', (*)=>CloseMacro())

Loop Names.Length {
  ChangeNameMenu.Add('Change Player ' A_Index, ChangeName.Bind(A_index))
}

CloseMacro() {
  if(MsgBox('Close Macro?',,4) == 'Yes') {
    NamesFile := FileOpen('Names.txt', 'w')
    Loop Names.Length {
          NamesFile.Write(Names[A_index] . '`n')
    }
    NamesFile.Close()
    ExitApp()
  }
}

ChangeName(i, *){
  Input := InputBox('New Name:')
  if(Input.Result == 'OK') {
    Names[i] := Input.Value
  }
}

•

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:

Any variable reference in an assume-local function may resolve to a global variable if it is only read. However, if a variable is used in an assignment or with the reference operator (&), it is automatically local by default.

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

•

u/CharnamelessOne 2h ago

I dont think it needs the if check for checking input.result also dont need input.value, the variable is just Input

InputBox returns an object with the properties result and value. You check result to ensure that the user didn't cancel the inputbox. value holds the string entered by the user.
That part of OP's script is completely fine.

•

u/KozVelIsBest 2h ago

yeah. someone already mentioned earlier in a rude way then deleted the comment. I only ever made stuff using v1 in the past, inputbox got changed in v2. never had any reason to build anything in V2 at least by hand now. I built a few new things but just used Claude for it because Claude just built them way faster.

•

u/CharnamelessOne 2h ago

inputbox got changed in v2

For the better, I'd say. Good riddance to ErrorLevel shenanigans.

I still script by hand, mostly. I do it exclusively for shits and giggles, and bossing LLMs around is not really doing it for me.

I do have subjects I know fuck all about clanksplained to me fairly frequently, though.

•

u/KozVelIsBest 2h ago

yeah I dont mind scripting small stuff by hand. but when its like 5000 lines of code. Claude cooks it up in like 5 minutes compared to me having to spend hours, days, weeks maybe lol

•

u/CharnamelessOne 2h ago

I believe it. I do want to spend those weeks, though. I barely even use the stuff I write, so I'm not in any sort of rush.

Some people crochet as a pastime, I type out struct member offsets.

•

u/KozVelIsBest 2h ago

I just creating a tool that displays window/apps in your custom way. you can crop out sections and align position the view any way you like.

the purpose behind it was just to show information on a television for visual reference

https://github.com/ezflow997/LiveView

requires thumbnail previews on taskbar tabs (windows 7 or newer)

0

u/[deleted] 7h ago

[deleted] — view deleted comment

•

u/KozVelIsBest 7h ago

thats why I said "I dont think" meaning that im not certain about it. good to know you care more about being a jerk that actually trying to be helpful for OP lol