lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


If you have access to the system clock and an OnUpdate() function you can schedule events. In Warcraft I do something like this:

doAI()
scheduleEvent(GetTime() + 3, doAI2, param1, param2)

scheduleEvent() adds the event to a table of events ordered by execution time and OnUpdate() does any events whose time has come. Here's my code (written very early on when I was a beginner):

function ABGScheduleEvent(eventTime, functionName, a1, a2, a3, a4)
    local temp = 1
    local tempEvent = ABGEventList[1]
    while tempEvent[1] < eventTime do
        temp = temp + 1
        tempEvent = ABGEventList[temp]
    end
    table.insert(ABGEventList, temp, {eventTime, functionName, a1, a2, a3, a4})
end

function ABG_OnUpdate(timeSinceLast)
    -- do any scheduled events
    local tempEvent = ABGEventList[1] -- events are in chronological order
    local functionName
    local timeNow = GetTime()
    while timeNow > ABGEventList[1][1] do
        functionName = ABGEventList[1][2]
        functionName(ABGEventList[1][3], ABGEventList[1][4], ABGEventList[1][5], ABGEventList[1][6])
-- Now erase the event
        table.remove(ABGEventList, 1)
    end
end

It's a bit clunky and could be optimised a bit, but it works rock-solidly. I don't really understand metamethods and my knowledge of c-calls is very basic, so sorry if this isn't relevant to you.

On 13/03/2008, Joseph Saadé <jsaade@gmail.com> wrote:
> Thanks for the reply, i found several patches to make lua yield and
>  resume from inside a metamethod. I would see if it is that important
>  to the project or not.
>  
>
>  On 3/13/08, Alex Davies <alex.mania@iinet.net.au> wrote:
>  > I couldn't fully understand your post, but I think I understand the error
>  > message and "hook".
>  >
>  > Lua can only yield if the C call stack is empty. This means you cannot yield
>  > from inside a metamethod, eg __index, or from a nested C call, or, in this
>  > case, from a hook. If you really require this, you need a dedicated C stack
>  > for all lua threads, eg you need coco. http://luajit.org/coco.html
>  >
>  > This solution increases memory usage somewhat. Future versions of jit
>  > promise a better approach if IIRC.
>  >
>  > - Alex
>  >
>  > ----- Original Message -----
>  > From: "Joseph Saadé" <jsaade@gmail.com>
>  > To: <lua@bazar2.conectiva.com.br>
>  > Sent: Thursday, March 13, 2008 6:19 PM
>  > Subject: lua threads resuming and yielding from C
>  >
>  >
>  > > In my game project every entity has a different script running in a
>  > > new lua thread.
>  > > Every game loop, the engine calls the function OnUpdate located in every
>  > > script.
>  > > I want the ability to pause a script.
>  > > I implemented that by using a script manager that keeps track if a
>  > > script is yield.
>  > > so somthing like this would work:
>  > >
>  > > function OnUpdate(dt)
>  > >  doAI()
>  > >  Entity.Pause(3)
>  > >  doAI2()
>  > > end
>  > >
>  > > Entity.Pause is a C hook function that pauses the thread execution
>  > > using lua_yield.
>  > > the script engine waits 3 seconds and then calls lua_resume.
>  > >
>  > > The function OnUpdate is not called while is script is yielding.
>  > > my problem is that after the script first resumes, the doAI2() is not
>  > > called, nothing after the pause command is actually called. and the
>  > > next call to yield/resume will resolve in an error (yield returns:
>  > > "attempt to yield across metamethod/c-call boundary")
>  > >
>  > > so can't i yield a thread from inside a hook function and resume it's
>  > > execution normally?
>  >
>