Page 1 of 1
Is it possible to run a script over multiple ticks?
Posted: Wed Oct 23, 2019 2:40 pm
by Romner_set
For example :
Code: Select all
script.on_event(defines.events.on_tick, function(event)
run_every_5_seconds
if something do
do stuff
wait 1 tick
do more stuff
end
wait 1 tick
do more stuff
etc.
end
end)
This is useful because I want to run kinda heavy code every 5 (maybe more) secs, so it is better to have a drop in UPS for a few seconds than lag the whole game for a second or two.
Re: Is it possible to run a script over multiple ticks?
Posted: Wed Oct 23, 2019 3:05 pm
by ratchetfreak
You can use a coroutine:
Code: Select all
local mycoroutine = coroutine.create (function ()
run_every_5_seconds
if something do
do stuff
coroutine.yield()
do more stuff
end
coroutine.yield()
do more stuff
etc.
end
end )
script.on_event(defines.events.on_tick, function(event)
coroutine.resume(mycoroutine)
end)
Re: Is it possible to run a script over multiple ticks?
Posted: Wed Oct 23, 2019 3:57 pm
by Romner_set
ratchetfreak wrote: ↑Wed Oct 23, 2019 3:05 pm
You can use a coroutine
Oh! For now I found a way to make the code much less performance heavy, so this isn't needed. But thanks anyway!
Re: Is it possible to run a script over multiple ticks?
Posted: Wed Oct 23, 2019 3:58 pm
by eradicator
ratchetfreak wrote: ↑Wed Oct 23, 2019 3:05 pm
You can use a coroutine:
Factorio engine does not suppot coroutine.
If you just want to iterate a table see
this thread.
Re: Is it possible to run a script over multiple ticks?
Posted: Wed Oct 23, 2019 8:33 pm
by darkfrei