Check if game is running, if not restart Topic is solved

Ask gaming related questions (AHK v1.1 and older)
Redas
Posts: 8
Joined: 04 Jan 2020, 19:11

Check if game is running, if not restart

12 Jun 2021, 04:15

Hello friends, I need your help, I want script to, check in a loop (infinite loop) that game is running or not, if so, then do some stuff in game (clicks), if not then start game and do some stuff in game (those clicks). I tried so many variations, but none of them doing what I want...What I have

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.

#Persistent
Process, Exist, Game.exe
if (ErrorLevel = 0)
{
Random, rand, 5000, 10000
SetTimer, DO, %rand%
return
DO:
WinWaitActive, Game
Click, 1469, 852 ; start game
Sleep %rand% ;
Click, 793, 201 ; hero upgrade
Sleep %rand% ;
return
}
else
{
Run, C:\Games\Game.exe ;
Sleep, 5000 ;
return
}
[Mod edit: [code][/code] tags added.]
User avatar
mikeyww
Posts: 26851
Joined: 09 Sep 2014, 18:38

Re: Check if game is running, if not restart

12 Jun 2021, 05:20

It looks like you have the opposite of what you want. When ErrorLevel is zero, the game is not running.
Sets ErrorLevel to the Process ID (PID) if a matching process exists, or 0 otherwise.
Redas
Posts: 8
Joined: 04 Jan 2020, 19:11

Re: Check if game is running, if not restart

12 Jun 2021, 14:35

What should I do to get results I want? Thank you.
User avatar
mikeyww
Posts: 26851
Joined: 09 Sep 2014, 18:38

Re: Check if game is running, if not restart

12 Jun 2021, 15:12

Here is one example.

Code: Select all

#Persistent
If !WinExist("ahk_exe Game.exe")
 Run, C:\Games\Game.exe
Start:
Random, rand, 5000, 10000
SetTimer, Do, -%rand%
Return
Do:
WinWaitActive, ahk_exe Game.exe
Click, 1469 852
Sleep, rand
Click, 793 201
Gosub, Start
Return
Redas
Posts: 8
Joined: 04 Jan 2020, 19:11

Re: Check if game is running, if not restart

13 Jun 2021, 11:40

It's not working((( I mean, if game crash (or I close it), it stop doing anything.
If I remove WinWaitActive, ahk_exe Game.exe , then it clicking like in the game...
Script should rerun game if Game.exe not active, but if game crash and Game.exe not active anymore, it carry on clicking...What I have now.

Code: Select all

#Persistent
If !WinExist("ahk_exe Game.exe")
{
Run, C:\Games\Game.exe ;
Sleep, 5000 ;
Click, 155, 890 ; Click to enter game
Sleep, 20000 ;
Click, 958, 559 ; Click to enter game
Sleep, 5000 ;
Click, 1373, 743 ; Click to enter game
}
Start:
Random, rand, 5000, 10000
SetTimer, DO, %rand%
return
DO:
WinWaitActive, ahk_exe Game.exe
Click, 1469, 852 ; Click in game
Sleep %rand% ;
Click, 793, 201 ; Click in game
Sleep %rand% ;
Gosub, Start
return
[Mod edit: [code][/code] tags added.]
Last edited by gregster on 13 Jun 2021, 12:42, edited 1 time in total.
Reason: Please use [code] tags. Thank you!
User avatar
mikeyww
Posts: 26851
Joined: 09 Sep 2014, 18:38

Re: Check if game is running, if not restart

13 Jun 2021, 13:27

Here is an example of a loop. Adjust according to what you need.

Code: Select all

#SingleInstance Force
WinActivate, % wTitle := "ahk_exe " app := "C:\Games\Game.exe"
Loop {
 If !WinExist(wTitle)
  Run, %app%
 WinWaitActive, %wTitle%
 Gosub, Start
 SoundBeep, 1500
 WinWaitNotActive
 SetTimer, Do, Off
 SoundBeep, 1000
}
Start:
Random, rand, 5000, 10000
SetTimer, Do, -%rand%
Return
Do:
If WinActive(wTitle)
 Click, 1469 852
If WinActive(wTitle)
 Sleep, rand
If WinActive(wTitle)
 Click, 793 201
If WinActive(wTitle)
 Gosub, Start
Return
Redas
Posts: 8
Joined: 04 Jan 2020, 19:11

Re: Check if game is running, if not restart

14 Jun 2021, 06:19

It's not working...If I close game, it do not rerun it + it never "Do:". What I have now. Stop working after this line - Click, 1373, 743 ; Run play. And if I close game, doesn't restart.

PS - To start the game, I need to start launcher first, and then click in laucher "Play" (let say it's Steam), so just to run "Game.exe" I need few lines and clicks.

Code: Select all

#SingleInstance Force
WinActivate, % wTitle := "ahk_exe " app := "B:\Game\Game.exe"
Loop {
 If !WinExist(wTitle)
{
Run, B:\Launcher\Launcher.exe ;
Sleep, 5000 ;
Click, 155, 890 ; Run game from Launcher
Sleep, 20000 ;
Click, 958, 559 ; Run modes (in game already)
Sleep, 5000 ;
Click, 1373, 743 ; Run play (in game)
}
 WinWaitActive, %wTitle%
 Gosub, Start
 SoundBeep, 1500
 WinWaitNotActive
 SetTimer, Do, Off
 SoundBeep, 1000
}
Start:
Random, rand, 5000, 10000
SetTimer, Do, %rand%
Return
Do:
If WinActive(wTitle)
 Click, 1469 852 ; in game click
If WinActive(wTitle)
 Sleep, rand
If WinActive(wTitle)
 Click, 793 201 ; in game click
If WinActive(wTitle)
 Gosub, Start
Return
[Mod edit: [code][/code] tags added.]
Last edited by gregster on 14 Jun 2021, 06:24, edited 1 time in total.
Reason: Please use [code] tags. Thank you!
Redas
Posts: 8
Joined: 04 Jan 2020, 19:11

Re: Check if game is running, if not restart

14 Jun 2021, 06:59

ONLY problem my script has, it doesn't restart if game crash, otherwise it works perfecto. Tried to check with "Process, Exist, Game.exe", still not working, it just carry on clicking in "DO:".

Code: Select all

#Persistent
If !WinExist("ahk_exe Game.exe")
{
Run, C:\Launcher\Launcher.exe ;
Sleep, 5000 ;
Click, 155, 890 ; Run Launcher
Sleep, 20000 ;
Click, 958, 559 ; Run modes
Sleep, 5000 ;
Click, 1373, 743 ; Run Play
}
Start:
Random, rand, 5000, 10000
SetTimer, DO, %rand%
return
DO:
WinWaitActive, ahk_exe Game.exe
Click, 1469, 852 ; start game
Sleep %rand% ;
Click, 793, 201 ; upgrade
Sleep %rand% ;
Gosub, Start
return
[Mod edit: [code][/code] tags added.]
Last edited by gregster on 14 Jun 2021, 09:06, edited 1 time in total.
Reason: Please use [code] tags. Thank you!
User avatar
mikeyww
Posts: 26851
Joined: 09 Sep 2014, 18:38

Re: Check if game is running, if not restart

14 Jun 2021, 07:21

The window title in my script might not be the right one. If you run Window Spy, it will show you. In any case, if you want to work with your version, then debug it by looking at what is happening, line by line. Test by shortening the script so that it has only the essential lines. Your current script would not restart anything because you have no run commands inside a loop of any kind.
Redas
Posts: 8
Joined: 04 Jan 2020, 19:11

Re: Check if game is running, if not restart  Topic is solved

15 Jun 2021, 04:15

I solved problem by my self, thank you. Script became much more complicated, but it's working as it should.

Return to “Gaming Help (v1)”

Who is online

Users browsing this forum: No registered users and 52 guests