Expressions and Statements

Recommended Posts

Can someone please explain to me a few things about these expressions and statements?

While 1

If PixelGetColor(33, 718) = (0x636978) Then

Send("{F9}")

EndIf

If PixelGetColor(33, 718) = (0xFFFFFF) Then

Send("{F1}")

EndIf

WEnd

#1. If I am understanding correctly, While 1 will do an infinite loop of both if expressions providing that

the expressions are true. Please tell me if I understand this part correctly.

#2 The rub comes in when .... 75% of the time the if expressionss will be false.

I am assuming I would need an Elseif expression and statement to make these 2 if expressions work properly. If not then I am barking up the wrong tree.

Basically what I am trying to do is send an F9 or an F1 keystroke if the expressions ever do become true.

However as it is now this little bit of scripting does not work. My brain probably isnt grasping the whole concept quite clearly. I have no experience in any programming language. And I really dont want to be one of these people that doesnt read or look for the answer. I have read the online documentation all the way through a few times and keep referencing back to it often. But this little problem here has me puzzled.

I am sure I can see the look on a few of the veteran members faces right now as they are reading this,

thinking to themselves oh lordy here we go again another noob that doesnt know his arse from a hole in the ground. This statement would be so true but hey you gotta start some where right?

I am not asking for someone to write the script for me just asking for a finger pointing in the right direction..... lol preferably not the bird.

Phy

Share on other sites

Can someone please explain to me a few things about these expressions and statements?

While 1

If PixelGetColor(33, 718) = (0x636978) Then

Send("{F9}")

EndIf

If PixelGetColor(33, 718) = (0xFFFFFF) Then

Send("{F1}")

EndIf

WEnd

#1. If I am understanding correctly, While 1 will do an infinite loop of both if expressions providing that

the expressions are true. Please tell me if I understand this part correctly.

#2 The rub comes in when .... 75% of the time the if expressionss will be false.

I am assuming I would need an Elseif expression and statement to make these 2 if expressions work properly. If not then I am barking up the wrong tree.

Basically what I am trying to do is send an F9 or an F1 keystroke if the expressions ever do become true.

However as it is now this little bit of scripting does not work. My brain probably isnt grasping the whole concept quite clearly. I have no experience in any programming language. And I really dont want to be one of these people that doesnt read or look for the answer. I have read the online documentation all the way through a few times and keep referencing back to it often. But this little problem here has me puzzled.

I am sure I can see the look on a few of the veteran members faces right now as they are reading this,

thinking to themselves oh lordy here we go again another noob that doesnt know his arse from a hole in the ground. This statement would be so true but hey you gotta start some where right?

I am not asking for someone to write the script for me just asking for a finger pointing in the right direction..... lol preferably not the bird.

Phy

In your case, it will do an 'infinate loop' regardless if both statements are true, and it will just keep checking those 2 PixelGetColor() statements, and doing the If/Then statements.

```Opt("SendKeyDelay", 1); So as soon as the colors are found they are sent the proper keys
Opt("PixelCoordMode", 2); replace the 2 with 0 for window coords (PixelGetColor(x-coord, y-coord)) or 1 for screen coords, which ever you used for getting your x and y coords

\$WindowTitle = "Put your window title of the application your going to send ot between the quotes"

While 1

If PixelGetColor(33, 718) = (0x636978) Then
If Not WinActive(\$WindowTitle) Then WinActivate(\$WindowTitle)
Send("{F9}")
EndIf

If PixelGetColor(33, 718) = (0xFFFFFF) Then
If Not WinActive(\$WindowTitle) Then WinActivate(\$WindowTitle)
Send("{F1}")
EndIf

Sleep(100); Putting a sleep in an infinite loop will help to make sure you don't burn your CPU up
WEnd```

Good Luck!

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share on other sites

Here is the whole script as it is, if what you are saying is true then this should work but it doesnt.

```HotKeySet("{ESC}", "MyExit")

WinActivate("[XXXXX]")

While 1

If PixelGetColor(33, 718) = (0x636978) Then
Send("{F9}")
Sleep(2000)
EndIf

If PixelGetColor(33, 718) = (0xFFFFFF) Then
Send("{F1}")
Sleep(2000)
EndIf

WEnd

Func MyExit()
Exit
EndFunc;==>MyExit```

I will keep messing with it to see if I can get it to work hehe.

Phy

Share on other sites

Hmmm thats interesting Larry it looks a lot cleaner this way.

```While 1
\$color = PixelGetColor(33, 718)
If \$color = 0x636978 Then
Send("{F9}")
ElseIf \$color = 0xFFFFFF Then
Send("{F1}")
EndIf
Sleep(20);small wait to give CPU a break
WEnd```

I am kind of curious as to how often it is checking for the colors? Is it moving at break neck speed? What is the timing of the checking of pixelgetcolor?

Phy

hehe Ok I just realised that the 2 if expression coords are the same they are not supposed to be oops.

But changing the coords of 2nd if expression still doesnt help the whole thing to work. *sighs

Edited by Phydough66

Share on other sites

Ok now I know this should be simple and I am getting a little edgy after trying several things so I am going to give it a rest for a While 1. hehe Anyways I discoverd that in my copying and pasting of lines in the script that I screwed up and forgot to change the 2nd if expression's coords.

```HotKeySet("{ESC}", "MyExit")

WinActivate("[XXXXX]")

While 1

If PixelGetColor(33, 718) = (0x636978) Then
Send("{F9}")
EndIf

If PixelGetColor(140, 649) = (0xFFFFFF) Then
Send("{F1}")
EndIf

Sleep(100)

WEnd

Func MyExit()
Exit
EndFunc;==>MyExit```

This is what I have currently and it is not working as I am expecting it to. I am wondering if maybe like Smoke is saying that it is going to fast and the keystroke isnt getting registered. I dont know.... but for now I will let it rest.

Phy

Share on other sites

Here is the whole script as it is, if what you are saying is true then this should work but it doesnt.

Sounds like bit of an insult, then Poof, straight from the AutoIt Guru himself... Larry Writes!!

Are your coordinates relative to the top,left of the window? check Opt("PixelCoordMode",...

Lar.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share on other sites

No No Smoke I wasnt trying to insult by any means by saying that. I was just saying it should work the way it is. If the coord thing is screen mode by default then that is what it is. I used screen mode in the window info tool to get the x,y coords. I did try what you had said earlier Smoke... the Opt lines I put those in and messed with them to no avail. I am assuming that you wouldnt have to put the opt line in if screen mode coords are the default?

Phy

Share on other sites

Screen Coords - are not always the best type of coords to use... if it is a window that can and or does get moved, I would suggest using the Opt("PixelCoordMode", 0 or 2), and getting specific window or client coords for the PixelGetColor(), you'll find it will save you alot of hassel now and in future Pixel/Mouseclick commands.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share on other sites

Not sure if this has been made clear yet: While 1..WEnd will always result in an infinite loop (as will Do..Until 0). The only way to break out of such a loop is to use ExitLoop within it (generally following an If..Then).

Games require longer keystrokes. Try explicitly setting SendKeyDownDelay to 50 or 100.

```Opt('SendKeyDownDelay', 50)
WinActivate('[XXXXX]')
HotkeySet('{ESC}', 'Off')

While 1
If PixelGetColor(33, 718) = 0x636978 Then Send('{F9}')
If PixelGetColor(140, 649) = 0xFFFFFF Then Send('{F1}')
Sleep(100)
WEnd

Func Off()
Exit
EndFunc```

Share on other sites

First of all I would like to thank you guys, SmOke_N, Larry, and LxP for helping a dimwitted noob such as myself. I finally got the script working as I wanted it to. Here is the final draft.

```HotKeySet("{ESC}", "MyExit")

Opt("SendKeyDelay", 50); So as soon as the colors are found they are sent

the proper keys
Opt("PixelCoordMode", 1); replace the 2 with 0 for window coords

\$WindowTitle = "XXXXXX"

While 1

If PixelGetColor(34, 707) = (0x5A6973) Then
If Not WinActive(\$WindowTitle) Then WinActivate(\$WindowTitle)
Send("{F9}")
Sleep(1000)
EndIf

If PixelGetColor(140, 649) = (0xFFFFFF) Then
If Not WinActive(\$WindowTitle) Then WinActivate(\$WindowTitle)
Send("{F1}")
Sleep(1000)
EndIf

WEnd

Func MyExit()
Exit
EndFunc;==>MyExit```

The first problem I was having and the reason the F9 key wasnt getting pressed was because the first expression was never ever going to be true.

```If PixelGetColor(33, 718) = (0x636978) Then
Send("{F9}")```

The Hex value was not supposed to be 0x636978 the 8 was supposed to be a B. (Mine eyes arent what they use to be!!!)

Larry and Smoke both were talking about the pixelcoordmode, so I decided to do some testing. Turns out that in client mode and window mode the coord's for both expressions were the same.

They both said x=-32 y=160. As it turns out the game is in full screen so minimising, closing or restarting computer did not change the screen mode value ever.

As I was doing all of this testing for the coordmode I kept reading my script over and over last night and was getting very frustrated so I decided to say screw it I am done for the night. So this morning I am up and I open the script for more editing and I am looking at things and I notice the Hex value should have a B instead of an 8, DOH!!!! After that things worked as they should.

I really appreciate you guys helping me with this because I really do know how it is to know something that is pretty basic in my mind but to try and explain it to someone who isnt getting the picture. Kudos to all of you for being extremely patient people.

Phy

Create an account

Register a new account