Hi,
I was just wondering what the difference is between the commands "GoSub" and "Goto"?.
Difference between GoSub & Goto?
Moderators: Dorian (MJT support), JRL
- Dorian (MJT support)
- Automation Wizard
- Posts: 1384
- Joined: Sun Nov 03, 2002 3:19 am
- Contact:
Re: Difference between GoSub & Goto?
Goto will jump to a label and carry on from there, missing out everything inbetween. So:
Whereas Gosub jumps to a Subroutine - a self contained section of code - executes all the code in that subroutine, then goes back to next line after the gosub. This is useful if you have certain code you use a lot. You don't have to keep repeating it. I know you use Image Recognition a lot, so you could, for example, use it to look for a certain image wherever you used it in your script, without needing to use the same Image Recognition code over and over. Like this :
Try experimenting with this very simple example in the editor, with >Debug >Trace so you can see it run line-by-line
Code: Select all
MDL>Hi
Goto>Jump
MDL>This line will be skipped
Label>Jump
MDL>And carry on from here.
Code: Select all
Gosub>LookforImage1
..do something else
Gosub>LookforImage2
..do something else
Gosub>LookforImage3
..do something else
//look for image 2 again
Gosub>LookforImage2
SRT>LookforImage1
FindImagePos>%BMP_DIR%\image_1.bmp,SCREEN,0.7,1,XArr,YArr,NumFound,CCOEFF
If>NumFound>0
MouseMove>XArr_0,YArr_0
LClick
Endif
END>LookforImage1
SRT>LookforImage2
FindImagePos>%BMP_DIR%\image_2.bmp,SCREEN,0.7,1,XArr,YArr,NumFound,CCOEFF
If>NumFound>0
MouseMove>XArr_0,YArr_0
LClick
Endif
END>LookforImage2
SRT>LookforImage3
FindImagePos>%BMP_DIR%\image_3.bmp,SCREEN,0.7,1,XArr,YArr,NumFound,CCOEFF
If>NumFound>0
MouseMove>XArr_0,YArr_0
LClick
Endif
END>LookforImage3
Code: Select all
Gosub>MySubroutine
MDL>Now I am back up here
Gosub>MySubroutine
SRT>MySubroutine
MDL>I jumped down here
END>MySubroutine
Yes, we have a Custom Scripting Service. Message me or go here
Re: Difference between GoSub & Goto?
Hi Dorian,Dorian (MJT support) wrote: ↑Wed Apr 03, 2019 12:13 pmGoto will jump to a label and carry on from there, missing out everything inbetween. So:
Whereas Gosub jumps to a Subroutine - a self contained section of code - executes all the code in that subroutine, then goes back to next line after the gosub. This is useful if you have certain code you use a lot. You don't have to keep repeating it. I know you use Image Recognition a lot, so you could, for example, use it to look for a certain image wherever you used it in your script, without needing to use the same Image Recognition code over and over. Like this :Code: Select all
MDL>Hi Goto>Jump MDL>This line will be skipped Label>Jump MDL>And carry on from here.
Try experimenting with this very simple example in the editor, with >Debug >Trace so you can see it run line-by-lineCode: Select all
Gosub>LookforImage1 ..do something else Gosub>LookforImage2 ..do something else Gosub>LookforImage3 ..do something else //look for image 2 again Gosub>LookforImage2 SRT>LookforImage1 FindImagePos>%BMP_DIR%\image_1.bmp,SCREEN,0.7,1,XArr,YArr,NumFound,CCOEFF If>NumFound>0 MouseMove>XArr_0,YArr_0 LClick Endif END>LookforImage1 SRT>LookforImage2 FindImagePos>%BMP_DIR%\image_2.bmp,SCREEN,0.7,1,XArr,YArr,NumFound,CCOEFF If>NumFound>0 MouseMove>XArr_0,YArr_0 LClick Endif END>LookforImage2 SRT>LookforImage3 FindImagePos>%BMP_DIR%\image_3.bmp,SCREEN,0.7,1,XArr,YArr,NumFound,CCOEFF If>NumFound>0 MouseMove>XArr_0,YArr_0 LClick Endif END>LookforImage3
Code: Select all
Gosub>MySubroutine MDL>Now I am back up here Gosub>MySubroutine SRT>MySubroutine MDL>I jumped down here END>MySubroutine
Am I right in thinking that the gosub runs faster than say the other types of loops like goto etc?. I'll be testing this out ASAP.
Thanks,
Sam
- Dorian (MJT support)
- Automation Wizard
- Posts: 1384
- Joined: Sun Nov 03, 2002 3:19 am
- Contact:
Re: Difference between GoSub & Goto?
Tbh I'm not sure there'd be any difference, speed-wise. In my opinion they are more convenient if you're using the same routines over and over.
Yes, we have a Custom Scripting Service. Message me or go here