Thread Tools Display Modes
11-08-07, 03:49 PM   #1
rokdar
A Murloc Raider
Join Date: Nov 2007
Posts: 7
Best way to sleep() ?

Greetings

Sense there doesn't seem to be any built in sleep for x function, I tried the following but had some issues of it never ending for some reason:

function sleep() {

start_time = GetTime() -- current system time in seconds
end_time = start_time + 2 -- 2 seconds later
while (end_time > start_time) do
-- sleeping
end
}

thats from memory as im not at my wow machine atm, any ideas? thanks
  Reply With Quote
11-08-07, 05:45 PM   #2
mulesh
A Chromatic Dragonspawn
 
mulesh's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2006
Posts: 193
You need to GetTime() again inside your While loop.

Let me explain a bit.

When the function runs, it gets time and stores it in Start_Time. Lets say its 5400.
Then it sets the End_Time to 5400 + 2
The loop starts and of course End_Time(5402) is more than Start_Time(5400), so it does your sleeping code and repeats the loop.

If your Sleep() function gets called again, it will do that same thing just with slightly higher numbers. End_Time will always be 2 more than Start_Time, always. You need to update Start_Time every time the loop runs or it will always evaluate true and do your sleeping code.

Something like this should work for you:
Code:
function Sleep()
    Start_Time = GetTime()
    End_Time = Start_Time + 2
   While (End_Time > Start_Time) do
        -- Sleeping code
        Start_Time = GetTime()
    end
end
__________________
"Don"t tase me bro!" ~ Andrew Meyer
  Reply With Quote
11-08-07, 07:41 PM   #3
Kaomie
A Scalebane Royal Guard
 
Kaomie's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2007
Posts: 438
Just to make sure, are you tying to implement a sleep function using an empty while loop?
Was your "--Sleeping" just a comment like I think it is or do you actually have some "Sleeping code" like Mulesh implies?
__________________
Kaomie
"WE LOTS OF PEOPLE FROM STRONG SERVER GUILDS" - Trade Channel
  Reply With Quote
11-08-07, 11:48 PM   #4
Eidolarr
An Aku'mai Servant
 
Eidolarr's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2006
Posts: 34
What exactly is the goal? The code posted here will outright lock your game client until a certain amount of time has passed. Such a function raises some ethical questions, as it can be used to simulate lag in very nasty ways by unscrupulous players; for example, you could run across your graveyard in the Warsong Gulch, hit 'sleep' for 10 seconds, then watch as the enemy team chases the ghost produced by your "lag" off the cliff.

Assuming that's what you want, this would be a function to "sleep" for a variable length of time:
Code:
sleep = function(time)
  local e = GetTime() + time
  repeat until e < GetTime() end
end
If you're looking for code to "delay" actions, which is far more useful in general, create a frame and look up OnUpdate.
  Reply With Quote
11-09-07, 02:04 PM   #5
rokdar
A Murloc Raider
Join Date: Nov 2007
Posts: 7
Yeah sorry guys for not being very clear, my goal was to just have a sleep function I could use to mod wait for a specified period of time on specific functions but when I used my sleep function it would cause the game to "lock up" for the duration.

Thanks for mentioning OnUpdate, I found this blurb on it and it seems to be what i want:

http://www.wowwiki.com/HOWTO:_Use_OnUpdate_correct
  Reply With Quote
11-09-07, 02:36 PM   #6
Eidolarr
An Aku'mai Servant
 
Eidolarr's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2006
Posts: 34
Well, within a function you can't have a "wait." There is no definition for sleep that will make this behave the way you describe without locking up your game client:
Code:
talkInTen = function()
  sleep(10)
  talk()
end
You can write a queue system like this:
Code:
local q = { }

function doIn(seconds, func, ...)
  local f = function()
    func(...)
  end
  q[f] = GetTime() + seconds
end

local f = CreateFrame("Frame", nil, UIParent)
f:SetScript("OnUpdate", function ()
  local t = GetTime()
  for k,v in pairs(q) do
    if t > v then
      k(); q[k] = nil;
    end
  end)
It generates some garbage (anonymous functions) and could be more efficient (if it were sorted you could significantly reduce your average case probe time), but hopefully the simplicity lets you see what it does. Try to use it as a base for writing your own.

Last edited by Eidolarr : 11-09-07 at 02:44 PM.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Best way to sleep() ?


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off