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.
Add shift to on_nth_tick
Re: Add shift to on_nth_tick
I think in this case is better use the event on_tick and inside the event use someting like:
And according to Factorio documentation https://lua-api.factorio.com/latest/Lua ... n_nth_tick:
Code: Select all
if event.tick % global.wanted_tick == 0 then...
Don't specify any way to do that or use arguments given to the function for this scope.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.
Re: Add shift to on_nth_tick
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.
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.
I have mods! I guess!
Link
Link
Re: Add shift to on_nth_tick
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
On every thousandth tick, register a function to tick + 1.
I have mods! I guess!
Link
Link
Re: Add shift to on_nth_tick
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.
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.
I have mods! I guess!
Link
Link