Page 1 of 1
Add shift to on_nth_tick
Posted: Sun Dec 15, 2019 12:32 pm
by darkfrei
Hi all!
Is it possible to add shift to on_nth_tick?
For example I need the event on 100th, 1100th, 2100th ticks, but not between them. So I need the event every nth+const ticks.
For example the event every midday or every midnight.
Re: Add shift to on_nth_tick
Posted: Sun Dec 15, 2019 4:02 pm
by Linver
I think in this case is better use the event on_tick and inside the event use someting like:
Code: Select all
if event.tick % global.wanted_tick == 0 then...
And according to Factorio documentation
https://lua-api.factorio.com/latest/Lua ... n_nth_tick:
on_nth_tick(tick, f)
Register a handler to run every nth tick(s). When the game is on tick 0 it will trigger all registered handlers.
Parameters
tick :: uint or array of uint: The nth-tick(s) to invoke the handler on. Passing nil as the only parameter will unregister all nth-tick handlers.
f :: function(NthTickEvent): The handler to run. Passing nil will unregister the handler for the provided ticks.
Don't specify any way to do that or use arguments given to the function for this scope.
Re: Add shift to on_nth_tick
Posted: Mon Dec 16, 2019 7:39 am
by Honktown
I recently asked about this. There's no way do every n ticks after a starting point, or tick+offset (a bit of the opposite what you're asking). For simplicity/performance reasons the on_nth_tick handler is just "if (tick % modulo_n) == 0 do <these things> end" in Lua terms (it's in C++, but same difference).
on_nth_tick handler can have multiple nth_tick functions, which isn't explicit in the api. You can do a on_nth_tick 1000, and have it assign a function to do every nth+100 ticks, and then at tick nth+100 the function runs and de-registers itself. It'd be relatively simple because events always provide event.tick, so you can do on_nth_tick(n, nil) after you finished doing your business.
Edit edit: hmmmm it sounds like you can have multiple functions assigned to a single nth-tick, which makes sense, but I can't trust anything these days.
Re: Add shift to on_nth_tick
Posted: Mon Dec 16, 2019 8:45 pm
by darkfrei
And if I need the event on 1001, 2001, 3001 ticks, then I need to check it every tick?
Re: Add shift to on_nth_tick
Posted: Mon Dec 16, 2019 8:58 pm
by Honktown
darkfrei wrote: Mon Dec 16, 2019 8:45 pm
And if I need the event on 1001, 2001, 3001 ticks, then I need to check it every tick?
On every thousandth tick, register a function to tick + 1.
Re: Add shift to on_nth_tick
Posted: Mon Dec 16, 2019 9:07 pm
by Honktown
I should add, if you're not DOING anything each tick, the cost for checking it yourself every tick is not noticeable. I do that because I was a bit sloppy in my initial design for a mod.
You can either do if last_tick_we_did_stuff + offset <= current tick, or use next_tick_to_do_stuff <= event.tick, for the on_tick() function.