 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
Guest
|
Posted: Sun Oct 15, 2006 6:39 am Post subject: Example script wont run did I write it out wrong. |
|
|
I saw a handy script posted in the BF script topic but I have tried to use it with out much luck. The script was the code I have added the header but when I run it in BF2 1.4 under windows XP SP2 and press F nothign happens. If any one could give me a few tips or tell me what I am doing wrong that would be great.
| Code: |
;
; AutoHotkey Version: 1.x
; Language: English
; Platform: Win9x/NT
;
;
; Only run this script when bf2.exe is running in the foreground
;
#SingleInstance force
#InstallKeybdHook
#InstallMouseHook
SetTimer, KeepRunning
return
KeepRunning:
; Get the process name of the active window (i.e. Notepad.exe)
WinGet, szProcessName, ProcessName, A
if szProcessName = bf2.exe
{
Suspend, off
}
else
{
Suspend, on
}
return
;
; Disable the Window's keys so they don't switch to desktop while in-game
;
$LWin:: ; Left Windows Button
$RWin:: ; Right Windows Button
; Do nothing
return
$^CapsLock::
ExitApp
return
SetKeyDelay, 50, 50 ; NB: Once "WeaponSelect()" added this was needed!!
v_LastUsedWeapon = 3 ; Initialise as Primary Weapon 3
v_LastStoredWeapon = 3 ; Initialise as Primary Weapon 3
$f::Goto, QuickSwitch
$1::WeaponSelect(1)
$2::WeaponSelect(2)
$3::WeaponSelect(3)
$4::WeaponSelect(4)
$5::WeaponSelect(5)
$6::WeaponSelect(6)
KeyPress(v_KeyName)
{
Send, {%v_KeyName% Down}
Sleep %KeySleep1%
Send, {%v_KeyName% Up}
If( v_KeyName > 0 and v_KeyName < 7) ; QuickSwitch: Check if weapon, between 1-8, should it be 1-6?
; NB: Above 'If' only needed if Quickswitch desired.
{
v_ChosenWeapon = %v_KeyName%
QuickSwitchVar(ByRef v_ChosenWeapon)
}
}
QuickSwitch:
KeyPress(ByVar v_LastUsedWeapon) ; QuickSwitch: "v_LastUsedWeapon" is declared globally and stored by "QuickSwitchVar()" function
return
QuickSwitchVar(v_ChosenWeapon)
; 1.Edit Store previous weapon, 2. Placeholder variable for current weapon so next use of function can determine previous weapon
{
Global v_LastUsedWeapon
Global v_LastStoredWeapon
v_LastUsedWeapon = %v_LastStoredWeapon% ; QuickSwitch: Previous run stored source for this variable. Used to record last used Weapon and used in QuickSwitch usage
v_LastStoredWeapon = %v_ChosenWeapon% ; QuickSwitch: Stored so the following call of function can determine Previous Weapon
}
WeaponSelect(v_ChosenWeapon)
; Select weapon 1 - 6 %v_ChosenWeapon%
{
If( v_ChosenWeapon > 0 and v_ChosenWeapon < 7)
{
Global v_LastStoredWeapon ; QuickSwitch: Declare this to to access it and determine previous weapon
If( v_ChosenWeapon <> v_LastStoredWeapon) ; QuickSwitch: If current weapon wasn't pressed last time then ...
QuickSwitchVar(ByRef v_ChosenWeapon) ; ... update variables for weapon history.
Send, {%v_ChosenWeapon% Down}
Sleep %KeySleep1%
Send, {%v_ChosenWeapon% Up}
}
Else
Send, @huh????%v_ChosenWeapon%
Sleep 50
}
|
|
|
| Back to top |
|
 |
nick
Joined: 24 Aug 2005 Posts: 549 Location: Berlin / Germany
|
Posted: Sun Oct 15, 2006 7:29 am Post subject: |
|
|
There are some curious statements:
| Code: | If( v_KeyName > 0 and v_KeyName < 7) ; QuickSwitch: Check if weapon, between 1-8, should it be 1-6?
; NB: Above 'If' only needed if Quickswitch desired.
{
v_ChosenWeapon = %v_KeyName%
QuickSwitchVar(ByRef v_ChosenWeapon)
}
QuickSwitch:
KeyPress(ByVar v_LastUsedWeapon)
return
...
QuickSwitchVar(ByRef v_ChosenWeapon)
...
Sleep %KeySleep1% |
ByRef is used in function declarations, not in function calls.
Delete ByRef/Byvar and assign some value to KeySleep1. May help!
P.S.: Use a nick or beware of the BoBo!!! _________________ nick  |
|
| Back to top |
|
 |
Jackthehat
Joined: 15 Oct 2006 Posts: 10
|
Posted: Sun Oct 15, 2006 7:44 am Post subject: |
|
|
Opps sorry BoBo
will try and give it a shot is there a way to debug code with out running BF2 could I change BF2.exe and put notepad.exe or is there a nice tool to capture and see what is being pressed eg if it pressed Shift and Crtl you would not see this in notepad |
|
| Back to top |
|
 |
nick
Joined: 24 Aug 2005 Posts: 549 Location: Berlin / Germany
|
Posted: Sun Oct 15, 2006 8:18 am Post subject: |
|
|
I tried your code with if szProcessName = PSPad.exe
For "debugging" I use ListVars, Pause and MsgBox.  _________________ nick  |
|
| Back to top |
|
 |
Lanser
Joined: 24 Mar 2006 Posts: 20
|
Posted: Sun Oct 15, 2006 1:14 pm Post subject: |
|
|
| Jackthehat wrote: | Opps sorry BoBo
will try and give it a shot is there a way to debug code with out running BF2 could I change BF2.exe and put notepad.exe or is there a nice tool to capture and see what is being pressed eg if it pressed Shift and Crtl you would not see this in notepad |
I replied to your PM, thanks for expressing an interest in the idea. The original code I posted might have been missing some variables or such when I copied and pasted so I went back to my original and extracted all the QuickSwitch related pieces and tried them out in a Standalone script. Tested it in Notepad first and then fired up BF2 in singleplayer and the QuickSwitch is working ok so hope you find the below useful.
| Code: |
;GameName = notepad.exe ; use when testing in notepad
GameName = bf2.exe ; Set exe name here
KeySleep1 = 100 ; Ideal Sleep time for BF2 commands (check?, SetKeyDelay influence?)
v_InTextChatOn := False ; Used for pausing running of Script
v_LastUsedWeapon = 3 ; Initialise as Primary Weapon 3
v_LastStoredWeapon = 3 ; Initialise as Primary Weapon 3
#Persistent
#NoEnv ; prevents empty variables from being looked up
#SingleInstance force
#InstallKeybdHook
#InstallMouseHook
SetKeyDelay, 75, 75 ; NB: Once "WeaponSelect()" added this was needed!!
SetTimer, KeepRunning ; Only run this script when {BF2.exe} is running in the foreground
Return
;;;; Timer related ;;;;
KeepRunning:
; Get the process name of the active window (i.e. Notepad.exe)
; Only run this script when Battlefield 2 {BF2.exe} is running in the foreground.
WinGet, szProcessName, ProcessName, A
If szProcessName = %GameName% ; Determine if window Active, enable hotkeys
{
; If running check to determine whether in chat mode.
If (v_InTextChatOn == False) ; Text chat isn't active so allow hotkeys
{
Suspend, off
}
Else if (v_InTextChatOn == True) ; Text chat is active so disable hotkeys
{
Suspend, on
}
}
Else ; Else window inactive, disable hotkeys
{
Suspend, on
}
Return
; --------------------
; Quickswitch related.
$1::WeaponSelect(1)
$2::WeaponSelect(2)
$3::WeaponSelect(3)
$4::WeaponSelect(4)
$5::WeaponSelect(5)
$6::WeaponSelect(6)
$f::Goto, QuickSwitch
QuickSwitch:
KeyPress(ByVar v_LastUsedWeapon) ; QuickSwitch: "v_LastUsedWeapon" is declared globally and stored by "QuickSwitchVar()" function
return
QuickSwitchVar(v_ChosenWeapon)
; 1.Edit Store previous weapon, 2. Placeholder variable for current weapon so next use of function can determine previous weapon
{
Global v_LastUsedWeapon
Global v_LastStoredWeapon
v_LastUsedWeapon = %v_LastStoredWeapon% ; QuickSwitch: Previous run stored source for this variable. Used to record last used Weapon and used in QuickSwitch usage
v_LastStoredWeapon = %v_ChosenWeapon% ; QuickSwitch: Stored so the following call of function can determine Previous Weapon
}
WeaponSelect(v_ChosenWeapon)
; Select weapon 1 - 6 %v_ChosenWeapon%
{
If( v_ChosenWeapon > 0 and v_ChosenWeapon < 7)
{
Global v_LastStoredWeapon ; QuickSwitch: Declare this to to access it and determine previous weapon
If( v_ChosenWeapon <> v_LastStoredWeapon) ; QuickSwitch: If current weapon wasn't pressed last time then ...
QuickSwitchVar(ByRef v_ChosenWeapon) ; ... update variables for weapon history.
Send, {%v_ChosenWeapon% Down}
Sleep %KeySleep1%
Send, {%v_ChosenWeapon% Up}
}
Else
Send, @huh????%v_ChosenWeapon%
Sleep 50
}
KeyPress(v_KeyName)
{
Send, {%v_KeyName% Down}
Sleep %KeySleep1%
Send, {%v_KeyName% Up}
If( v_KeyName > 0 and v_KeyName < 7) ; QuickSwitch: Check if weapon, between 1-8, should it be 1-6 as NightVision and gas Mask not really need in the switch?
; NB: Above 'If' only needed if Quickswitch desired.
{
v_ChosenWeapon = %v_KeyName%
QuickSwitchVar(ByRef v_ChosenWeapon)
}
}
; --------------------
|
When I originally thought of I thought it would be quiet simple but of course as I delved in a little deeper it became a little more complex, after some trial and error in Notepad the code evolved and the above appears to work fine. Someone a little more expereinced with coding might be able to simplify it further. Anyway hope someone finds it useful. |
|
| Back to top |
|
 |
Jackthehat
Joined: 15 Oct 2006 Posts: 10
|
Posted: Tue Oct 17, 2006 10:25 am Post subject: |
|
|
| Works perfect thank you very much do you think you could add this script to the BF2 offical thread as its a very handy script with some quite advanced options. |
|
| Back to top |
|
 |
Guest
|
Posted: Tue Oct 17, 2006 10:34 am Post subject: |
|
|
| Jackthehat wrote: | | Works perfect thank you very much do you think you could add this script to the BF2 offical thread as its a very handy script with some quite advanced options. |
When I posted this here I edited my code in the original posting so anyone interested in it should be good to go. Glad you find it useful. |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|