Troubles with closing additional prog on exit Topic is solved

Ask gaming related questions (AHK v1.1 and older)
LegzRwheelz
Posts: 56
Joined: 04 Nov 2019, 17:46

Troubles with closing additional prog on exit

08 Jan 2021, 03:21

:headwall: :wtf:
Hey all, I'm back with an issue that most likely has a simple solution as most of my ahk issues seem to entail.

I hope I am not being difficult and that this fix is simple enough.

I am attempting to create a launcher for launching a trainer alongside GR (Ghostrunner) so my son (who's 6) can play this hard as nails game with ease and when GR's process closes, the trainer closes along with it. I need to prevent the trainer from launching multiple times as well as GR. I have the part figured out for the game already, as you can see. I tend to have difficulties laying things out simply and/or clearly when I am trying to convey what I intend to or need to accomplish, so...Just to clarify on what I am needing to accomplish:

1. Upon launching the script, The script checks to see if trainer.exe is already running (to avoid multiple instances of it running), if not then launch it (minimized preferably). If it is running, then close and reopen it (to ensure the trainer's settings are cleared). Once it is running, the script waits 2 seconds before proceeding to launching GR. If the trainer can be launched minimized, then the order of launching isn't important. As of now, the trainer needs to launch first because it launches on top of GR.
2. GR is then loaded, if GR is already running and the launcher is ran again or clicked too many times, it uses winactivate to bring the game's window to the forefront (i prefer to use ("ahk_exe Ghostrunner-Win64-Shipping.exe") over the window title) as the window it looks for.
3. Next it waits for the ghostrunner window to close as it's cue to close the trainer
4. close the script when both Ghostrunner and the trainer is closed. (exitapp)

I imagine that some of these steps are redundant. I am far from proficient in AHK though. Hopefully all of this makes sense. Here is what I've come up with so far....


Code: Select all

if WinExist("ahk_exe Ghostrunner-Win64-Shipping.exe")  
 {
      WinActivate
  }
   else
Run "F:\LaunchBox\Games\PC Games\Ghostrunner\Trainer.exe", 2
Run "F:\LaunchBox\Games\PC Games\Ghostrunner\Ghostrunner.exe"
Process, Wait, Ghostrunner-Win64-Shipping.exe, 10 ;max 10 sec.
Process, WaitClose, Ghostrunner-Win64-Shipping.exe
Process, Wait, Trainer.exe
Process, WaitClose, Trainer.exe
return
exitapp

As you can tell, it is not doing what I am intending for it to do.

Thank you in advance. I feel like a needy child. lol

Last, but most certainly not least...I would like to say thanks to all of you for all the help you selflessly provide to others. Be it here or IRL. :bravo:
User avatar
mikeyww
Posts: 27198
Joined: 09 Sep 2014, 18:38

Re: Troubles with closing additional prog on exit

08 Jan 2021, 08:37

Work on getting step #1 working first, and forget about the rest for now. I don't see any sort of WinClose activity at the moment, so perhaps you want to add that. Would also review syntax for :arrow: Run.
LegzRwheelz
Posts: 56
Joined: 04 Nov 2019, 17:46

Re: Troubles with closing additional prog on exit

08 Jan 2021, 21:03

Thank you for commenting, but that is what I need help with. I am far from seasoned in AHK. I can come up with basic scripts but this is more advanced and need something to work with besides what I have already come up with. For the record, I have the script launching the game and trainer in proper succession as well as winactivate-ing the game if the script is ran too many times.
User avatar
mikeyww
Posts: 27198
Joined: 09 Sep 2014, 18:38

Re: Troubles with closing additional prog on exit  Topic is solved

08 Jan 2021, 21:20

Code: Select all

dir    = F:\LaunchBox\Games\PC Games\Ghostrunner
tProc := "Trainer.exe", trainer := "ahk_exe " tproc
gProc := "Ghostrunner-Win64-Shipping.exe", gr := "ahk_exe " gProc
WinClose, %trainer%
Sleep, 200
Run, %dir%\%tProc%
WinWait, %trainer%,, 10
If ErrorLevel {
 MsgBox, 48, Error, An error occurred while waiting for trainer window.
 Return
}
WinMinimize
Sleep, 2000
If !WinExist(gr) {
 Run, %dir%\Ghostrunner.exe
 WinWait, %gr%,, 10
 If ErrorLevel {
  MsgBox, 48, Error, An error occurred while waiting for Ghostrunner window.
  Return
 }
} Else WinActivate
WinWaitClose
WinClose, %trainer%
ExitApp
LegzRwheelz
Posts: 56
Joined: 04 Nov 2019, 17:46

Re: Troubles with closing additional prog on exit

08 Jan 2021, 23:55

Thank you so much, I am going to give this a whirl right now and report back. With this, I can kind of tell what is going in the script and make adjustments if needed, I have a feeling with your script, I wont need to though. It appears as though you REALLY know your ahk language. Thank you again and I'll brb

Update: THis script is amazing and I will use it in the future, I am still having the issue of it leaving the trainer running after game exit. The trainer does exit with an alt+f4 key combo (I am thinking that using winactivate on the trainer at game exit and sending alt+f4 to it upon close might work. I am wanting to do something cleaner than that though...Beggars cant be choosers and I am not trying to be picky, it just may be an unusual script, and I know I can do exit it manually, my son though cannot). I am going to compile this when it is working as expected and add it to Launchbox letting RocketLauncher handle the launching, RL will be using using the compiled script as the game's executable. (Kinda irrelevant to this, but I plan on having multiple entries for each game my son likes to play, using this script as the launcher for the trainer+game as well as the game by itself for those whom don't want to use trainers).

Note, The trainer was initially named "Ghostrunner v1.0 Plus 5 Trainer.exe" and I changed it to just "trainer.exe". I tried both variants in the script to see if that made any difference. As I figured, it did not.

I have marked your solution as the right answer or whatever that check mark button says. Running the script as admin makes it run properly. Thank you SO MUCH kind sir and have an exceptionally wonderful weekend!

I edited it a lil bit, but not very much. I added set it to run as a single instance, no tray icon(I can;t stand tray clutter), added a few lines to run it elevated, changed the directory to a relative path so it is can be portable and lastly removed "exitapp" as it closes by itself when the trainer closes.

Code: Select all

#NoTrayIcon
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
#SingleInstance Force


if not (RegExMatch(DllCall("GetCommandLine", "str"), " /restart(?!\S)")) {
	Run *RunAs "%A_AhkPath%" /restart "%A_ScriptFullPath%",, UseErrorLevel
	ExitApp
}

if(A_IsAdmin)

dir    = %A_ScriptDir%  
tProc := "Ghostrunner v1.0 Plus 5 Trainer.exe", trainer := "ahk_exe " tproc
gProc := "Ghostrunner-Win64-Shipping.exe", gr := "ahk_exe " gProc
WinClose, %trainer%
Sleep, 200
Run, %dir%\%tProc%
WinWait, %trainer%,, 10
If ErrorLevel {
 MsgBox, 48, Error, An error occurred while waiting for trainer window.
 Return
}
WinMinimize
Sleep, 2000
If !WinExist(gr) {
 Run, %dir%\Ghostrunner.exe
 WinWait, %gr%,, 10
 If ErrorLevel {
  MsgBox, 48, Error, An error occurred while waiting for Ghostrunner window.
  Return
 }
} Else WinActivate
WinWaitClose
WinClose, %trainer%
This works brilliantly. I gotta tell you, I love how your script is laid out, I plan to use this in the future many times over. It is eloquent, simple, organized and very effective. I can look at this and clearly see what is happening and therefore, you taught me to fish, whether you realize it or not. Thanks to you and a fellow over at the Launchbox forums, You guys got this sorted out.Thank you again.
User avatar
mikeyww
Posts: 27198
Joined: 09 Sep 2014, 18:38

Re: Troubles with closing additional prog on exit

09 Jan 2021, 06:01

Thanks for the kind words. I'm glad it works.

You mentioned that the trainer might remaining running at the end. An alternative to WinClose, %trainer% is the following, which might help.

Code: Select all

Process, Close, %tProc%
Best wishes!
LegzRwheelz
Posts: 56
Joined: 04 Nov 2019, 17:46

Re: Troubles with closing additional prog on exit

10 Jan 2021, 00:44

Firstly, You're welcome, I'm glad they're meaningful to you. I must say, Thank you, yet again, I really like the way Process, Close works and your method here is much cleaner. I love it. I have added it to my script and is the default method for the launcher.

Return to “Gaming Help (v1)”

Who is online

Users browsing this forum: No registered users and 37 guests