Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm trying to make a script that periodically reads the pixels at various coordinates and compares them to a given hex color based on a given file out.txt. The format is x-cord y-cord color.

Loop
{
    if (GetKeyState("Space"))
    {
        Loop, Read, out.txt
        {
            tgt := StrSplit(%A_LoopReadLine%,%A_Space%)
            PixelGetColor hue, tgt[0], tgt[1]
            if (%hue% != tgt[2])
            {
                Click tgt[0], tgt[1]
            }
        }
    }
        Sleep 10
}

produces this error, screenshot because you cant select the text:

error

It says the variable name contains an illegal character, followed by what should be a line in the file, where I'm trying to assign that value to an array so I'm not sure what I'm doing wrong. I reviewed ahk syntax in all the parts that seem relevant and nothing pops out at me. It looks like just setting a variable. I can't find the error here.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
204 views
Welcome To Ask or Share your Answers For Others

1 Answer

You're basically just confused about the deprecated legacy syntax and the modern expression syntax.
Referring to a variable by wrapping it in % is what you'd do in legacy AHK.
However, that's legacy and basically should never be done anymore.
In an expression, you refer to a variable by just typing its name.

If you are in a legacy statement (basically just parameters in commands), you switch over to the expression syntax by starting off the parameter with a single % followed up by a space. You'll see examples of this below.

I'll fix your code line by line documenting the changes:

Here I force AHK to interpret an expression on the filename parameter just to explicitly specify a string with "".
This is not needed and makes no difference in practice, but it looks a lot better if you ask me. Especially if we were to have a longer and more complex string. Would eliminate the need of e.g. escaping certain characters etc.
Loop, Read, % "out.txt"

Here I removed the %s, because we're in function's parameters.
Functions aren't legacy AHK and they use the modern expression syntax, so we refer to variables by just simply typing their name.
tgt := StrSplit(A_LoopReadLine, A_Space)

Now we're again using a command. All commands use the legacy syntax on each parameter by default (unless otherwise specified in the documentation).
PixelGetColor actually happens to be one of those commands which would accept an expression on the x and y parameters, so me forcing an expression here by starting off the parameter with % isn't actually needed.
But it doesn't hurt, so I'll just do it to make it clearer and make it show what you'd normally do with commands.
PixelGetColor, hue, % tgt[0], % tgt[1]

The non-legacy if doesn't use the legacy syntax, so %% gets removed.
if (hue != tgt[2])

The Click command doesn't accept expressions, so we have to force it with a % .
Click, % tgt[0] ", " tgt[1]
And now if you were paying attention above, you should wonder why I didn't do this:
Click, % tgt[0], % tgt[1]
And that would be a very good question. You would do exactly that if it was literally any other command but the Click command.
The reason why you don't do that with the Click command, is because it's special (for good or for worse). It doesn't actually have different parameters, it just has one single string parameter. That one parameter contains all the required words, commas, and numbers.
And one single string parameter is what tgt[0] ", " tgt[1] creates. It concatenates those two values with the string comma and space.


So here's the full script:

Loop
{
    if (GetKeyState("Space"))
    {
        Loop, Read, % "out.txt"
        {
            tgt := StrSplit(A_LoopReadLine, A_Space)
            PixelGetColor, hue, % tgt[0], % tgt[1]
            if (hue != tgt[2])
            {
                Click, % tgt[0] ", " tgt[1]
            }
        }
    }
    Sleep, 10
}

To learn more about legacy vs expression, here's a pretty good documentation page to get you started:
https://www.autohotkey.com/docs/Language.htm


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...