1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2024-12-12 10:04:40 +02:00

Implemented Thread.queue_action

This commit is contained in:
Valansch 2017-10-06 02:13:56 +02:00
parent 4494fc0a6c
commit adbb1b36be

View File

@ -21,6 +21,11 @@ global.next_async_callback_time = -1
local function on_tick() 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 if game.tick == global.next_async_callback_time then
for index, callback in pairs(global.callbacks) do for index, callback in pairs(global.callbacks) do
if game.tick == callback.time then if game.tick == callback.time then
@ -36,8 +41,8 @@ local function on_tick()
end end
end end
function Thread.set_timeout(sec, callback, params) function Thread.set_timeout_in_ticks(ticks, callback, params)
local time = game.tick + 60 * sec local time = game.tick + ticks
if global.next_async_callback_time == -1 or global.next_async_callback_time > time then if global.next_async_callback_time == -1 or global.next_async_callback_time > time then
global.next_async_callback_time = time global.next_async_callback_time = time
end end
@ -46,6 +51,16 @@ function Thread.set_timeout(sec, callback, params)
table.insert(global.callbacks, {time = time, callback = callback, params = params}) table.insert(global.callbacks, {time = time, callback = callback, params = params})
end 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) Event.register(defines.events.on_tick, on_tick)
return Thread return Thread