Certainly! Improving your AutoHotkey (AHK) script involves enhancing its reliability, flexibility, and maintainability. Below is an improved version of your script with the following enhancements:
- Modular Structure: Organized code into functions for better readability and maintenance.
- Dynamic Menu Navigation: Attempts to navigate menus dynamically using image detection. (Note: You'll need to provide screenshots for specific menu items for image-based navigation.)
- Error Handling: Checks if windows and menus are active before performing actions.
- Configurability: Added variables to easily adjust settings like the number of crafting iterations and delays.
- Logging: Provides feedback in the console for easier troubleshooting.
Improved AutoHotkey Script for Fallout 76 Crafting
#Persistent
#SingleInstance, Force
#NoEnv
SendMode, Input ; Faster and more reliable
SetTitleMatchMode, 2
DetectHiddenWindows, On
; === Configuration ===
craftIterations := 10 ; Number of times to craft
actionKey := "e" ; Key to open crafting menu
navigateDelay := 500 ; Delay between navigation steps (in ms)
confirmKey := "{Enter}" ; Key to confirm selections
menuImagePath := "menu.png" ; Path to menu image for ImageSearch (optional)
sectionImagePath := "section.png"; Path to section image for ImageSearch (optional)
craftItemDowns := 2 ; Number of times to press Down to select the crafting item
craftAmountConfirm := "{Enter}" ; Key to confirm crafting amount
; === Hotkeys ===
; Start Crafting (F1)
F1::
Log("Starting crafting process...")
if (!ActivateGameWindow("Fallout 76")) {
MsgBox, 16, Error, Fallout 76 window not found!
return
}
Sleep, 500 ; Pause before starting
Loop, %craftIterations%
{
Log("Crafting iteration: " . A_Index)
if (!OpenCraftingMenu()) {
Log("Failed to open crafting menu.")
Break
}
if (!NavigateToCraftingStation()) {
Log("Failed to navigate to crafting station.")
Break
}
if (!SelectCraftingItem()) {
Log("Failed to select crafting item.")
Break
}
if (!ConfirmCrafting()) {
Log("Failed to confirm crafting.")
Break
}
Sleep, navigateDelay ; Wait before next iteration
}
MsgBox, 64, Complete, Finished crafting %craftIterations% Ignition Cores!
Log("Crafting process completed.")
return
; Stop Script (F2)
F2::
Log("Exiting script...")
ExitApp
return
; === Functions ===
; Activate the game window
ActivateGameWindow(title) {
WinActivate, %title%
WinWaitActive, %title%,, 5
if (WinActive("ahk_exe Fallout76.exe")) { ; Adjust the executable name if different
Log("Activated window: " . title)
return true
}
return false
}
; Open the crafting menu
OpenCraftingMenu() {
Log("Opening crafting menu...")
Send, %actionKey%
Sleep, navigateDelay
; Optional: Use ImageSearch to verify menu opened
; if (!VerifyMenuOpened()) {
; return false
; }
return true
}
; Navigate to the appropriate crafting station/menu section
NavigateToCraftingStation() {
Log("Navigating to crafting station/section...")
; Example key navigation:
Send, {Down %craftItemDowns%}
Sleep, navigateDelay
Send, %confirmKey%
Sleep, navigateDelay
; Optional: Use ImageSearch to verify navigation
; if (!VerifySectionOpened()) {
; return false
; }
return true
}
; Select the specific crafting item (e.g., Ignition Core)
SelectCraftingItem() {
Log("Selecting crafting item...")
Send, {Down} ; Adjust if needed based on menu layout
Sleep, navigateDelay
Send, %confirmKey%
Sleep, navigateDelay
return true
}
; Confirm crafting action
ConfirmCrafting() {
Log("Confirming crafting...")
Send, %craftAmountConfirm%
Sleep, navigateDelay
return true
}
; Optional: Verify crafting menu opened using ImageSearch
; VerifyMenuOpened() {
; ImageSearch, foundX, foundY, 0, 0, A_ScreenWidth, A_ScreenHeight, %menuImagePath%
; if (ErrorLevel = 0) {
; Log("Menu detected at " . foundX . "," . foundY)
; return true
; }
; Log("Menu image not found.")
; return false
; }
; Optional: Verify crafting section opened using ImageSearch
; VerifySectionOpened() {
; ImageSearch, foundX, foundY, 0, 0, A_ScreenWidth, A_ScreenHeight, %sectionImagePath%
; if (ErrorLevel = 0) {
; Log("Section detected at " . foundX . "," . foundY)
; return true
; }
; Log("Section image not found.")
; return false
; }
; Logging function
Log(message) {
; Uncomment the next line to enable console logging
; OutputDebug, %message%
ToolTip, %message%
Sleep, 1000
ToolTip
}
Explanation of Improvements
-
Modular Functions:
ActivateGameWindow: Ensures that the Fallout 76 window is active before proceeding.
OpenCraftingMenu: Handles opening the crafting menu.
NavigateToCraftingStation: Manages navigation within the crafting menu.
SelectCraftingItem: Selects the specific item to craft.
ConfirmCrafting: Confirms the crafting action.
Log: Provides real-time feedback using tooltips (can be switched to debug output).
-
Configurability:
- Variables at the top allow easy adjustments for the number of iterations, keys used, delays, and file paths for images if you choose to implement image-based verification.
-
Dynamic Menu Navigation:
- The script is structured to allow easy insertion of
ImageSearch functions to verify if menus or sections have been successfully opened. Uncomment and set the menuImagePath and sectionImagePath with appropriate screenshots to use this feature.
-
Error Handling:
- Each function returns
false if it fails to perform its task, which stops the loop and provides an error message. This prevents the script from continuing in an unexpected state.
-
Logging and Feedback:
- Uses
ToolTip to display messages on the screen. For more detailed logs, you can uncomment the OutputDebug line and use a tool like DebugView to monitor logs.
-
Adjustable Delays:
- Centralized delay configuration (
navigateDelay) makes it easier to tweak timing based on your system's performance and game responsiveness.
-
Safety Measures:
#SingleInstance, Force prevents multiple instances of the script from running simultaneously, which could cause unexpected behavior.
#NoEnv and SendMode, Input enhance script performance and reliability.
Additional Recommendations
-
Image-Based Navigation:
- For more reliable navigation, especially if the game's UI changes, consider using
ImageSearch with screenshots of key UI elements. This allows the script to detect if specific menus or buttons are present before attempting to interact with them.
-
Window Title and Executable:
- Ensure that the window title
"Fallout 76" matches exactly how it appears during gameplay. You might need to adjust this if the window title changes (e.g., with different modes or overlays).
- Verify the executable name (
Fallout76.exe) matches the actual game executable. You can use tools like Window Spy (included with AHK) to check.
-
Testing and Calibration:
- Test the script with smaller
craftIterations initially to ensure it behaves as expected.
- Adjust
navigateDelay based on your game's responsiveness to prevent skipped actions.
-
Anti-Cheat Considerations:
- Be aware that using scripts to automate in-game actions might be against the game's terms of service and could potentially lead to penalties. Use such scripts responsibly and at your own risk.
-
Enhancing Reliability:
- Consider adding more sophisticated checks, such as verifying specific pixel colors or using OCR to read on-screen text, to ensure that each step has been successfully completed before moving to the next one.
By implementing these improvements, your script should become more robust, easier to maintain, and better suited to handle changes or unexpected behavior within the game. Always remember to test thoroughly and adjust configurations as needed based on your specific environment and game version.