Functions?

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
Warren
Pro Scripter
Posts: 83
Joined: Sun Oct 08, 2017 11:57 pm

Functions?

Post by Warren » Mon Oct 09, 2017 12:24 am

I'm not sure if I'm blind, tired, or just missing something, but I just read through nearly everything in the help section, and searched several times on here, and I'm not seeing a few basic but crucial elements. Maybe I'm using the wrong terminology for searches.

I'll break this down into one issue per thread to help keep the forum organized.

First issue: How do I "functionize" the following so that I can just have the script in one spot, and call it in a single line everywhere else while passing parameters to it.

So, something like: functionPressButton>buttonName,imageTolerance


Code: Select all

  Let>buttonName=(INPUT BUTTON NAME)
  Let>imageTolerance=(INPUT IMAGE TOLERANCE)

  FindImagePos>%SCRIPT_DIR%\%buttonName%.bmp,SCREEN,%imageTolerance%,1,X,Y,Num,CCOEFF

  IF>Num<>1
    MessageModal>{%buttonName%+" # found:  "+%Num%}
  ELSE
    MouseMove>X_0,Y_0
    LClick
  ENDIF
Thx

User avatar
JRL
Automation Wizard
Posts: 3497
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Re: Functions?

Post by JRL » Mon Oct 09, 2017 1:27 am

Code: Select all

//Save as PressButton.scp

FindImagePos>%SCRIPT_DIR%\%buttonName%.bmp,SCREEN,%imageTolerance%,1,X,Y,Num,CCOEFF

IF>Num<>1
  MessageModal>%buttonName% found: %Num%
ELSE
  MouseMove>X_0,Y_0
  LClick
ENDIF

Code: Select all

//run as second script:

Let>buttonName=MyButton
Let>imageTolerance=0.7
Include>\path\PressButton.scp

Code: Select all

//Alternatively,  run from outside of Macro Scheduler perhaps from vb or from a command line:

\path\PressButton.scp /buttonName=MyButton /imageTolerance=0.7

Warren
Pro Scripter
Posts: 83
Joined: Sun Oct 08, 2017 11:57 pm

Re: Functions?

Post by Warren » Mon Oct 09, 2017 1:34 am

OK, thx. I get the first method. Actually, I was just reading up on includes in MS. I'd have to dig into the second example to see how or where it should be used.

Is there, then, no function shorthand so all the variable names don't need to be typed out each time along the lines of:

functionButton>1,2,3,4,5,6

vs.

Let>firstVariable=1
Let>secondVariable=2
Let>thirdVariable=3
Let>fourthVariable=4
Let>fifthVariable=5
Let>sixthVariable=6
INCLUDE>%SCRIPT_DIR%\Button.scp



Seems like the functionality (no pun intended) may exist be because that's how some of the built in functions work. For instance, when using WaitScreenImage, I just type all the values in separated by commas instead of a long list of let statements with full variable names.




This also brings up a related question. How would I have the function assign a default value for a particular case that is always used UNLESS I have assigned a particular value for the current call (and making sure it doesn't store my value from last time I called it)

So, if I call it the first time WITH imageTolerance defined:

Code: Select all

Let>buttonName=firstButton
Let>imageTolerance=1
INCLUDE>%SCRIPT_DIR%\Button.scp
Then it uses a value of 1 for imageTolerance, but if I call it again WITHOUT defining imageTolerance:

Code: Select all

Let>buttonName=secondButton
INCLUDE>%SCRIPT_DIR%\Button.scp
Then it goes back to using it's predefined value instead since it is not user defined on this call?

User avatar
JRL
Automation Wizard
Posts: 3497
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Re: Functions?

Post by JRL » Mon Oct 09, 2017 4:05 am

Is there, then, no function shorthand so all the variable names don't need to be typed out each time along the lines of:
Look up help for GoSub>. Turn the included script into a subroutine then after including it you could have a gosub> line and that line can contain parameters passed to the subroutine that is now part of the script via the include.

Code: Select all

//This is now the look of PressButton.scp (yes the script and sub can have the same name) 
SRT>PressButton
  FindImagePos>%SCRIPT_DIR%\%PressButton_var_1%.bmp,SCREEN,%PressButton_var_2%,1,X,Y,Num,CCOEFF
  IF>Num<>1
    MessageModal>%buttonName% found: %Num%
  ELSE
    MouseMove>X_0,Y_0
    LClick
  ENDIF
END>PressButton

Code: Select all

GoSub>PressButton,MyButton,0.7
How would I have the function assign a default value for a particular case that is always used UNLESS I have assigned a particular value for the current call (and making sure it doesn't store my value from last time I called it)
Add LOCALVARS=1 See help for SRT>
Also add a test to see if PressButton_var_2 has no value or an empty value (because you presented no value in the parameter position) assign a "default" value.

Code: Select all

Let>LOCALVARS=1

SRT>PressButton
  //test is case sensitive
  If>{(%PressButton_var_2%="PressButton_var_2")or(%PressButton_var_2%="")}
    Let>PressButton_var_2=0.5
  EndIf
  FindImagePos>%SCRIPT_DIR%\%PressButton_var_1%.bmp,SCREEN,%PressButton_var_2%,1,X,Y,Num,CCOEFF
  IF>Num<>1
    MessageModal>%buttonName% found: %Num%
  ELSE
    MouseMove>X_0,Y_0
    LClick
  ENDIF
END>PressButton

Warren
Pro Scripter
Posts: 83
Joined: Sun Oct 08, 2017 11:57 pm

Re: Functions?

Post by Warren » Mon Oct 09, 2017 4:17 am

Awesome, thanks.

That's exactly what I was looking for. I had stumbled across a few posts touching on those subjects, but found them confusing.

Much clearer now,and I know what to dig into further.

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts