Why doesn't my script work?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
lukeman3000
Posts: 23
Joined: 20 Aug 2017, 00:52

Why doesn't my script work?

24 Aug 2017, 00:52

My script does work up to the point where the while loop exists. ScummVM launches correctly. However, what I want the script to do is continue to run and execute a command when it detects that scummvm.exe has closed. Every time I close scummvm, nothing happens.

Code: Select all

#NoTrayIcon
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

RunWait, "..\sc55 scummvm.exe"
Run, ".\scummvm.exe" "--no-console" "--config=.\scummvm.ini" "samnmax"
Process, Exist, scummvm.exe ;wait until scummvm.exe is found before continuing
ScummVM = %ErrorLevel%  ; Save the value immediately since ErrorLevel is often changed.
while ScummVM != 0 ;while scummvm is running i.e., error level is not equal to 0
{} ;"do nothing"
Run, "..\kill.exe" ;action taken when scummvm is closed
return
Edit: This is what I came up with that actually seems to work. Is this good syntax and stable enough to run continuously for perhaps several hours while scummvm is still active?

Code: Select all

#NoTrayIcon
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

RunWait, "..\sc55 scummvm.exe"
Run, ".\scummvm.exe" "--no-console" "--config=.\scummvm.ini" "samnmax"
Loop
{
	Process, Exist, scummvm.exe
	ScummVM = %ErrorLevel%  ; Save the value immediately since ErrorLevel is often changed.
}
until ScummVM = 0
Run, "..\kill.exe"
return
Also, why does the latter work but the former does not? I also tried

Code: Select all

Loop
{
	Process, Exist, scummvm.exe
	ScummVM = %ErrorLevel%  ; Save the value immediately since ErrorLevel is often changed.
}
while ScummVM != 0
But this also didn't work. Why not?
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Why doesn't my script work?

24 Aug 2017, 01:34

If you want to repeatedly check for Errorlevel, then best check for ErrorLevel inside a loop. see https://autohotkey.com/boards/viewtopic ... 28#p165928

Hmm, let me try and improve the explanation: You first version only produces the ErrorLevel ONCE, After that you repeatedly check if it has changed. But you did not CHECK the errorLevel, you check the ONLY value for ScummVM you ever received. There is no point to check the same variable over and over again, you have to update it and check.
lukeman3000
Posts: 23
Joined: 20 Aug 2017, 00:52

Re: Why doesn't my script work?

24 Aug 2017, 22:57

Thanks wolf, got it to work.

Another question - why is this not working for me?

Code: Select all

#NoTrayIcon
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

EnvGet, center, SDL_VIDEO_CENTERED
if (center)
{
	return
} else {
	EnvSet, SDL_VIDEO_CENTERED, 1
}
return
I want to check if the environment variable SDL_VIDEO_CENTERED is defined. If not, I want to define it as 1. Also, it can/should be a user environment variable, not a system environment variable. In batch, I can accomplish it via the following:

Code: Select all

REM Defines environment variable to center DOSBox and ScummVM game windows if undefined

if defined SDL_VIDEO_CENTERED (
	exit
) else (
	setx SDL_VIDEO_CENTERED 1
)
exit
So basically I'm just trying to recreate that batch code in AHK.
lukeman3000
Posts: 23
Joined: 20 Aug 2017, 00:52

Re: Why doesn't my script work?

24 Aug 2017, 23:44

Thanks. I already stumbled across using comspec to do it. However, I still want to be able to check if the variable exists first and to only take action if it does not. Here's what I've got:

Code: Select all

; NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

commands=
(join&
	setx SDL_VIDEO_CENTERED 1
)

EnvGet, center, SDL_VIDEO_CENTERED
if center =
{
	runwait, %comspec% /c %commands%, ,Hide
} else {
	return
}
return
It doesn't work. It goes straight to the "else" of the if statement and never writes the variable. If I switch the position of the statements, then it writes the variable. But it always writes it. So in other words, it always does the "else".

Also, I know that the commands=(join& stuff is unnecessary since i'm only using one command, but I want to keep it in case I decide to add more commands, unless I shouldn't for some reason.
obeeb
Posts: 140
Joined: 20 Feb 2014, 19:15

Re: Why doesn't my script work?

24 Aug 2017, 23:57

Works for me, you probably already have SDL_VIDEO_CENTERED environment variable, try with a different one that you definitely don't have.
lukeman3000
Posts: 23
Joined: 20 Aug 2017, 00:52

Re: Why doesn't my script work?

25 Aug 2017, 00:17

obeeb wrote:Works for me, you probably already have SDL_VIDEO_CENTERED environment variable, try with a different one that you definitely don't have.
Lol, you're right. It does work.

I was testing by deleting the environment variable and then running the script. But I didn't realize I had to hit "ok" in the environment variable dialogue box for it to to actually delete the variable.

Image

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: doanmvu, Google [Bot], RSable and 388 guests