tt

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
Nagarules
Junior Coder
Posts: 24
Joined: Tue Nov 21, 2006 9:33 pm

tt

Post by Nagarules » Thu Nov 23, 2006 7:04 pm

..
Last edited by Nagarules on Thu Jun 28, 2007 9:26 pm, edited 1 time in total.

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

Post by JRL » Fri Nov 24, 2006 6:30 am

Wish I could explain it but I don't understand either. Not knowing what you were trying to accomplish I took your script and pared it down a bit so I could see logic I could understand. I would expect the following script to bounce back and forth between the "bot" label and the "menu" label by pressing F4 and F5.

Instead, I find you can press either F5 and repeat the "menu" label via the "pause" subroutine or F4 and get to the "bot" label via the "resume" subroutine. After pressing F4 or F5 once, pressing either F4 or F5 a second time does nothing.

Code: Select all

//VK116 = F5
OnEvent>KEY_DOWN,VK116,0,pause
//VK115 = F4
OnEvent>KEY_DOWN,VK115,0,resume

Dialog>natb
   Caption=NaTB Naga)'s Advanced Talk Bot
   Width=406
   Height=207
   Top=CENTER
   Left=CENTER
   Max=0
   Min=0
   Close=0
   Resize=0
   Button=Close Bot,320,8,65,25,3
EndDialog>natb

Show>natb
Label>menu
Let>count=0
Label>cycle1
  add>count,1
  GetDialogAction>natb,r
  if>r=3,exit
  Wait>0.01
  message>menu cycle %count%
goto>cycle1

Label>bot
Let>count=0
Label>cycle2
  add>count,1
  GetDialogAction>natb,r
  if>r=3,exit
  Wait>0.01
  message>bot cycle %count%
goto>cycle2

SRT>resume
mdl>bot attained
Goto>bot
END>resume

SRT>pause
MDL>pause attained
Goto>menu
END>pause

Label>exit

Nagarules
Junior Coder
Posts: 24
Joined: Tue Nov 21, 2006 9:33 pm

Post by Nagarules » Fri Nov 24, 2006 9:40 am

After pressing F4 or F5 once, pressing either F4 or F5 a second time does nothing.

thats hte problem... I want my BOT to pause when i clcik F5 even if i already clicked F4 before... (meaning im in a current loop..) (meaning the value of r gos to 0 and it will go back to the menu where you can define your next value of r...)

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

Post by Marcus Tettmar » Fri Nov 24, 2006 10:14 am

The reason is that the event subroutine is never completed. You press F5, execution goes into the subroutine then jumps up to a loop and keeps looping. The event handler routine is never allowed to complete.

You should ensure that each event subroutine completes. Instead of jumping to another loop use variable flags to trigger behaviour in ONE main control loop and use the event subroutines to set the flags.

E.g. Here's how I would code your example:

Code: Select all

//VK116 = F5
OnEvent>KEY_DOWN,VK116,0,pause
//VK115 = F4
OnEvent>KEY_DOWN,VK115,0,resume

Dialog>natb
   Caption=NaTB Naga)'s Advanced Talk Bot
   Width=406
   Height=207
   Top=CENTER
   Left=CENTER
   Max=0
   Min=0
   Close=0
   Resize=0
   Button=Close Bot,320,8,65,25,3
   Edit=msEdit1,48,16,121,Resumed
EndDialog>natb

Let>b_paused=false
Show>natb
Label>main_loop

  if>b_paused=true

    Let>count=0
    Label>cycle1
	  Wait>0.01
	  GoSub>CheckForExit
      add>count,1
      Wait>0.01
      Let>natb.msEdit1=menu cycle %count%
	  ResetDialogAction>natb
	  if>b_paused=true
	    Goto>cycle1
	  endif
  else

  	Let>count=0
    Label>cycle2
	  Wait>0.01
	  GoSub>CheckForExit
      add>count,1
      Wait>0.01
      Let>natb.msEdit1=bot cycle %count%
	  ResetDialogAction>natb
      if>b_paused=false
	     Goto>cycle2
	  Endif
  endif

  Wait>0.2
Goto>main_loop


SRT>resume
  mdl>bot attained
  Let>b_paused=false
END>resume

SRT>pause
  MDL>pause attained
  Let>b_paused=true
END>pause

SRT>CheckForExit
  GetDialogAction>natb,r
  if>r=3,exit
END>CheckForExit

Label>exit
Note there is one main event loop - called main_loop. The pause key (F5) causes b_paused to be set to true, and F4 causes b_paused to be set to false. The main event loop determines what to do based on the value of b_paused. At the end of each sub loop we only keep looping if b_paused is still the appropriate value.
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
JRL
Automation Wizard
Posts: 3517
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Fri Nov 24, 2006 3:48 pm

I know I've used multiple OnEvent>Key_Downs successfully before and I often use variable flags, so I've apparently gotten it right accidentally in the past. I'll have to remember to look when I get back to work Monday.

From what I remembered from this thread, I was thinking that jumping out of a subroutine with a goto> ended the subroutine. But now I see at the very end of the thread, Marcus' last example uses flags. I didn't comprehend that it was mandatory.

Thanks for the clarification,
Dick

Speaking ofthis thread and others, I've noticed that a lot of old posted code has been rendered unusable. Is there anything that can be done to fix those old posts other than editing and reposting with HTML disabled?

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