[REQUEST] atStop

Discuss, ask for help, share ideas, give suggestions, read tutorials, and tell us about bugs you have found with MicroMacro in here.

Do not post RoM-Bot stuff here. There is a subforum for that.
Forum rules
This is a sub-forum for things specific to MicroMacro.

This is not the place to ask questions about the RoM bot, which uses MicroMacro. There is a difference.
Post Reply
Message
Author
luonline
Posts: 21
Joined: Thu May 15, 2008 1:57 pm

[REQUEST] atStop

#1 Post by luonline » Thu Sep 04, 2008 7:24 am

Hi!
Is it possible to add an atStop function (similar to atExit), which would be called when the script stops by pressing the stop key?
Thanks in advance.

User avatar
Administrator
Site Admin
Posts: 5331
Joined: Sat Jan 05, 2008 4:21 pm

Re: [REQUEST] atStop

#2 Post by Administrator » Thu Sep 04, 2008 3:57 pm

Sure. I can add this. But the question is, should an exit (CTRL+L) generate the stop event, as well?

luonline
Posts: 21
Joined: Thu May 15, 2008 1:57 pm

Re: [REQUEST] atStop

#3 Post by luonline » Fri Sep 05, 2008 6:32 am

Good question. i think so, because stoping a script before it exits makes sense to me. Furthermore, i think that atStop would be used for cleaning or logging purposes and it doesn't affect later events.
Just my opinion.

User avatar
Administrator
Site Admin
Posts: 5331
Joined: Sat Jan 05, 2008 4:21 pm

Re: [REQUEST] atStop

#4 Post by Administrator » Fri Sep 05, 2008 2:08 pm

I've added this as well as atResume(). The updated executable is attached (just the .exe, so don't delete anything yet).

luonline
Posts: 21
Joined: Thu May 15, 2008 1:57 pm

Re: [REQUEST] atStop

#5 Post by luonline » Tue Sep 09, 2008 3:33 pm

Hi! I tried:

Code: Select all

function stop_callback()
  printf("Stoping...\n");
end

atStop(stop_callback);
But I got an error:
"...of\Desktop\mutools\micromacro\scripts\mu-engnovo.lua:399: attempt to call global 'atStop' (a nil value)".
Could you give an example? Thank you.

User avatar
Administrator
Site Admin
Posts: 5331
Joined: Sat Jan 05, 2008 4:21 pm

Re: [REQUEST] atStop

#6 Post by Administrator » Tue Sep 09, 2008 6:32 pm

The function is atPause() instead of atStop().

luonline
Posts: 21
Joined: Thu May 15, 2008 1:57 pm

Re: [REQUEST] atStop

#7 Post by luonline » Wed Sep 10, 2008 7:37 am

Didn't work. I tried:

Code: Select all

startKey = key.VK_INSERT;
stopKey = key.VK_DELETE;

function pause_callback()
  printf("Pausing...\n");
end

atPause( pause_callback );

function main()
  while(true) do
    printf("Running...\n");
    yrest(100);  
  end
end

startMacro(main);
But, I still got: "...of\Desktop\mutools\micromacro\scripts\atPause.lua:7: attempt to call global 'atPause' (a nil value)"

Where is my mistake?

User avatar
Administrator
Site Admin
Posts: 5331
Joined: Sat Jan 05, 2008 4:21 pm

Re: [REQUEST] atStop

#8 Post by Administrator » Thu Sep 11, 2008 12:58 am

My mistake. I forgot about the changes to lib.lua. Here's a full update that includes the atPause() and atResume() functions.

luonline
Posts: 21
Joined: Thu May 15, 2008 1:57 pm

Re: [REQUEST] atStop

#9 Post by luonline » Thu Sep 11, 2008 7:25 am

Both files you've uploaded are the same. Not working yet.

User avatar
Administrator
Site Admin
Posts: 5331
Joined: Sat Jan 05, 2008 4:21 pm

Re: [REQUEST] atStop

#10 Post by Administrator » Thu Sep 11, 2008 4:17 pm

I attached the wrong file again. This one should work.

luonline
Posts: 21
Joined: Thu May 15, 2008 1:57 pm

Re: [REQUEST] atStop

#11 Post by luonline » Mon Sep 15, 2008 2:36 pm

Function yrest causes error inside the callback function... For example:

Code: Select all

startKey = key.VK_INSERT;
stopKey = key.VK_DELETE;

function pause_callback()
  yrest(100);
  printf("Pausing...\n");
end

atPause( pause_callback );

function main()
  while(true) do
    printf("Running...\n");
    yrest(100); 
  end
end

startMacro(main);
I got error: "...of\Desktop\micromacro\scripts\atPause.lua:5: attempt to yield across metamethod/C-call boundary".
Isn't it possible? Thank you.

User avatar
Administrator
Site Admin
Posts: 5331
Joined: Sat Jan 05, 2008 4:21 pm

Re: [REQUEST] atStop

#12 Post by Administrator » Mon Sep 15, 2008 9:11 pm

Nice find. Well, I figured out where the problem was. The good news is that it's "fixed." The bad news is that you still won't be able to yield.

See, the callback function is being called from the main thread (is not a coroutine). Because of this, you cannot yield it. So, I added a check into safeYield() to see if it is in the main thread, and do nothing if it is. The result is that you will still be resting, but not yielding (so other threads will not actually be getting CPU time here...which is actually a good thing for these types of events).

Here's the safeYield() patch:

Code: Select all

-- Runs a coroutine in a protected state, if available
function safeYield()
  -- make sure we're not trying to yield in the main thread
  -- do nothing.
  local co = coroutine.running();
  if( co == nil ) then
    return;
  end

  if( cocoAvailable ) then
    local status, err = pcall(coroutine.yield);

    if( status ~= true ) then
      __log_traceback();

      setTextColor(cli.yellow);
      error(err, 3);
    end
  else
    coroutine.yield();
  end
end

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests