Jump to content

Recommended Posts

Posted

I've been trying to write a script for the past couple days, and all would be well, except AutoIT appears to be returning incorrect results. The idea behind the script is to read a set of coordinates from a text file (current location appears as the last line in the file), and to travel to the coordinates marked in $TargetX and $TargetY.

The program itself works fine. However, the issue is that the program isn't supposed to resend unnecessary data. If an object is already travelling in a specific direction, there's no need to keep firing off that direction. That was my intention behind:

If ($Direction == 4 OR $Direction == 6 OR $Direction == 7 OR $Direction == 1) Then

Send("{F5}")

$Direction = 5

;South

EndIf

and any of the other similar blocks of code. If $Direction is set to a number, it shouldn't be able to set itself to that number again without passing through another number first, (eg West -> North -> West, not West -> West -> West). Unfortunately, it still isn't working. I've tried using NOT, <>, multiple If statements, and, etc, and nothing works. Everyone I know is currently stumped as to why AutoIT keeps allowing a key to be pressed multiple times.

Ambiguous Variables: $Ticker is used to prevent twitchy movement on a diagonal, by requiring a delay between movements. This forces a staircase like pattern, as opposed to a really buggy diagonal line. Pressing 9 updates the coordinates in the text file, and the ToolTip is just for debugging. $XMovement and $YMovement are designed to prevent something from being stuck on one axis.

; Press Esc to terminate script

Global $UnPaused
HotKeySet("{INS}", "TogglePause")
HotKeySet("{ESC}", "Terminate")

While 1
    Sleep(1000)
WEnd

Func TogglePause()
    $Destination = False
    $file = FileOpen("test.txt", 0)
    ; Check if file opened for reading OK
    If $file = -1 Then
        MsgBox(0, "Error", "Unable to open file.")
        Exit
    EndIf

    $TargetX = 4500.000000
    $TargetY = 4500.000000
    $Direction = 1
    $XMovement = True
    $YMovement = True
    $Ticker = 1

    ; Read in lines of text until the EOF is reached
    While $Destination == False
        Send("9")
        While 1
            $line = FileReadLine($file)
            If @error = -1 Then ExitLoop
            $ToolTip = $line
        WEnd
        
        $PositionX = StringMid($ToolTip, 9, 11)
        $PositionY = StringMid($ToolTip, 26, 30)
        $Ticker = $Ticker + 1
        ;ToolTip($PositionX & @CRLF & $PositionY & @CRLF & "XMovement:" & $XMovement & @CRLF & "YMovement:" & $YMovement & @CRLF & "Direction: " & $Direction, 0, 0)
        If ($Ticker > 100) Then
            If $YMovement == True Then
                If ($PositionY >= ($TargetY - 5)) Then
                    If ($Direction == 4 OR $Direction == 6 OR $Direction == 7 OR $Direction == 1) Then
                        Send("{F5}")
                        $Direction = 5
                        ;South
                    EndIf
                EndIf
                
                If ($PositionY <= ($TargetY + 5)) Then
                    If ($Direction == 5 OR $Direction == 6 OR $Direction == 7 OR $Direction == 1) Then
                        Send("{F4}")
                        $Direction = 4
                        ;North
                    EndIf
                EndIf
            EndIf

            If $XMovement == True Then
                If ($PositionX >= ($TargetX + 5)) Then
                    If ($Direction == 5 OR $Direction == 6 OR $Direction == 4 OR $Direction == 1) Then
                        Send("{F7}")
                        $Direction = 7
                        ;West
                    EndIf
                EndIf
                
                
                If ($PositionX <= ($TargetX - 5)) Then
                    If ($Direction == 5 OR $Direction == 4 OR $Direction == 7 OR $Direction == 1) Then
                        Send("{F6}")
                        $Direction = 6
                        ;East
                    EndIf
                EndIf
                
            EndIf
            $Ticker = 0
        EndIf
        
        If ($PositionY > ($TargetY - 5)) Then
            If ($PositionY < ($TargetY + 5)) Then
                $YMovement = False
            EndIf
        EndIf

        If ($PositionX > ($TargetX - 5)) Then
            If ($PositionX < ($TargetX + 5)) Then
                $XMovement = False
            EndIf
        EndIf
        
        If ($XMovement == False) And ($YMovement == False) Then
            Send("{SPACE}")
            Exit 0
        EndIf
        
    WEnd
    FileClose($file)
EndFunc   ;==>TogglePause


Func Terminate()
    Exit 0
EndFunc   ;==>Terminate
Posted (edited)

The program itself works fine. However, the issue is that the program isn't supposed to resend unnecessary data. If an object is already travelling in a specific direction, there's no need to keep firing off that direction. That was my intention behind:

I don't understand your goal but what i do to avoid this is using variables like that:

$EventFired = False

While 1
   If Not $EventFired And ... then
      ...
      $EventFired = True
   EndIf
Loop

You need to set $EventFired to False if you want to start again... Hope it helps

Edited by Jango

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...