Help ahk search color and generate parametre.ini

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Spelth
Posts: 5
Joined: 09 May 2024, 04:47
Contact:

Help ahk search color and generate parametre.ini

09 May 2024, 04:54

Hello,

I need help please,
My script starts well but closes each time I click on save and does not reuse the settings.ini. And above all, it does not search for the color on the screen.

Thank you all.

Code: Select all

#SingleInstance Force

CoordMode, Pixel, Screen

Gui, Add, Text, x10 y10 w150 h20, Welcome to the matrix
Gui, Add, Text, x10 y30 w150 h20, Screen resolution:
Gui, Add, Text, x10 y60 w150 h20, Width:
Gui, Add, Edit, x170 y60 w50 h20 vScreenWidth, 2560
Gui, Add, Text, x10 y90 w150 h20, Height:
Gui, Add, Edit, x170 y90 w50 h20 vScreenHeight, 1440

Gui, Add, Text, x10 y120 w150 h20, Color to search (RGB format):
Gui, Add, Edit, x10 y150 w100 h20 vPixelColor, FF0000

Gui, Add, Text, x10 y180 w150 h20, Color tolerance:
Gui, Add, Edit, x10 y210 w100 h20 vTolerance, 20

Gui, Add, Text, x10 y240 w150 h20, Search area:
Gui, Add, Text, x10 y270 w150 h20, Start X:
Gui, Add, Edit, x170 y270 w50 h20 vStartX, 0
Gui, Add, Text, x10 y300 w150 h20, Start Y:
Gui, Add, Edit, x170 y300 w50 h20 vStartY, 0
Gui, Add, Text, x10 y330 w150 h20, End X:
Gui, Add, Edit, x170 y330 w50 h20 vEndX, 50
Gui, Add, Text, x10 y360 w150 h20, End Y:
Gui, Add, Edit, x170 y360 w50 h20 vEndY, 50

Gui, Add, Text, x10 y390 w150 h20, Click type:
Gui, Add, Checkbox, x170 y390 w100 h20 vLeftClick, Left click
Gui, Add, Checkbox, x170 y420 w100 h20 vRightClick, Right click

Gui, Add, Button, x10 y450 w100 h30 gSaveSettings, Save
Gui, Add, Button, x120 y450 w100 h30 gStartScript, Start

Gui, Add, Text, x10 y480 w150 h20, Spelth

Gui, Show, w250 h520, Configuration settings
return

SaveSettings:
    Gui, Submit
    IniWrite, % ScreenWidth, Settings.ini, Config, ScreenWidth
    IniWrite, % ScreenHeight, Settings.ini, Config, ScreenHeight
    IniWrite, % PixelColor, Settings.ini, Config, PixelColor
    IniWrite, % Tolerance, Settings.ini, Config, Tolerance
    IniWrite, % StartX, Settings.ini, Config, StartX
    IniWrite, % StartY, Settings.ini, Config, StartY
    IniWrite, % EndX, Settings.ini, Config, EndX
    IniWrite, % EndY, Settings.ini, Config, EndY
    IniWrite, % LeftClick, Settings.ini, Config, LeftClick
    IniWrite, % RightClick, Settings.ini, Config, RightClick
    MsgBox, 64, Save, Settings have been saved successfully.
return

StartScript:
    if !FileExist("Settings.ini")
    {
        MsgBox, 48, Error, Settings file not found. Please configure the settings first.
        return
    }

    IniRead, ScreenWidth, Settings.ini, Config, ScreenWidth
    IniRead, ScreenHeight, Settings.ini, Config, ScreenHeight
    IniRead, PixelColor, Settings.ini, Config, PixelColor
    IniRead, Tolerance, Settings.ini, Config, Tolerance
    IniRead, StartX, Settings.ini, Config, StartX
    IniRead, StartY, Settings.ini, Config, StartY
    IniRead, EndX, Settings.ini, Config, EndX
    IniRead, EndY, Settings.ini, Config, EndY
    IniRead, LeftClick, Settings.ini, Config, LeftClick
    IniRead, RightClick, Settings.ini, Config, RightClick

    CrossColor := PixelColor
    Width := EndX - StartX
    Height := EndY - StartY

    ; Initialize pause state of the script
    Paused := false

    ; Initialize variables for mouse movement
    X_Start := 0
    Y_Start := 0
    Distance := 10 ; 1 cm on a 2k screen (2560 pixels wide)
    MoveMouse := false

    ; Function to search for the enemy's head
    SearchEnemyHead() {
        X_Start := StartX
        Y_Start := StartY
        X_End := EndX
        Y_End := EndY
        
        Loop, % Width {
            Loop, % Height {
                PixelGetColor, PixelColor, % X_Start+A_Index-1, % Y_Start+A_Index-1
                Difference := ColorDifference(CrossColor, PixelColor)
                if (Difference <= Tolerance) {
                    X_Target := X_Start + (Width // 2)
                    Y_Target := Y_Start + (Height // 2)
                    MouseMove, % X_Target, % Y_Target
                    return
                }
            }
        }
    }

    ; Function for mouse movement
    MoveMouse() {
        MouseGetPos, X_Current, Y_Current
        if (MoveMouse) {
            New_X := X_Start + Distance
            New_Y := Y_Start + Distance
            MouseMove, % New_X, % New_Y
        }
    }

    ; Calculate color difference between two RGB colors
    ColorDifference(Color1, Color2) {
        R1 := (Color1 >> 16) & 0xFF, G1 := (Color1 >> 8) & 0xFF, B1 := Color1 & 0xFF
        R2 := (Color2 >> 16) & 0xFF, G2 := (Color2 >> 8) & 0xFF, B2 := Color2 & 0xFF
        return sqrt((R2 - R1) ** 2 + (G2 - G1) ** 2 + (B2 - B1) ** 2)
    }

    ; Watch key to pause or resume the script
    End:: 
        Paused := !Paused
        if (!Paused) {
            SoundBeep, 500, 100
        }
    return

    ; Loop to continuously monitor left mouse button click
    While (true) {
        if (Paused)
            continue
        
        SearchEnemyHead()
        
        if (GetKeyState("LButton", "P") && (LeftClick || RightClick)) {
            MouseGetPos, X_Start, Y_Start
            MoveMouse := true
            Sleep, 100
        }
        else {
            MoveMouse := false
        }
    }

[Mod action: Moved topic to the v1 section since this is v1 code. The main section is for v2.]
User avatar
mikeyww
Posts: 27169
Joined: 09 Sep 2014, 18:38

Re: Help ahk search color and generate parametre.ini

09 May 2024, 05:52

Welcome to this AutoHotkey forum!

Below are a few tips to get you started with your debugging process.

The length and complexity of your script might slow your troubleshooting, so I would think about shortening and simplifying, and testing specific parts of the script systematically.

To avoid hiding the GUI, you can use the NoHide option. See the documentation about it.

For clarity, you might consider moving your functions to the bottom of the script. This can help you follow your script to see what it is doing.

Unlabeled code following a Return command is unreachable and will never execute. Delete your End hotkey and subroutine for now. After your script works, you can decide where to put it. You would not insert it into your other labeled subroutine.

You have variables such as ScreenWidth, but you never actually refer to them, so they will have no impact on how your script works.

I recommend removing your various loops while you debug your script. Get a single iteration working. After it works, you can add Loop, While, etc.

You can test how to use an INI file in a separate script that does nothing else. I would forget about your INI in this script until you have your main actions working. Add the INI later.

This forum has a dedicated gaming board that may be a good fit for this issue and script, as well as solutions to help you.

If you are new to AHK, I recommend using its current version, which is v2, instead of this older deprecated version that is no longer developed.
Spelth
Posts: 5
Joined: 09 May 2024, 04:47
Contact:

Re: Help ahk search color and generate parametre.ini

09 May 2024, 06:15

Okay, thank you for your help.

I'm using ChatGPT to help me make a script, which is why I don't think everything is very good :lol: :| Can you help me make it work?
User avatar
boiler
Posts: 17189
Joined: 21 Dec 2014, 02:44

Re: Help ahk search color and generate parametre.ini

09 May 2024, 06:22

This is why asking for help with ChatGPT-generated code is against the forum rules. I’ll leave it in place since you already received a response, but please don’t post code written by AI in the future, at least until they get better at it and the forum rules change accordingly.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], denis_q2 and 100 guests