Let>i=i+1 will not Increment

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
Aaron
Pro Scripter
Posts: 113
Joined: Mon Apr 09, 2007 1:35 am
Location: Wyoming

Let>i=i+1 will not Increment

Post by Aaron » Fri Apr 20, 2007 5:44 pm

I have spent hours rewriting the code every way I can imagine.

I just cant see the problem.

Please point out the issue.

Code: Select all

Let>i=0
 Goto>MAINLOOP
SRT>STARTEVAL

  GetCursorPos>X,Y


Let>i=i+1
If>i>2,FINISH

Let>TopLeft_X[1]=0
Let>BottomRight_X[1]=1024
Let>TopLeft_Y[1]=0
Let>BottomRight_Y[1]=768

Let>TopLeft_X[2]=0
Let>BottomRight_X[2]=1024
Let>TopLeft_Y[2]=0
Let>BottomRight_Y[2]=768

Let>myMessage[1]=This is Message One
Let>myMessage[2]=This is Message Two

Let>X_TopLeft=TopLeft_X[%i%]
Let>X_BottomRight=BottomRight_X[%i%]
Let>Y_TopLeft=TopLeft_Y[%i%]
Let>Y_BottomRight=BottomRight_Y[%i%]

Let>MessageVar=myMessage[%i%]

If>{((%X%>=%X_TopLeft%)AND(%X%<=%X_BottomRight%)AND(%Y%>=%Y_TopLeft%)AND(%Y%<=%Y_BottomRight%))}
  	       MessageModal>%MessageVar%
		 Goto>MAINLOOP
      ELSE
	     Goto>MAINLOOP
	  Endif


//======== Label MAINLOOP ==========

Label>MAINLOOP
   OnEvent>KEY_DOWN,VK1,0,STARTEVAL
  Wait>0.2
 Goto>MAINLOOP

End>STARTEVAL

//======== Sub FINISH ==========

SRT>FINISH

    MessageModal>Finished

 Goto>END

End>FINISH

//======== Label END ==========

Label>END
Aaron

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Fri Apr 20, 2007 8:36 pm

This is very confusing - you seem to be using a label to jump from outside a subroutine into the middle of a subroutine. This is unusual and undesirable. I can't follow the flow of your script at all!
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
jpuziano
Automation Wizard
Posts: 1085
Joined: Sat Oct 30, 2004 12:00 am

Post by jpuziano » Fri Apr 20, 2007 11:52 pm

Hi Aaron,

- Don't put OnEvent> lines inside any kind of loop, they only need to execute once and the event is setup... just put them at the top.

- Within your MAINLOOP, you need a Wait> to make sure the loop doesn't hog too much CPU and a test to exit the MAINLOOP when you want to exit the macro.

- Your subroutine STARTEVAL will run when the OnEvent> fires, in your case, when you hold the left mouse button down.

I re-wrote your macro and simplified it as much as I could while still trying to have it do *what I think* it is you were trying to do, see below.

One trick to use when writing a macro is not to start coding right away. Take the time to write out what it is you are trying to do in "pseudo-code" first like this:


//setup an event that will fire when the left mouse button is held down

//wait forever until event fires

//if event has fired 2 times already, exit

//when event fires (left mouse button down) capture the X,Y screen coordinates.

//compare them to some known coordinates and if the click was within the box, display a modal message, if outside, display nothing.

//the first click (if within the box coordinates) will display Msg#1

//the second click (if within the box coordinates) will display Msg#2

//immediately after the second click (whether Msgs were displayed or not) a Finished message will be displayed.

If you take the time to write out the pseudo-code, the code for the macro will practically write itself.

Here's my re-write, I added a Modal messagebox in there showing the X,Y coords of the click just for fun. Give this a try...

Code: Select all

OnEvent>KEY_DOWN,VK1,0,STARTEVAL

Let>i=0

Label>MAINLOOP
If>i=2,END
Wait>0.01
Goto>MAINLOOP

SRT>STARTEVAL
Let>i=i+1
GetCursorPos>X,Y

MDL>you clicked at X: %X%  Y: %Y%

Let>TopLeft_X[1]=0
Let>BottomRight_X[1]=1024
Let>TopLeft_Y[1]=0
Let>BottomRight_Y[1]=768

Let>TopLeft_X[2]=0
Let>BottomRight_X[2]=1024
Let>TopLeft_Y[2]=0
Let>BottomRight_Y[2]=768

Let>myMessage[1]=This is Message One
Let>myMessage[2]=This is Message Two

Let>X_TopLeft=TopLeft_X[%i%]
Let>X_BottomRight=BottomRight_X[%i%]
Let>Y_TopLeft=TopLeft_Y[%i%]
Let>Y_BottomRight=BottomRight_Y[%i%]

Let>MessageVar=myMessage[%i%]

If>{(%X%>=%X_TopLeft%) AND (%X%<X_BottomRight>=%Y_TopLeft%) AND (%Y%<Y_BottomRight>%MessageVar%
Endif
END>STARTEVAL

Label>END
MessageModal>Finished
Is this sort of what you were trying to do?

Take care and have a good weekend everyone...
jpuziano

Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post - :-)

Aaron
Pro Scripter
Posts: 113
Joined: Mon Apr 09, 2007 1:35 am
Location: Wyoming

Thank you for your time

Post by Aaron » Sat Apr 21, 2007 1:47 pm

This was confusing, mostly because I had to change the code inorder to try and show what I was trying to acomplish without putting down 4 pages of code.
jpuziano, your script shows how to use the loop which is what I was asking.
thank you very much for taking the time to write that out and reply.
Im still not sure how it works but will take a closer look latter today.

Thanks again
Aaron

Aaron
Pro Scripter
Posts: 113
Joined: Mon Apr 09, 2007 1:35 am
Location: Wyoming

Better example of what I need

Post by Aaron » Sun Apr 22, 2007 6:31 am

First of all I reduced the above example down to this below and it accomplishes the same thing without any of my variables.

Code: Select all

OnEvent>KEY_DOWN,VK1,0,STARTEVAL

Let>i=0

Label>MAINLOOP
If>i=2,END
Wait>0.01
Goto>MAINLOOP

SRT>STARTEVAL
Let>i=i+1
GetCursorPos>X,Y

MDL>you clicked at X: %X%  Y: %Y%

If>{(%X%>=%X_TopLeft%) AND (%X%<X_BottomRight>=%Y_TopLeft%) AND (%Y%<Y_BottomRight>%MessageVar%
Endif
END>STARTEVAL

Label>END
MessageModal>Finished


The problem is that I need to use my variables, Im not trying to get the screen x and y but trying to get a differant message box on each loop.

I have another example below. This one is much better than the first thanks to the help of jpuziano. It should show what im trying to do.


Code: Select all

OnEvent>KEY_DOWN,VK1,0,STARTEVAL

Let>i=0

Label>MAINLOOP
If>i=2,END
Wait>0.01
Goto>MAINLOOP

SRT>STARTEVAL
Let>i=i+1
GetCursorPos>X,Y

MDL>%MessageVar%

Let>TopLeft_X[1]=0
Let>BottomRight_X[1]=1024
Let>TopLeft_Y[1]=0
Let>BottomRight_Y[1]=768

Let>TopLeft_X[2]=0
Let>BottomRight_X[2]=1024
Let>TopLeft_Y[2]=0
Let>BottomRight_Y[2]=768

Let>myMessage[1]=This is Message One
Let>myMessage[2]=This is Message Two

Let>X_TopLeft=TopLeft_X[%i%]
Let>X_BottomRight=BottomRight_X[%i%]
Let>Y_TopLeft=TopLeft_Y[%i%]
Let>Y_BottomRight=BottomRight_Y[%i%]

Let>MessageVar=myMessage[%i%]

If>{((%X%>=%X_TopLeft%)AND(%X%<=%X_BottomRight%)AND(%Y%>=%Y_TopLeft%)AND(%Y%<=%Y_BottomRight%))}STARTEVAL
Endif

END>STARTEVAL

Label>END
MessageModal>Finished


The only problem now is that when I run it, I first get a message " %MessageVar% " then I get the correct message and then loss the last message. if this makes no sence still, please run the code and you will see the problem.

If I could get the correct message boxs without the " %MessageVar% " comming up first, then I should be able to accomplish all I need.

Thanks again
Aaron

Aaron
Pro Scripter
Posts: 113
Joined: Mon Apr 09, 2007 1:35 am
Location: Wyoming

Forgot to metion

Post by Aaron » Sun Apr 22, 2007 6:49 am

One more thing is that I only want to exit after the mouse has clicked twice WITHIN THE BOUNDS set, so if I clicked outside of the bounds twice it would not exit till after the forth click.

In the code I wrote I set the bound to my desktop just to simplify the testing of the Loop. once that works I will only want it to fire if clicked in a smaller areas of the screen

Thanks
Aaron

User avatar
jpuziano
Automation Wizard
Posts: 1085
Joined: Sat Oct 30, 2004 12:00 am

Post by jpuziano » Sun Apr 22, 2007 6:30 pm

Hi Aaron,

Lines of code that setup variables with values that will never change like your box boundary coordinates should not be within the MAINLOOP or the STARTEVAL subroutine... so I put them at the top. They run once and they're done.

I just realized the two boxes could overlap. I added some code to handle this possibility and changed the coordinates of the boxes so you could see the effect. Clicking in the lower half of the screen will be ignored completely. Box 1 covers the left top half and Box 2 the right top half of your screen.

The two boxes overlap slightly so if you click in the middle top half of the screen, you may trigger the messages for both Box1 and Box 2 as well as an extra message that appears only in this case.

If you will never define areas that overlap, you can simplify the code but if won't hurt to leave it in either. Try this...


Note: Code below re-posted 10 PM Sun Apr 22, 2007 with HTML, BBCode and Smilies disabled and this time code posted correctly...


[code]
OnEvent>KEY_DOWN,VK1,0,STARTEVAL

//Note: If box coordinates overlap and mouse is clicked where they
//overlap, both msgs are displayed but it only counts as one click.

//coordinates for box 1
Let>TopLeft_X[1]=0
Let>BottomRight_X[1]=600
Let>TopLeft_Y[1]=0
Let>BottomRight_Y[1]=384

//coordinates for box 2
Let>TopLeft_X[2]=500
Let>BottomRight_X[2]=1024
Let>TopLeft_Y[2]=0
Let>BottomRight_Y[2]=384

//Message text
Let>myMessage[1]=This is Message One (you clicked inside box 1)
Let>myMessage[2]=This is Message Two (you clicked inside box 2)

Let>valid_click_counter=0

Label>MAINLOOP
//Exit after handling 2 clicks within either box
If>valid_click_counter=2,END
Wait>0.01
Goto>MAINLOOP

SRT>STARTEVAL
GetCursorPos>X,Y
Let>valid_click_inside_box1=no
Let>valid_click_inside_box2=no

//Did we click within box 1? If so, display appropriate msg.
Let>i=1
Let>X_TopLeft=TopLeft_X[%i%]
Let>X_BottomRight=BottomRight_X[%i%]
Let>Y_TopLeft=TopLeft_Y[%i%]
Let>Y_BottomRight=BottomRight_Y[%i%]
If>{(%X%>=%X_TopLeft%) AND (%X%=%Y_TopLeft%) AND (%Y%MessageVar=myMessage[%i%]
MDL>%MessageVar%
Let>valid_click_inside_box1=yes
Endif

//Did we click within box 2? If so, display appropriate msg.
Let>i=2
Let>X_TopLeft=TopLeft_X[%i%]
Let>X_BottomRight=BottomRight_X[%i%]
Let>Y_TopLeft=TopLeft_Y[%i%]
Let>Y_BottomRight=BottomRight_Y[%i%]
If>{(%X%>=%X_TopLeft%) AND (%X%=%Y_TopLeft%) AND (%Y%MessageVar=myMessage[%i%]
MDL>%MessageVar%
Let>valid_click_inside_box2=yes
Endif

If>{(%valid_click_inside_box1%="yes") OR (%valid_click_inside_box2%="yes")}
Add>valid_click_counter,1
Endif

If>{(%valid_click_inside_box1%="yes") AND (%valid_click_inside_box2%="yes")}
MDL>Alert: You clicked in the area shared by both box1 and box2
Endif
END>STARTEVAL

Label>END
MessageModal>Finished
[/code]

Hopefully that gets you on the right track. Please let us know how it turns out and take care.
Last edited by jpuziano on Mon Apr 23, 2007 3:06 am, edited 2 times in total.
jpuziano

Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post - :-)

Aaron
Pro Scripter
Posts: 113
Joined: Mon Apr 09, 2007 1:35 am
Location: Wyoming

Post by Aaron » Sun Apr 22, 2007 8:02 pm

Your last code did not work at all

I can see what you were trying to do on the above code.

If you could, just tell me what is wrong with the code below. why is it not displaying the message box 1.

Thanks


Code: Select all

OnEvent>KEY_DOWN,VK1,0,STARTEVAL

Let>i=0

Label>MAINLOOP
If>i=2,END
Wait>0.01
Goto>MAINLOOP

SRT>STARTEVAL
Let>i=i+1
GetCursorPos>X,Y

MDL>%MessageVar%

Let>TopLeft_X[1]=0
Let>BottomRight_X[1]=1024
Let>TopLeft_Y[1]=0
Let>BottomRight_Y[1]=768

Let>TopLeft_X[2]=0
Let>BottomRight_X[2]=1024
Let>TopLeft_Y[2]=0
Let>BottomRight_Y[2]=768

Let>myMessage[1]=This is Message One
Let>myMessage[2]=This is Message Two

Let>X_TopLeft=TopLeft_X[%i%]
Let>X_BottomRight=BottomRight_X[%i%]
Let>Y_TopLeft=TopLeft_Y[%i%]
Let>Y_BottomRight=BottomRight_Y[%i%]

Let>MessageVar=myMessage[%i%]

If>{((%X%>=%X_TopLeft%)AND(%X%<=%X_BottomRight%)AND(%Y%>=%Y_TopLeft%)AND(%Y%<=%Y_BottomRight%))}STARTEVAL
Endif

END>STARTEVAL

Label>END
MessageModal>Finished
Aaron

User avatar
jpuziano
Automation Wizard
Posts: 1085
Joined: Sat Oct 30, 2004 12:00 am

Post by jpuziano » Mon Apr 23, 2007 2:52 am

Hi Aaron,
Aaron wrote:Your last code did not work at all
The code works great here... so I copied the code from that post, tried it and you're right, does nothing... then I found the code was messed up by this bulletin board. The really long lines with the AND's were chopped off.
Aaron wrote:I can see what you were trying to do on the above code.

If you could, just tell me what is wrong with the code below. why is it not displaying the message box 1.
When you first click, the STARTEVAL subroutine first runs, look at your code:
Aaron's code wrote:SRT>STARTEVAL
Let>i=i+1
GetCursorPos>X,Y

MDL>%MessageVar%
The MDL> command will show a message but you have not yet defined the variable "MessageVar" anywhere so all I would expect it to display is "%MessageVar%" as a literal text stiring. If you wanted your actual Message text to be displayed, you'd have to define it before you try to display it.

Another possible problem is the way you're using the variable i, it keeps incrementing with each click. Let's say you clicked twice outside both Box1 and Box2. Those clicks would be ignored. The next click would increment i to 3 but how would the following line then work?

Let>X_TopLeft=TopLeft_X[%i%]

You have these lines:
Let>TopLeft_X[1]=0
Let>TopLeft_X[2]=0

But you don't have
Let>TopLeft_X[3]=0

But don't add one... you don't need i to be incrementing each time you click.

I editted my previous post and clicked the options to Disable HTML, BBCode and Smilies in the post and this time it made it through unchanged. Please try the code I posted again, it should work fine this time and then let me know how it goes.
jpuziano

Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post - :-)

Aaron
Pro Scripter
Posts: 113
Joined: Mon Apr 09, 2007 1:35 am
Location: Wyoming

Works perfect

Post by Aaron » Mon Apr 23, 2007 3:22 am

Thank you jpuziano,

All your support is greatly appreciated.

The script works perfect. I will now take a closer look at it so as to understand all you did here..

Thanks again
Aaron

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