Advertisement
Tocuto

Lua Asyncio (soft example)

Apr 26th, 2019
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.43 KB | None | 0 0
  1. do
  2.     local _, nickname = pcall(nil)
  3.     tfm.get.room.owner = string.match(nickname, "(.-)%.")
  4. end
  5.  
  6. local await = coroutine.yield
  7. local loop = Loop()
  8. loop.max_runtime = 30 -- You can play a bit with these values
  9. loop.iteration_cooldown = 4000 -- This is the best one when you need to spam a bunch of tasks that are not that heavy!
  10. loop.accept_faster = true -- It runs it all as fast as it cans
  11. -- These tasks are very soft so this configuration will be one of the bests for it!
  12.  
  13. -- You can spam the spacebar, as an example of task spamming
  14.  
  15. function keyboardTask(player, key, down, x, y)
  16.     print("Key pressed, executing some code!! Current task runtime is " .. loop.current_task.real_runtime() .. "ms")
  17.     loop.sleep(30) -- We use loop.sleep
  18.     print("Half of the code has been executed. Executing the other half! Runtime is " .. loop.current_task.runtime .. "ms")
  19.     loop.sleep()
  20.     print("Alright, ended running the code! The total runtime is " .. loop.current_task.real_runtime() .. "ms")
  21. end
  22.  
  23. function eventKeyboard(...)
  24.     local start = os.time()
  25.  
  26.     loop.add_task(Task(keyboardTask, ...))
  27.  
  28.     loop.run_tasks(start) -- We can call this function from any event, and it is better
  29.     -- because it will run faster when it cans!
  30. end
  31.  
  32. function eventLoop()
  33.     loop.run_tasks() -- We don't call it as eventLoop = loop.run_tasks because the eventLoop has arguments, and it will just break the system
  34. end
  35.  
  36. system.bindKeyboard(tfm.get.room.owner, 32, true, true)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement