diff --git a/locale/utils/Thread.lua b/locale/utils/Thread.lua index 2656a186..9728a451 100644 --- a/locale/utils/Thread.lua +++ b/locale/utils/Thread.lua @@ -21,6 +21,11 @@ global.next_async_callback_time = -1 local function on_tick() + if global.actions_queue[1] then + local callback = global.actions_queue[1] + pcall(callback.action, callback.params) + table.remove(global.actions_queue, 1) + end if game.tick == global.next_async_callback_time then for index, callback in pairs(global.callbacks) do if game.tick == callback.time then @@ -36,8 +41,8 @@ local function on_tick() end end -function Thread.set_timeout(sec, callback, params) - local time = game.tick + 60 * sec +function Thread.set_timeout_in_ticks(ticks, callback, params) + local time = game.tick + ticks if global.next_async_callback_time == -1 or global.next_async_callback_time > time then global.next_async_callback_time = time end @@ -46,6 +51,16 @@ function Thread.set_timeout(sec, callback, params) table.insert(global.callbacks, {time = time, callback = callback, params = params}) end +function Thread.set_timeout(sec, callback, params) + Thread.set_timeout_in_ticks(60 * sec, callback, params) +end + +global.actions_queue = {} +function Thread.queue_action(action, params) + + table.insert(global.actions_queue, {action = action, params = params}) +end + Event.register(defines.events.on_tick, on_tick) return Thread