Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 06-13-2005, 11:06 AM   #1 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,532
sde is on a distinguished road
Sleeping with Lua ;)

Anyone use Lua? more specifically, anyone know how to make a Lua scrit sleep?

It's just for fun, but world of warcraft supports Lua scripting in its macros and i want to write something that requires the script to pause for a few seconds.
__________________
Mike
sde is offline   Reply With Quote
Old 06-13-2005, 02:50 PM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
Could this be of any use ?
Code:
int sleep_glue(lua_State *L)
{
  // We need 1 parameter, no more, no less
  if(lua_gettop(L) != 1)
  {
    lua_pushstring(L, "sleep: parameter mismatch");
    lua_error(L);
  }

  // Execute the sleep
  DWORD Duration = (DWORD) lua_tonumber(L, 1)
  Sleep(Duration);

  // No results
  return 0;
}
It's a hack, by using the internal sleep function in windows...
It will sleep for the given value in seconds..
A better way, is to constantly check your script, to see if anything has happened..
Code:
int sleep_glue(lua_State *L)
{
  // We need 1 parameter, no more, no less
  if(lua_gettop(L) != 1)
  {
    lua_pushstring(L, "sleep: parameter mismatch");
    lua_error(L);
  }

  // Execute the sleep (per 100ms)
  DWORD Duration = (DWORD) lua_tonumber(L, 1);
  while(Duration > 100)
  {
    // Abort if necessary...
    if(bAbortScript)
      return 0;

    // Sleep 100ms
    Sleep(100);
    Duration = Duration - 100;
  }

  // Execute the remaing part
  // Don't execute it if the script is being aborted...
  if(!bAbortScript)
    Sleep(Duration);

  // No results
  return 0;
}
Where we check the script for action every 100 milliseconds..

But it is a bad hack, since it will consume CPU time like fire will consume gassoline..
You might wanna use windows buildin WaitForSingleObject like this:
Code:
volatile HANDLE hCloseProgram; // an event handle
hCloseProgram = CreateEvent(NULL, TRUE, FALSE, NULL);
int sleep_glue(lua_State *L)
{
  // We need 1 parameter, no more, no less
  if(lua_gettop(L) != 1)
  {
    lua_pushstring(L, "sleep: parameter mismatch");
    lua_error(L);
  }

  // Wait for X milliseconds
  // Or return immediatly if hCloseProgram is signaled
  DWORD Duration = (DWORD) lua_tonumber(L, 1);
  WaitForSingleObject(hCloseProgram, Duration);

  // No results
  return 0;
}
When you want to cancel the WaitForSingleObject, you'd use:
Code:
SetEvent(hCloseProgram);
Of course, event objects still don't abort the LUA script executioner. You also need to modify your hook function to teach it how to use the event object :
Code:
void HookRoutine(lua_State *L, lua_Debug *ar)
{
  if(ar->event == LUA_HOOKLINE)
  {
    if(WaitForSingleObject(hCloseProgram, 0) == WAIT_OBJECT_0)
    {
      // Ok, let's quit the program
      lua_pushstring(L, "HookRoutine: Abort requested!");
      lua_error(L);
    }
  }
}
But the be honest, that's not a method I would recommend, the hook routine will be called a lot and calling every time the WaitForSingleObject function causes it to slow down and thus slowing down your scripts! A better method would be a combination of both a global variable to allow aborting of the script engine and an event to allow aborting of functions that suspend the system.
you can for example set only the LUA hook routine when you need to close your program. ie:
Code:
oid AbortRoutine(lua_State *L, lua_Debug *ar)
{
  if(ar->event == LUA_HOOKLINE)
  {
    // Ok, let's abort the script
    lua_pushstring(L, "AbortRoutine has been called!");
    lua_error(L);
   }
}

void OnQuit()
{
  // Set a hook for LUA_MASKLINE
  lua_sethook(L, AbortRoutine, LUA_MASKLINE, 0);
  
  // Tell the wait functions that we're quiting
  SetEvent(hCloseProgram);
}
hmm.. guess I can add Lua to my known languages aswell....

//edit
I would have added an example more, but just found out I've once more become an uncle, so I'll guess I'll have a beer instead and celebrate..
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001

Last edited by redhead; 06-13-2005 at 03:04 PM. Reason: Time to celebrate
redhead is offline   Reply With Quote
Old 06-13-2005, 03:25 PM   #3 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,532
sde is on a distinguished road
hah thanks a lot =) .. i'll give it a try later. my macros can be 256 characters long and they de-activate some of the lua functionality so add-on writers can't compomise systems .. so i wll cross my fingers and give it a try.

that is funny ,, .. i really didn't expect anyone to know about lua here =) that's cool!
__________________
Mike
sde is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Loving Lua MrGreen All Other Coding Languages 0 02-18-2005 11:21 AM


All times are GMT -8. The time now is 03:40 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0 RC8 ©2007, Crawlability, Inc.





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting