Advertisement
C0BRA

coroutine test

Jan 4th, 2013
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.90 KB | None | 0 0
  1. function Test()
  2.     print("hello, ", CurTime())
  3.     local x = 0
  4.    
  5.     while true do
  6.         x = x + 1
  7.         print("world ", CurTime(x))
  8.         coroutine.yield()
  9.     end
  10. end
  11.  
  12. -- same output, doesn't matter if the env is is is not set.
  13. setfenv(Test, {
  14.     coroutine = table.Copy(coroutine),
  15.     print = print,
  16.     CurTime = CurTime
  17. })
  18.    
  19. local co = coroutine.create(Test)
  20.  
  21. concommand.Add("co_resume",
  22.     function ()
  23.         print("resuming at " .. CurTime())
  24.         print(coroutine.resume(co))
  25.     end
  26. )
  27.  
  28.  
  29. --[[
  30.  
  31. http://i.imgur.com/KEAiq.png
  32.  
  33. Running script autorun/server/co_test.lua...
  34. ] co_resume
  35. resuming at 839.35498046875
  36. hello,  1.1195346664694e-088
  37. world   1
  38. true
  39. ] co_resume
  40. resuming at 840.05999755859
  41. world   2
  42. true
  43. ] co_resume
  44. resuming at 840.88500976563
  45. world   3
  46. true
  47. ] lua_run print(CurTime(5))
  48. > print(CurTime(5))...
  49. 863.65496826172
  50. ] lua_run print(CurTime(5))
  51. > print(CurTime(5))...
  52. 864.19500732422
  53.  
  54.  
  55. ]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement