Assigning ahk_exe with variable Topic is solved

Get help for the alpha version of AutoHotkey v2 here. Please state the v2 version you are working with in the title when making a new topic.
Finallf
Posts: 7
Joined: 30 Oct 2020, 13:52

Assigning ahk_exe with variable

30 Oct 2020, 14:23

I am passing a script v1 to v2.

This way it works:

Code: Select all

SetTimer "checkwin", 1000

checkwin() {
	if !WinExist("ahk_exe snes9x-x64.exe")
		ExitApp
}
But I need it to work like this:

Code: Select all

AhkName := "ahk_exe snes9x-x64.exe"
SetTimer "checkwin", 1000

checkwin() {
	if !WinExist("%AhkName%")
		ExitApp
}
or like this:

Code: Select all

AhkName := "snes9x-x64.exe"
SetTimer "checkwin", 1000

checkwin() {
	if !WinExist("ahk_exe %AhkName%")
		ExitApp
}

I tried in many ways and nothing to accept the variable.
User avatar
kczx3
Posts: 1224
Joined: 06 Oct 2015, 21:39

Re: Assigning ahk_exe with variable  Topic is solved

30 Oct 2020, 14:41

If you need that variable's value to be bound to the function then I guess you will need to make the variable global. Or pass it via a fat arrow function.

GLOBAL

Code: Select all

global AhkName := "ahk_exe snes9x-x64.exe"
SetTimer "checkwin", 1000

checkwin() {
	if !WinExist(AhkName)
		ExitApp
}
FAT ARROW

Code: Select all

AhkName := "ahk_exe snes9x-x64.exe"
SetTimer (*) => checkwin(AhkName), 1000

checkwin(exe) {
	if !WinExist(exe)
		ExitApp
}
Finallf
Posts: 7
Joined: 30 Oct 2020, 13:52

Re: Assigning ahk_exe with variable

30 Oct 2020, 15:04

Thanks, it worked.
I will use it global because I have other places to use the variable, so it is easier to use.
Helgef
Posts: 4479
Joined: 17 Jul 2016, 01:02
Contact:

Re: Assigning ahk_exe with variable

31 Oct 2020, 07:17

Instead of the fat-arrow example, it is often better to use a boundfunc. Eg, SetTimer func('checkwin').bind(AhkName), 1000. Because the fat-arrow still depends on the global variable AhkName, the boundfounc binds the value and doesn't depend on the variable after it has been created.
User avatar
kczx3
Posts: 1224
Joined: 06 Oct 2015, 21:39

Re: Assigning ahk_exe with variable

31 Oct 2020, 07:49

The OP wanted that variables value to be bound at the time the function was called. So binding it wouldn’t help because then it’s static. I suppose you could bind an object with a property.

Return to “AutoHotkey v2 Help”

Who is online

Users browsing this forum: TheArkive and 1 guest