Trying to use ahk for a video game and the outcome is different every time

Ask gaming related questions (AHK v1.1 and older)
yesman
Posts: 3
Joined: 19 Feb 2022, 17:20

Trying to use ahk for a video game and the outcome is different every time

19 Feb 2022, 17:30

So the code is working in the background but the game character ends up in a different position every time I use the script despite the starting point being the same

Code: Select all

SendMode, Event
SetKeyDelay, 10000
WinGet, Gameid,List, Game
ControlSend,,{Right Down}, ahk_id %Gameid1%
	Sleep, 10000
ControlSend,,{Right Up}, ahk_id %Gameid1%
	Sleep, 1000
ControlSend,,{Left Down}, ahk_id %Gameid1%
	Sleep, 10000
ControlSend,,{Left Up}, ahk_id %Gameid1%
	Sleep, 1000
Last edited by yesman on 19 Feb 2022, 22:20, edited 1 time in total.
User avatar
mikeyww
Posts: 27163
Joined: 09 Sep 2014, 18:38

Re: Trying to use ahk for a video game and the outcome is different every time

19 Feb 2022, 19:59

There is no key called Leftt. In any case, this may simply have to do with how your game is responding to the script. Perhaps your key is sent down but never sent up.

Examine the KeyHistory to find out what is happening with the keys.

Why do you need to use the hWnd? If your game has multiple windows, then how do you know that you have the right one?

https://autohotkey.com/board/topic/111737-how-to-make-ahk-work-in-most-games-the-basics/
yesman
Posts: 3
Joined: 19 Feb 2022, 17:20

Re: Trying to use ahk for a video game and the outcome is different every time

19 Feb 2022, 20:55

@mikeyww
I just recreated a simplified version of the code and must've made a typo.
and I do have multiple windows in use.
I change the window by changing "ahk_id %Gameid1%" to "ahk_id %Gameid2%" if that is what you are asking.
Anyhow it is working but always ends up in a slightly different position.
User avatar
mikeyww
Posts: 27163
Joined: 09 Sep 2014, 18:38

Re: Trying to use ahk for a video game and the outcome is different every time

19 Feb 2022, 22:53

You can test in Notepad to see what the script itself does. The script does not control the position; the game does that!
yesman
Posts: 3
Joined: 19 Feb 2022, 17:20

Re: Trying to use ahk for a video game and the outcome is different every time

19 Feb 2022, 23:59

@mikeyww
Is there any way to fix this?
Like feeding the code for the game slower if it misses something because of that?
User avatar
mikeyww
Posts: 27163
Joined: 09 Sep 2014, 18:38

Re: Trying to use ahk for a video game and the outcome is different every time

20 Feb 2022, 06:40

The usual way would be to follow your own script, line by line; instead of running it, execute each action manually, including the sleeps. See if the same problem occurs. If so, then you know that the fix is easy: you simply need to revise your code to match a working sequence. If the problem does not occur, then see where the key history might differ from your manual sequence, after you run the script.

When you run this script, I think I would assign a hotkey to your routine, so that you can trigger it with the hotkey.

Some windows do not respond to ControlSend, so another test you can do is use Send instead of ControlSend. Keep your target window active when you do that. This will simplify your testing and will help you understand whether you can develop a set of sequences that work in AHK. ControlSend complicates the picture, so you can eliminate it in your testing.

Rohwedder points out that a long key delay can also be problematic, so I would eliminate that. It is probably a mistake anyway in your script, as you already have a sleep for the same duration.

Code: Select all

#IfWinExist Game
F3::
WinActivate ; Activate the game
SoundBeep, 1500
Send {Right down}
Sleep, 10000
Send {Right up}
Sleep, 1000
Send {Left down}
Sleep, 10000
Send {Left up}
MsgBox, 64, Done, Done!, 1
Return
#IfWinExist
During this test, let the game window remain active until the hotkey routine ends.

Return to “Gaming Help (v1)”

Who is online

Users browsing this forum: No registered users and 33 guests