Jump to content

If IsArray VS. If Not @error // If VS ElseIf


Recommended Posts

Been a long time since I came back here, but I just finally starting learning AutoIt and I made a beginner script for a simple game called missile command). I'm basically trying to get it to auto check first if in-game, shoot at incoming missiles/enemies, auto collect money, and click the ok box once you level up.

From what I've learned from others, I've been able to write this script which works, but is very inefficient in clicking (guessing because pixels are searched top-down so it takes a few cycles until it finds all the pixels?)

HotKeySet("{F5}","ShootOnOff")
HotKeySet("{F6}","ExitApp")

Tooltip("F5 = Shoot, F6 = Exit bot", 0, 0)
Dim $shoot = False

Func ShootOnOff()
   If $shoot = False Then
      $shoot = True
      ToolTip("Shooting",0,0)
   Else
      $shoot = False
      Tooltip("F5 = Shoot, F6 = Exit bot", 0, 0)
   EndIf
EndFunc

Func ExitApp()
   Exit
EndFunc



While True
   
   If $shoot = True Then
            
       ;if in-game
       Dim $cordA = PixelSearch(164, 279, 270, 353, 0x00059)
        If Not @error Then
            
       ;if missile detected
            Dim $cordinates = PixelSearch( 164, 279, 846, 610, 0x272727)
                If Not @error Then
                MouseClick( "left", $cordinates[0]-15, $cordinates[1]+35, 1, 0) 
                Sleep(100)
                MouseClick( "left", $cordinates[0]+15, $cordinates[1]+35, 1, 0)
            EndIf
            
        ;if level up/unlock new items   
        Dim $cordB = PixelSearch(164, 279, 846, 610, 0xCA8601)
        If  Not @error Then
            MouseClick( "left", 482, 554, 1, 0)
            Sleep(1000)
            MouseClick( "left", 419, 550, 1, 0) 
        EndIf
        
        ;boss!
        Dim $cordC = PixelSearch(164, 279, 846, 610, 0x677000)
        If  Not @error Then
            MouseClick( "left", $cordC [0], $cordC [1], 1, 0)   
        EndIf
        
        ;pickup money
        Dim $cordD = PixelSearch(164, 279, 846, 610, 0x2D5212)
        If  Not @error Then
            MouseClick( "left", $cordD[0], $cordD[1], 1, 0)
            MouseClick( "left", 205, 226, 1, 0)
            MouseClick( "left", 382, 239, 1, 0)
        EndIf
    EndIf
EndIf
   Sleep(100)
WEnd

However, I tried to optimize this script by making it use 'If IsArray' rather than 'If Not @error,' however my script won't even run now; also made a few changes: attack other monsters, made the 'if level up' part of the code outside of the 'if in-game part,' etc. Can you guys take a look at what I'm doing wrong? Any help would greatly be appreciated!

HotKeySet("{F5}","ShootOnOff")
HotKeySet("{F6}","ExitApp")

Tooltip("F5 = Shoot, F6 = Exit bot", 0, 0)
Dim $shoot = False

Func ShootOnOff()
   If $shoot = False Then
      $shoot = True
      ToolTip("Shooting",0,0)
   Else
      $shoot = False
      Tooltip("F5 = Shoot, F6 = Exit bot", 0, 0)
   EndIf
EndFunc

Func ExitApp()
   Exit
EndFunc

;if in-game
Global $coordsA = PixelSearch(164, 279, 270, 353, 0x000056)
;if missile detected
Global $cordinates = PixelSearch(164, 279, 846, 610, 0x272727)
;red objects
Global $coordsE = PixelSearch(164, 279, 846, 610, 0x4E1411)    
;boss!
Global $coordsC = PixelSearch(164, 279, 846, 610, 0xE0FE00)
;pickup money
Global $coordsD = PixelSearch(164, 279, 846, 610, 0x2D5212)
;if level up/unlock new items   
Global $coordsB = PixelSearch(164, 279, 846, 610, 0xCA8601)

While True
   If $shoot = True Then
       ;if in-game
       While IsArray($coordsA)
           ;pickup money
           If IsArray($coordsD) Then
                MouseMove($coordsD[0], $coordsD[1], 0)
                Sleep(100)
                ;upgrade reload & supply
                MouseClick( "left", 205, 226, 1, 0)
                Sleep(100)
                MouseClick( "left", 382, 239, 1, 0)
                Sleep(100)
            EndIf
            
           ;if missile detected
           If IsArray($cordinates) Then
                MouseClick( "left", $cordinates[0]-25, $cordinates[1]+35, 1, 0) 
                Sleep(100)
                MouseClick( "left", $cordinates[0]+25, $cordinates[1]+35, 1, 0)
            EndIf

           ;if boss
           If IsArray($coordsC) Then    
                MouseClick( "left", $coordsC [0], $coordsC [1], 1, 0)   
                Sleep(100)
            EndIf
            
            ;if red objects
           If IsArray($coordsE) Then    
                MouseClick( "left", $coordsE [0], $coordsE [1], 1, 0)   
                Sleep(100)
            EndIf
            
        WEnd
       
       ;if level up/unlock new items [OUTSIDE OF IN-GAME]
        If IsArray($coordsB) Then
            MouseClick( "left", 482, 554, 1, 0)
            Sleep(1000)
            MouseClick( "left", 419, 550, 1, 0) 
            Sleep(100)
        EndIf
        
    EndIf
WEnd

Also, if someone could explain why by changing the if statements of 'if missile detected,' 'if boss' and 'if red objects' to ElseIf statements, I get an error? ie:

HotKeySet("{F5}","ShootOnOff")
HotKeySet("{F6}","ExitApp")

Tooltip("F5 = Shoot, F6 = Exit bot", 0, 0)
Dim $shoot = False

Func ShootOnOff()
   If $shoot = False Then
      $shoot = True
      ToolTip("Shooting",0,0)
   Else
      $shoot = False
      Tooltip("F5 = Shoot, F6 = Exit bot", 0, 0)
   EndIf
EndFunc

Func ExitApp()
   Exit
EndFunc

;if in-game
Global $coordsA = PixelSearch(164, 279, 270, 353, 0x000056)
;if missile detected
Global $cordinates = PixelSearch(164, 279, 846, 610, 0x272727)
;red objects
Global $coordsE = PixelSearch(164, 279, 846, 610, 0x4E1411)    
;boss!
Global $coordsC = PixelSearch(164, 279, 846, 610, 0xE0FE00)
;pickup money
Global $coordsD = PixelSearch(164, 279, 846, 610, 0x2D5212)
;if level up/unlock new items   
Global $coordsB = PixelSearch(164, 279, 846, 610, 0xCA8601)

While True
   If $shoot = True Then
       ;if in-game
       While IsArray($coordsA)
           ;pickup money
           If IsArray($coordsD) Then
                MouseMove($coordsD[0], $coordsD[1], 0)
                Sleep(100)
                ;upgrade reload & supply
                MouseClick( "left", 205, 226, 1, 0)
                Sleep(100)
                MouseClick( "left", 382, 239, 1, 0)
                Sleep(100)
            EndIf
            
           ;if missile detected
           ElseIf IsArray($cordinates) Then
                MouseClick( "left", $cordinates[0]-25, $cordinates[1]+35, 1, 0) 
                Sleep(100)
                MouseClick( "left", $cordinates[0]+25, $cordinates[1]+35, 1, 0)
            EndIf

           ;if boss
           ElseIf IsArray($coordsC) Then    
                MouseClick( "left", $coordsC [0], $coordsC [1], 1, 0)   
                Sleep(100)
            EndIf
            
            ;if red objects
           ElseIf IsArray($coordsE) Then    
                MouseClick( "left", $coordsE [0], $coordsE [1], 1, 0)   
                Sleep(100)
            EndIf
            
        WEnd
       
       ;if level up/unlock new items [OUTSIDE OF IN-GAME]
        If IsArray($coordsB) Then
            MouseClick( "left", 482, 554, 1, 0)
            Sleep(1000)
            MouseClick( "left", 419, 550, 1, 0) 
            Sleep(100)
        EndIf
        
    EndIf
WEnd

Thanks a ton!! :mellow:

Edited by xiaosongshu
Link to comment
Share on other sites

You have removed PixelSearch() from within the loop in your second version so it's never going to have any coordinate arrays to check. 

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

Link to comment
Share on other sites

Thanks! So is this the correct/most optimized code?

HotKeySet("{F5}","ShootOnOff")
HotKeySet("{F6}","ExitApp")

Tooltip("F5 = Shoot, F6 = Exit bot", 0, 0)
Dim $shoot = False

Func ShootOnOff()
   If $shoot = False Then
      $shoot = True
      ToolTip("Shooting",0,0)
   Else
      $shoot = False
      Tooltip("F5 = Shoot, F6 = Exit bot", 0, 0)
   EndIf
EndFunc

Func ExitApp()
   Exit
EndFunc

While True
   If $shoot = True Then
       
       ;if in-game
        Global $coordsA = PixelSearch(164, 279, 270, 353, 0x000056)
       
       ;if in-game
       If IsArray($coordsA) Then
           
        ;if missile detected
        Global $cordinatesLT = PixelSearch(164, 279, 505, 312, 0x272727)
        Global $cordinatesRT = PixelSearch(505, 279, 846, 312, 0x272727)
        Global $cordinates = PixelSearch(164, 312, 846, 610, 0x272727)
        ;red objects
        Global $coordsE = PixelSearch(164, 279, 846, 610, 0x4E1411)    
        ;boss!
        Global $coordsC = PixelSearch(164, 279, 846, 610, 0xE0FE00)
        ;pickup money
        Global $coordsD = PixelSearch(164, 279, 846, 610, 0x2D5212)    
           
           ;pickup money
           If IsArray($coordsD) Then
                MouseMove($coordsD[0], $coordsD[1], 0)
                Sleep(100)
                ;upgrade reload & supply
                MouseClick( "left", 205, 226, 1, 0)
                Sleep(100)
                MouseClick( "left", 382, 239, 1, 0)
                Sleep(100)
            EndIf
            
           ;if missile detected
           If IsArray($cordinatesLT) Then
                MouseClick( "left", $cordinatesLT[0]+25, $cordinatesLT[1]+35, 1, 0) 
                Sleep(100)
            EndIf
           If IsArray($cordinatesRT) Then
                MouseClick( "left", $cordinatesRT[0]-25, $cordinatesRT[1]+35, 1, 0)
                Sleep(100)
            EndIf
            If IsArray($cordinates) Then
                MouseClick( "left", $cordinates[0]-25, $cordinates[1]+35, 1, 0)
                Sleep(100)
                MouseClick( "left", $cordinates[0]+25, $cordinates[1]+35, 1, 0)
                Sleep(100)
            EndIf 

           ;if boss
           If IsArray($coordsC) Then    
                MouseClick( "left", $coordsC [0], $coordsC [1], 1, 0)   
                Sleep(100)
            EndIf
            
            ;if red objects
           If IsArray($coordsE) Then    
                MouseClick( "left", $coordsE [0]-50, $coordsE [1], 1, 0)
                Sleep(100)
                MouseClick( "left", $coordsE [0]+50, $coordsE [1], 1, 0)
                Sleep(500)
            EndIf
            
        EndIf
        
        ;if level up/unlock new items   
        Global $coordsB = PixelSearch(164, 279, 846, 610, 0xCA8601) 
       
       ;if level up/unlock new items [OUTSIDE OF IN-GAME]
        If IsArray($coordsB) Then
            MouseClick( "left", 482, 554, 1, 0)
            Sleep(1000)
            MouseClick( "left", 419, 550, 1, 0) 
            Sleep(100)
        EndIf
        
    EndIf
WEnd

And is there anyway to:

-Make pixelsearch start from bottom up rather than top down? Or always search closest pixel to certain location? Or possibly prioritize money to always be collected?

-Speed up pixelsearch?

-Use pixelsearch to determine if a missile is firing toward the left diagonal or right diagonal? ie. \ or /. Would I have to search multiple pixels somehow?

-Make pixelsearch do certain actions only once! on a given enemy and then search for another pixel right away?

-Determine the speed at which certain missiles are firing and change +/- depth accordingly? Or another strat I can do to accommodate the increasing number of missiles and their varying speeds?

Here are two videos of it in action:

Level 2: http://www.youtube.com/watch?v=OXEaqP19kKA

Level 3: http://www.youtube.com/watch?v=PmL8geiPetU

**Notes**

-I am buying instant reloads to show what would happen if I had unlimited ammo (happens in higher levels, ie. 30+)

-Level 2 wasn't too bad, however still a lot of times isn't responsive (enough)

-Level 3, too many missiles on screen, cannot attack all fast enough, does not pick up all moneys

-Sometimes just doesn't attack or pickup even when no enemies on screen

-Cannot adjust for varying speed of enemies so misses a lot

Edited by xiaosongshu
Link to comment
Share on other sites

Thats a lot of questions, I'll answer the first

"-Make pixelsearch start from bottom up rather than top down?"

yes, its all documented in the helpfile

also you could change your function (could be conciderd optimize)

Func ShootOnOff()
   If $shoot = False Then
      $shoot = True
      ToolTip("Shooting",0,0)
   Else
      $shoot = False
      Tooltip("F5 = Shoot, F6 = Exit bot", 0, 0)
   EndIf
EndFunc

to

Func ShootOnOff($shoot)
   $shoot = Not $shoot
EndFunc

as for the rest, you really need to do some reading, and testing.

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

Hi JohnOne,

I read the helpfile and could not find an example of using bottom < top. I did find this thread http://www.autoitscript.com/forum/index.php?showtopic=101976&st=0&p=724486&hl=pixelsearch%20%20search%20direction&fromsearch=1&#entry724486 and I tried taking a look at SmOke_N's posts but still am a bit confused. Could you help clarify how exactly I would change the parameters?

Link to comment
Share on other sites

I'm not sure how to explain it any simpler than the helpfile

If your right coordinate is less than your left coordinate, it will search from right to left.

If your bottom coordinate is less than your top coordinate, it will search from bottom to top.

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

Let me use my graphics skillz to illustrate it for you

If you want to saerch from bottom to top with a 200x200 rect like this

Posted Image

then you would do this

Pixelsearch(200,200,0,0,$color)

I think :mellow:

Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

LOLS! Love the pic haha, but seriously thanks! Didn't know it was that simple. With that fix and a simple change in the area I'm scanning and the shades I'm scanning for, got it to speed up quite a bit. Any other tips would be appreciated as well. Cheers :mellow:

Edited by xiaosongshu
Link to comment
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
 Share

  • Recently Browsing   0 members

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