Just a small update, I have finally finished writing a system that allows dfhack.timeout() calls to persist between save/load. It is amazingly simple, with only two parts
local persistTable = require 'persist-table'
function persistantDelay(ticks,script)
local currentTick = 1200*28*3*4*df.global.cur_year + df.global.cur_year_tick
local runTick = currentTick + ticks
local persistDelay = persistTable.GlobalTable.roses.PersistTable
local number = #persistDelay._children
persistDelay[tostring(number+1)] = {}
persistDelay[tostring(number+1)].Tick = tostring(runTick)
persistDelay[tostring(number+1)].Script = script
dfhack.timeout(ticks,'ticks',function () dfhack.run_script(script) end)
end
return persistantDelay
local split = require('split')
local utils = require 'utils'
local persistTable = require 'persist-table'
persistTable.GlobalTable.roses.PersistTable = persistTable.GlobalTable.roses.PersistTable or {}
local persistDelay = require 'persist-delay'
local delayTable = persistTable.GlobalTable.roses.PersistTable
for _,i in pairs(delayTable._children) do
local delay = delayTable[i]
local currentTick = 1200*28*3*4*df.global.cur_year + df.global.cur_year_tick
if currentTick >= tonumber(delay.Tick) then
delay = nil
else
local ticks = delay.Tick-currentTick
dfhack.timeout(ticks,'ticks',function () dfhack.run_script(delay.Script) end)
end
end
Then all you need to do to run it in your own script is to use
delay = require 'persist-delay'
delay(ticks,script)
Where ticks is the number of ticks you want the game to wait, and script is the command line script you want to run.
And to make sure they are correctly queued up on load, just put persist-delay in onLoad.init
Note that this isn't quite the same as dfhack.timeout since it currently only supports in game tick delays, and will only run scripts through dfhack.run_script(), not arbitrary functions.
@expwnent, I think something like this would be helpful to put in the main repository, I'm sure some changes could be made to make it more useful and pretty.