Sign in to follow this  
Followers 0
amidoinitrite

Basic if statement question

5 posts in this topic

#1 ·  Posted (edited)

I am trying to make a program which waits for an active window, then scans 4 locations for a certain color pixel. Whenever that pixel color is on that location...one of four keys is pressed. For the sake of the script, lets say the color is black and the locations are in the PixelGetColor part below.

This is what I came up with but it does not want to work :-/

WinWaitActive("Test Game")

HotKeySet("{End}","End")
Global $End
$End = NOT $End

Func end()
    $End = $End
    Exit 0
EndFunc


Do
    If Hex(PixelGetColor(216,101), 6) == 000000 Then
        Send("{LEFT}")
    EndIf
    If Hex(PixelGetColor(350,101), 6) == 000000 Then
        Send("{DOWN}")
    EndIf
    If Hex(PixelGetColor(490,101), 6) == 000000 Then
        Send("{UP}")
    EndIf
    If Hex(PixelGetColor(627,101), 6) == 000000 Then
        Send("{RIGHT}")
    EndIf
    sleep(5)
Until $End

Thanks for the help fellas :-) Also, sorry about probably some unneeded parts, I am fully in the learning process so I do not optimize anything yet.

Edited by amidoinitrite

Share this post


Link to post
Share on other sites



I am trying to make a program which waits for an active window, then scans 4 locations for a certain color pixel. Whenever that pixel color is on that location...one of four keys is pressed. For the sake of the script, lets say the color is black and the locations are in the PixelGetColor part below.

This is what I came up with but it does not want to work :-/

WinWaitActive("Test Game")

HotKeySet("{End}","End")
Global $End
$End = NOT $End

Func end()
    $End = $End
    Exit 0
EndFunc


Do
    If Hex(PixelGetColor(216,101), 6) == 000000 Then
        Send("{LEFT}")
    EndIf
    If Hex(PixelGetColor(350,101), 6) == 000000 Then
        Send("{DOWN}")
    EndIf
    If Hex(PixelGetColor(490,101), 6) == 000000 Then
        Send("{UP}")
    EndIf
    If Hex(PixelGetColor(627,101), 6) == 000000 Then
        Send("{RIGHT}")
    EndIf
    sleep(5)
Until $End

Thanks for the help fellas :-) Also, sorry about probably some unneeded parts, I am fully in the learning process so I do not optimize anything yet.

Welcome to the AutoIt forums amidoinitrite ;)

You are comparing a hex conversion of a decimal number to a number. This is not the reason for your script not working now but it will cause a problem later. A hex conversion (using Hex) returns a string so it will only be equal to the number for small values because AutoIt will automatically convert a string to a number if you try to use it as a number.

So

Hex(5) = 5; this will be true

Hex(16) = 16 ; will be false

Hex(16) = 10; will be true!

In other words you don't need need the Hex conversion of the pixelGetColor. I expect the reason yuou did it is because PixeGetColor returns a decimal value and colours are normally RGB expressed as hex. But writing a value as hex is just a convenient way of writing a number, but it's still a number with the same value as if it had been written in decimal assuming that is possible.

0x20 = 32;is true

When you declare a variable without giving it a value AutoIt sets it to an empty string. An empty string equates to false if used in a boolean expression so

Global $End
$End = NOT $End

would be the same as

Global $End = True

Then your statement

$End = $End

is saying set the value of $End to be the value of $End, so nothing changes.

Because $End is true your do loop will end after the first loop. You don't need the condition at the end of the loop because when you press End the script exits. Instead of

Until $end

try

Until False


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

Welcome to the AutoIt forums amidoinitrite ;)

You are comparing a hex conversion of a decimal number to a number. This is not the reason for your script not working now but it will cause a problem later. A hex conversion (using Hex) returns a string so it will only be equal to the number for small values because AutoIt will automatically convert a string to a number if you try to use it as a number.

So

Hex(5) = 5; this will be true

Hex(16) = 16 ; will be false

Hex(16) = 10; will be true!

In other words you don't need need the Hex conversion of the pixelGetColor. I expect the reason yuou did it is because PixeGetColor returns a decimal value and colours are normally RGB expressed as hex. But writing a value as hex is just a convenient way of writing a number, but it's still a number with the same value as if it had been written in decimal assuming that is possible.

0x20 = 32;is true

When you declare a variable without giving it a value AutoIt sets it to an empty string. An empty string equates to false if used in a boolean expression so

Global $End
$End = NOT $End

would be the same as

Global $End = True

Then your statement

$End = $End

is saying set the value of $End to be the value of $End, so nothing changes.

Because $End is true your do loop will end after the first loop. You don't need the condition at the end of the loop because when you press End the script exits. Instead of

Until $end

try

Until False

Thank you so much for the help martin! I'll try cleaning it up and getting to it work then will repost the code later tonight :-)

Share this post


Link to post
Share on other sites

Okay, I got the code to work but it runs way to slow to perform what is needed. Here is the finished code, now I need help optimizing the speed:

WinWaitActive("Test Game")
HotKeySet("{End}","End")

Func End()
    Exit 0
EndFunc


Do
    If PixelGetColor(200,193) == 65280 Then
        Send("{LEFT}")
    EndIf
    If PixelGetColor(356,236) == 65280 Then
        Send("{DOWN}")
    EndIf
    If PixelGetColor(472,190) == 65280 Then
        Send("{UP}")
    EndIf
    If PixelGetColor(633,236) == 65280 Then
        Send("{RIGHT}")
    EndIf
    sleep(5)
Until False

I'll continue reading through the forums trying to find a faster way.

Share this post


Link to post
Share on other sites

Okay, I got the code to work but it runs way to slow to perform what is needed. Here is the finished code, now I need help optimizing the speed:

WinWaitActive("Test Game")
HotKeySet("{End}","End")

Func End()
    Exit 0
EndFunc


Do
    If PixelGetColor(200,193) == 65280 Then
        Send("{LEFT}")
    EndIf
    If PixelGetColor(356,236) == 65280 Then
        Send("{DOWN}")
    EndIf
    If PixelGetColor(472,190) == 65280 Then
        Send("{UP}")
    EndIf
    If PixelGetColor(633,236) == 65280 Then
        Send("{RIGHT}")
    EndIf
    sleep(5)
Until False

I'll continue reading through the forums trying to find a faster way.

On my laptop, with the sleep removed, I get over 8000 loops of your Do/Until section per second. How fast do you hope to go?

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0