 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
SKP
Joined: 08 Feb 2008 Posts: 17
|
Posted: Fri Feb 08, 2008 7:29 pm Post subject: Lil help for script |
|
|
This is the first script i have posted, i noticed in the help files that goto is discouraged, and that other methods are encouraged, this is the first script i have ever made. I would like someone to show me how to do the script without the use of the goto script, since i do not fully understand the alternative commands or how to use them, as i have never programmed before.
Have to move character if bag is full as it will continuosly refill.
| Code: |
#SingleInstance force
;Move character to the left
Mouseclick, left, 100,350
Sleep, 1000
;Bring up inventory
Send, {F9}
Sleep, 1000
Loop, 16
{
;Loop for item 1 and disguard if present, if not move on.
PixelSearch, Px, Py, 0, 15, 300, 170, 0xB317E3, 3, Fast
If Errorlevel
Goto, Label2
Else
{
Mouseclick, left, %Px%, %Py%
Sleep, 100
Mouseclick, left, 500, 200
Sleep, 100
Send {Enter}
}
}
;If loop runs out, move to the left and restart script
Send {F9}
reload
return
;If not move on to item 2
Label2:
Loop, 16
{
PixelSearch, Px, Py, 0, 15, 300, 170, 0x192893, 3, Fast
If Errorlevel
Goto, Label1
Else
{
Mouseclick, left, %Px%, %Py%
Sleep, 100
Mouseclick, left, 500, 200
Sleep, 100
Send {Enter}
}
}
Send, {F9}
reload
return
;The section below is for launching and then running another script.
Label1:
Send, {F9}
Mouseclick, left, 500, 0 ; Top right
Sleep, 1000
Run ahk bot script
Sleep, 1000
Send, ^m
Exitapp
return
^p::pause |
|
|
| Back to top |
|
 |
dmatch
Joined: 15 Oct 2007 Posts: 113
|
Posted: Sat Feb 09, 2008 6:14 pm Post subject: |
|
|
Hi SKP,
Remember "if it aint broke don't fix it", so if you want to resturcture this script save it so you "have a bird in hand." Afterall, you may have other things that you need to get to doing and run out of time for messing with it.
The problem with GoTo statements is demonstrated pretty well with your script. A script with GoTos tends to be harder to follow because, unlike a GoSub statement, a GoTo does not return to where it was called. So one GoTo tends to create another GoTo and so on until it gets confusing. Due to this tendency it also tends to create more code than neccesary.
The readability of a script may also be enhanced by using descriptive names for the labels or subroutines. If you ever return to work on the script in the distant future that may be helpful in re-learning how the script works.
The Reload command appears to be for use in the event you have changed a script and wish to reload it. While your use of it makes for a nifty way to get back to the first of the script a main loop would do that also without having to reload the script. Your means of exiting the main loop and thus the script would be through what you have coded as Label1. That's where you "Run ahk bot script" and then "ExitApp".
By using no GoTos or labels at all the script could be made much smaller and more readible.
There are more ways than one to skin a cat but here is one way: | Code: | #SingleInstance force
;**** Start a main loop in place of "Reloads" ******
Loop
{
;Move character to the left
Mouseclick, left, 100,350
Sleep, 1000
;Bring up inventory
Send, {F9}
Sleep, 1000
Loop, 16
{
;**** Look for either of 2 colors ****
PixelSearch, Px, Py, 0, 15, 300, 170, 0xB317E3, 3, Fast
If Errorlevel ;**** Search for second color ****
PixelSearch, Px, Py, 0, 15, 300, 170, 0x192893, 3, Fast
if Errorlevel ;**** Didn't find either color ****
{ ;**** Run other script ****
Send, {F9}
Mouseclick, left, 500, 0 ; Top right
Sleep, 1000
Run ahk bot script ;**** Run other script ****
Sleep, 1000
Send, ^m
Exitapp ;**** Completely exit this script ****
}
Else{ ;**** Comes here if either color above was found ****
Mouseclick, left, %Px%, %Py%
Sleep, 100
Mouseclick, left, 500, 200
Sleep, 100
Send {Enter}
}
}
;If loop runs out, move to the left and restart script
Send {F9}
};**** End of Main Loop **** returns to top of loop ****
^p::pause
|
I hope I understood what you were doing. I think you were looking for either of 2 colors to exist and if they did you did one thing, and if they did not you ran this other "ahk bot script ". If that is not correct then this may not work for you.
Hope this helps,
dmatch |
|
| Back to top |
|
 |
Lexikos
Joined: 17 Oct 2006 Posts: 2699 Location: Australia, Qld
|
Posted: Sat Feb 09, 2008 11:53 pm Post subject: |
|
|
| dmatch wrote: | A script with GoTos tends to be harder to follow because, unlike a GoSub statement, a GoTo does not return to where it was called.
| That makes no sense. When following a gosub, you'd need to find the label, then find the return, exit or exitapp. There may be more than one... Following a goto requires less work since you already know it won't return.
| Quote: | So one GoTo tends to create another GoTo and so on until it gets confusing. Due to this tendency it also tends to create more code than neccesary.
| That does sound like SKP's script, but that is because goto was used unnecessarily. The same could be true of any construct.
| Quote: | | By using no GoTos or labels at all the script could be made much smaller and more readible. | I disagree; by using more appropriate constructs, the script is made smaller and more readable.
There are times when a goto is the simplest/most efficient/most intuitive way to do something. For instance,
| Code: | Loop {
Loop {
;...
if some-condition
goto break_outer_loop
}
;...
}
break_outer_loop:
| In a simple case, you could break then check the condition again. In a complex case, you may have many break conditions, so you could set a flag, break, then check the flag. Either way goto reduces the amount of code and processing.
That said, I generally don't use goto; not because it's "bad", but because it is rarely appropriate.
What better way to end my post than with a wikipedia link: Criticism of goto usage. |
|
| Back to top |
|
 |
dmatch
Joined: 15 Oct 2007 Posts: 113
|
Posted: Sun Feb 10, 2008 4:03 pm Post subject: |
|
|
Hello lexiKos,
You say "That makes no sense". Well, that's a very extreme statement so I guess you are just looking for argument. If you wanted to you probably could find some perspective from which to see some sense in it, since you seem to be an intelligent individual, especially when it comes to programming. Other of your comments also seem to indicate more of a desire to confront me than to help SKP.
Seeing that you actually did have something constructive to add, I wish that you had directed your efforts and comments more to SKP instead of me.
dmatch |
|
| Back to top |
|
 |
[VxE]
Joined: 07 Oct 2006 Posts: 1426
|
Posted: Sun Feb 10, 2008 7:28 pm Post subject: |
|
|
FWIW: | Code: |
#SingleInstance force
SetKeyDelay, 10, 1000
Hotkey, Pause, TheBeginning, on
Pause::Pause
;Use the pause key to start or pause the script
TheBeginning:
Hotkey, Pause, Pause, on
;Move character to the left & Bring up inventory
SetMouseDelay, 1000
Send, {click 100, 350}{F9}
SetMouseDelay, 100
Loop 2
{ ; outer loop passes twice, the only difference is that the searchcolor changes
SearchColor := (A_index-1) ? "0x192893" : "0xB317E3"
Loop, 16
{
;Loop for item 1 and disguard if present, if not move on.
PixelSearch, Px, Py, 0, 15, 300, 170, %searchcolor%, 3, Fast
If Errorlevel
Break
Else
Send {Click %px% %py%}{Click 500 200}{Enter}
;If loop runs out, move to the left and reset script
If A_InDex = 16
{
Send {F9}
Hotkey, Pause, TheBeginning, on
return
}
}
}
;The section below is for launching and then running another script.
Send, {F9}{Click 500 0}{RShift} ; Top right
Run ahk bot script
Sleep, 1000
Send, ^m
Hotkey, Pause, TheBeginning, on
return |
Should be very very close to the order of execution of the script in the OP with regards to the PixelSearches and key/mouse inputs.
As far as "goto" is concerned, there will always be a wealth of proponents to provoke the naysayers because the term has a long history. I rarely use goto... but I don't know why. _________________ My Home Thread
More Common Answers: [1]. It's in the FAQ [2]. Ternary ( a ? b : c ) guide [3]. Post code inside [code][/code] tags ! |
|
| Back to top |
|
 |
SKP
Joined: 08 Feb 2008 Posts: 17
|
Posted: Sun Feb 10, 2008 9:42 pm Post subject: |
|
|
Thank you all very much for helping me understand how to accomplish the goals of my script without the use of goto dmatch, you seem to have understood perfectly how my script operated.
SKP |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|