Sign in to follow this  
Followers 0
Nethel

Help With Functions

14 posts in this topic

So this is my code :

HotKeySet("{NUMPADSUB}", "Terminate")

Func Terminate()
    Exit
EndFunc


Func target ()
    MouseClick("right", 960, 170, 1)
    sleep (500)
    MouseClick("left", 978, 224, 1)
    sleep (6000)
    Send ("{F1}")
    Sleep (500)
    Send ("{F2}")
    Sleep (500)
    Send ("{F3}")
    Sleep (500)
    Send ("{F4}")
    Sleep (500)
EndFunc

Func drag ()
    While 1
    MouseClickDrag( "left", 145, 520, 145, 235 ,1 )
    sleep (60000)
    WEnd
EndFunc

Func checkroid ()
    While 1
    If PixelGetColor(514,94) = 0x404040 Then
    continueloop
    Else 
    target ()
    EndIf
    sleep (10000)
    WEnd
EndFunc

While 1
target ()
drag ()
checkroid ()
WEnd

So, it runs perfectly, but the checkroid function never gets called, and therefore never serves its purpose. All the elements work on their own, but not when put together. Any help would be appriciated.

Share this post


Link to post
Share on other sites



Removed the endless loop from the drag function, now it only drags once at the beginning of the macro, and ceaces to drag ever again. If I remove the loop from the checkroid function the continue loop function does not work. Any ideas?

Share this post


Link to post
Share on other sites

Yeah, I think I see what you are trying to do.

Here is the problem:

You have 1 main function that is in an endless loop. That is OK.

But, then you have 2 seperate sub functions that have endless loops as well. That is not OK.

You need to remove the While....Wend from your two subfunction, or give them some trigger that meets the While criteria so that the functions end and kick back to the main function.

:whistle:

Based on a quick look at your code, it appears as though you are trying to select something, then checking to see if it is there. Is this some type of game, like an RTS, where you are trying to "build" a unit, select it once it is built, move it to another location, then build another one? Kind of hard to tell, but it feels like it...

If you tell me a little about what it is you are trying to accomplish, then I might be able to help you. No promises...

Share this post


Link to post
Share on other sites

yea, sounds like you would want:

HotKeySet("{NUMPADSUB}", "Terminate")

While 1
target ()
drag ()
checkroid ()
WEnd

Func Terminate()
   Exit
EndFunc


Func target ()
MouseClick("right", 960, 170, 1)
sleep (500)
MouseClick("left", 978, 224, 1)
sleep (6000)
Send ("{F1}")
Sleep (500)
Send ("{F2}")
Sleep (500)
Send ("{F3}")
Sleep (500)
Send ("{F4}")
Sleep (500)
EndFunc

Func drag ()
MouseClickDrag( "left", 145, 520, 145, 235 ,1 )
sleep (60000)
EndFunc

Func checkroid ()
If PixelGetColor(514,94) = 0x404040 Then
continueloop
Else
target ()
EndIf
sleep (10000)
EndFunc

AutoIt3, the MACGYVER Pocket Knife for computers.

Share this post


Link to post
Share on other sites

So Larry, I would like the game to target the asteroid, with the targte function. Then in the target, it activates the mining lasers, and the drags the ore from my cargo hold to an external holding area called a can. So, the roid occasionally depletes, hence the checkroid function. It does not matter in what order they run, as long as it can target, then drag the ore every 60 seconds, and then check the roid every 10-60 seconds.

Scriptkiddy, if I remove the loop from the checkroid function, the continueloop command cannot function.

Share this post


Link to post
Share on other sites

Why are you calling the Target function from the Checkroid function?

You should just check the asteroid, and if it is depleted then end the Checkroid fuction, which will kick you back to the main function, whose next step is to target.

Try something like this (not sure if I got the variable stuff right, been a while since I've done any coding):

Func checkroid ()

$Asteroid = 0x404040

While $Asteroid = 0x404040

$Asteroid = PixelGetColor(514,94)

Wend

sleep (10000)

EndFunc

I believe that this way, if the Asteoid has depleted (no longer the right pixel), you go back to the main function, which then sets up the next target function, continuing the loop until you terminate.

Share this post


Link to post
Share on other sites

Ok, so I changed that, makes sense to me.

But the drag function is still not repeating, my code now looks like :

HotKeySet("{NUMPADSUB}", "Terminate")

Func Terminate()
    Exit
EndFunc

$Asteroid = PixelGetColor(514,94)

Func target ()
    MouseClick("right", 960, 170, 1)
    sleep (500)
    MouseClick("left", 978, 224, 1)
    sleep (6500)
    Send ("{F1}")
    Sleep (500)
    Send ("{F2}")
    Sleep (500)
    Send ("{F3}")
    Sleep (500)
    Send ("{F4}")
    Sleep (500)
EndFunc

Func drag ()
    MouseClickDrag( "left", 145, 520, 145, 235 ,1 )
    sleep (60000)
EndFunc

Func checkroid ()
$Asteroid = 0x404040
While $Asteroid = 0x404040
Wend
sleep (10000)
EndFunc

While 1
target ()
drag ()
checkroid ()
WEnd

Share this post


Link to post
Share on other sites

Okay, why do you need to drag the ore every 60 seconds? Is this to keep the ore in the can? Does it drift out or something?

By the sounds of it I think you need to embed the ore checking function into the drag function, but I need a little more detail to be certain.

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

I need the drag the ore, because my cargo hold fills up every 60 seconds.

EDIT - The check is not to make sure the ore is there, its to make usre that the asteroid is still there, and to target a new one if it is not.

Edited by Nethel

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

I think I get it now.

You target an asteroid which contains some unknown amount of ore. When it is targeted it is collecting the ore into your cargo hold. You need to drag the ore to the can when the hold fills up, every 60 seconds. When you are done draging the ore to the can you need to target the asteroid again.

If this is correct then you need to do something like this:

; Setup Escape Hotkey
HotKeySet("{NUMPADSUB}", "Terminate")

; Escape From Macro
Func Terminate()
   Exit
EndFunc

; Target Asteroid and set Mining Beams to extract ore
Func target ()
MouseClick("right", 960, 170, 1)
sleep (500)
MouseClick("left", 978, 224, 1)
sleep (6500)
Send ("{F1}")
Sleep (500)
Send ("{F2}")
Sleep (500)
Send ("{F3}")
Sleep (500)
Send ("{F4}")
Sleep (500)
EndFunc

; Check Asteroid Every 10 Seconds to see if it still has ore
; Every 6th check (60 seconds) drag ore from cargo hold to storage can
Func drag ()
$Asteroid = 0x404040
$X = 0
While $Asteroid = 0x404040
      Sleep (10000)
      $Asteroid = PixelGetColor(514,94)
      $X = $X + 1
      If $X == 6
          MouseClickDrag( "left", 145, 520, 145, 235 ,1 )
          $X = 0
      EndIf
WEnd
EndFunc

While 1
target ()
drag ()
WEnd

If I have this set up right, it should work just fine. Of course I am just guessing that the $X is what is required for the counter variable. I have not yet looked into any other variable declaration statements for this program...

:whistle: Had to edit to add the second = to the If statement. Those =='s always trip me up...

B) Dang, moved the sleep line up one so that it waits 10 seconds before the first check...

Ken

Edited by krclark

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

I do not need to retaget the roid every time, just need to make sure the asteroid is still spawned. If not, I need to target a new one. But, this code may be the best solution.

Edited by Nethel

Share this post


Link to post
Share on other sites

I do not need to retaget the roid every time, just need to make sure the asteroid is still spawned.

This code is set up to initially target the asteroid, then wait 10 seconds to see if the asteroid is still there. It repeats the 10 second wait and check cycle 6 times, then drags your cargo hold to the can. Then it repeats the 10 second wait and check cycle again. The only time it retargets the asteroid is if the pixel check routine fails.

The one problem you may encounter is when the cargo hold is nearly full (say 50 seconds worth of ore collection) and then the asteroid disappears. At that point it will target a new asteroid and start again, but it will restart the 60 seconds as well.

I will post another code soon that should cover this case as well.

Share this post


Link to post
Share on other sites

Tested, it trys to target every 10 seconds, and while the asteroid is targeted the target button in game switches to untarget :whistle:. Also, manually prevented the untargeting and it never got to the drag function. Thank you all for your help.

PS. Had to add a then after the If $X == 6

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

OK, here is one more way you could try it. If this still does not work then it must be me not quite understanding this program yet...:whistle:

; Setup Escape Hotkey
HotKeySet("{NUMPADSUB}", "Terminate")

; Setup known values
$X = 0

; Main Loop
While 1
  ; Target an Asteroid
     MouseClick("right", 960, 170, 1)
     sleep (500)
     MouseClick("left", 978, 224, 1)
     sleep (6500)
     Send ("{F1}")
     Sleep (500)
     Send ("{F2}")
     Sleep (500)
     Send ("{F3}")
     Sleep (500)
     Send ("{F4}")
     Sleep (500)

  ; While an Asteroid is targeted
  ; Check to ensure it is still there
     While Hex (PixelGetColor(514,94),6) == 404040
      ; Wait 10 Seconds
         Sleep (10000)
      ; Update Cargo Hold Full Countdown
         $X = $X + 1
      ; If 60 seconds elapsed then dump cargo hold to can and restart countdown
          If $X == 6 Then
              MouseClickDrag( "left", 145, 520, 145, 235 ,1 )
              $X = 0
         EndIf
  ; Go back to checking if asteroid is still there
     WEnd
; If asteroid is not there go back to target a new asteroid
WEnd

; Escape From Macro
Func Terminate()
  Exit
EndFunc

Let me know what happens.

:iamstupid: Edited to fix the glaring error!! (Should have checked it earlier.)

The vaule you want to check against for the asteroid was written as hex (0x404040), but the function retrieves a Decimal value.

I converted the asteroid check value to a 6 digit number and added the Hex command in front of the pixel color check function.

This would explain why it was constantly targetting the asteroid. It never saw one being there!

Edited by krclark

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