Running Script in Background

Ask gaming related questions (AHK v1.1 and older)
Toyia4
Posts: 3
Joined: 26 Jul 2021, 21:37

Running Script in Background

Post by Toyia4 » 26 Jul 2021, 21:46

Hello all.

I just wrote my first code and it works wonderfully in the foreground. I was wondering how would I be able to have it run in the background so that I would still be able to do other things on my computer. Below is the code I created.

Code: Select all

#NoEnv
SetWorkingDir %A_ScriptDir%
CoordMode, Mouse, Window
SendMode Input
#SingleInstance Force
SetTitleMatchMode, 2
#WinActivateForce
SetControlDelay, 1
SetWinDelay, 0
SetKeyDelay, -1
SetMouseDelay, 1
SetBatchLines, -1


; Open a lobby for game with mode first.

Loop
{
    WinActivate, StarCraft II ahk_class StarCraft II
	d := 0

;Sleep, 15000

    Click, 208, 876, 0 ; start game
    Sleep, 15
    Click, 208, 876 Left, Down
    Sleep, 15
    Click, 208, 876 Left, Up
    Sleep, 40000 ; wait for game to load

	Click, 480, 65, 0 ; Hide hero list
    Sleep, 15
    Click, 480, 65 Left, Down
    Sleep, 15
    Click, 480, 65 Left, Up

	Send {Enter} ; selects Toyia Hero
	Sleep 100
	Send {-}
	Sleep 100
	Send {T}
	Sleep 100
	Send {O}
	Sleep 100
	Send {Y}
	Sleep 100
	Send {I}
	Sleep 100
	Send {A}
	Sleep 100
	Send {Enter}
	Sleep 2000
    Send {F2}
    Sleep, 150

	;Spam Extinction
	While d < 80
	{
	Send {W}
	d += 1
	Sleep 5000
	}


    Send {F10} ; open game menu
    Sleep, 50
    Click, 960, 387, 0 ; quit game
    Sleep, 15
    Click, 960, 387 Left, Down
    Sleep, 15
    Click, 960, 387 Left, Up
    Sleep, 8000
    Click, 422, 869, 0 ; play again
    Sleep, 15
    Click, 422, 869 Left, Down
    Sleep, 15
    Click, 422, 869 Left, Up
    Sleep, 13000 ; wait to restart
}
Any help would be appreciated.
Last edited by joedf on 26 Jul 2021, 21:54, edited 1 time in total.
Reason: added [code] tags

User avatar
mikeyww
Posts: 26599
Joined: 09 Sep 2014, 18:38

Re: Running Script in Background

Post by mikeyww » 26 Jul 2021, 22:39

Welcome. I would have a look at ControlClick and ControlSend. They do not work in all windows, but it may be worth a try.

Toyia4
Posts: 3
Joined: 26 Jul 2021, 21:37

Re: Running Script in Background

Post by Toyia4 » 26 Jul 2021, 22:52

mikeyww wrote:
26 Jul 2021, 22:39
Welcome. I would have a look at ControlClick and ControlSend. They do not work in all windows, but it may be worth a try.
So I did try adding those commands in replace of where I needed them at and I got the error of Parameter # 4 pointing to the first 4 lines. Im not to familiar with all of the variables and commands while using AutoHotkey.

User avatar
mikeyww
Posts: 26599
Joined: 09 Sep 2014, 18:38

Re: Running Script in Background

Post by mikeyww » 27 Jul 2021, 08:00

If you post your revision, readers can provide feedback about it.

Toyia4
Posts: 3
Joined: 26 Jul 2021, 21:37

Re: Running Script in Background

Post by Toyia4 » 27 Jul 2021, 10:12

@mikeyww

Here is the updated script. I was reading a few different post and it seems to come up with something similar to what I changed below.

Code: Select all

F6::
WinGet, programid,List, StarCraft II <------ programid = "program name"+id at the end
Loop
{
	d := 0

Sleep, 10000

    ControlClick, 208, 876, 0 ; start game
    Sleep, 15
    ControlClick, 208, 876 Left, Down
    Sleep, 15
    ControlClick, 208, 876 Left, Up
    Sleep, 40000 ; wait for game to load

	Click, 480, 65, 0 ; Hide hero list
    Sleep, 15
    ControlClick, 480, 65 Left, Down
    Sleep, 15
    ControlClick, 480, 65 Left, Up

	ControlSend {Enter}, ahk_id %programid1% ; selects Toyia Hero
	Sleep 100
	ControlSend {-}, ahk_id %programid1%
	Sleep 100
	ControlSend {T}, ahk_id %programid1%
	Sleep 100
	ControlSend {O}, ahk_id %programid1%
	Sleep 100
	ControlSend {Y}, ahk_id %programid1%
	Sleep 100
	ControlSend {I}, ahk_id %programid1%
	Sleep 100
	ControlSend {A}, ahk_id %programid1%
	Sleep 100
	ControlSend {Enter}, ahk_id %programid1%
	Sleep 2000
    ControlSend {F2}, ahk_id %programid1%
    Sleep, 150

	;Spam Extinction
	While d < 81
	{
	ControlSend {W}, ahk_id %programid1%
	d += 1
	Sleep 5000
	}



    ControlClick, 1172, 535, 0 ; quit game
    Sleep, 100
    ControlClick, 1172, 535 Left, Down
    Sleep, 30
    ControlClick, 1172, 535 Left, Up
    Sleep, 8000
    ControlClick, 422, 869, 0 ; play again
    Sleep, 15
    ControlClick, 422, 869 Left, Down
    Sleep, 15
    ControlClick, 422, 869 Left, Up
    Sleep, 13000 ; wait to restart
}
F2::pause
[Mod edit: [code][/code] tags added.]
Last edited by gregster on 27 Jul 2021, 10:14, edited 1 time in total.
Reason: Please use [code] tags. Thank you!

User avatar
mikeyww
Posts: 26599
Joined: 09 Sep 2014, 18:38

Re: Running Script in Background

Post by mikeyww » 27 Jul 2021, 11:53

Examples of ControlClick are at the bottom of the documentation page. Using the defined syntax is important.

Post Reply

Return to “Gaming Help (v1)”