lua-users home
lua-l archive

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


I've mostly built pretty simple state machines with coros for things
like handling asynchronous events from mouse and keyboard.  Typically
they look like this (coded in the mail so pardon any mistakes):


while(true) do
    local event = co.yield()
    if(event == "down") then
        -- do something
       local res1 = calc1()

        event = co.yield()
        while(event == "drag") do
            -- do something else
           local res2 = calc2()
          if(res1 == res2) then
                generate_event()
          end
       end
   end
end

This is really basic, but shows the principle.  The thing I like about
coroutines is that I can setup calculations at local scope for use at
further states along the way as illustrated with res1 and res2 and I
don't have to construct on object or closures to do so.

wes