Add shift to on_nth_tick

Place to get help with not working mods / modding interface.
Post Reply
User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2903
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Add shift to on_nth_tick

Post 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.

User avatar
Linver
Fast Inserter
Fast Inserter
Posts: 158
Joined: Wed Jan 09, 2019 2:28 pm
Contact:

Re: Add shift to on_nth_tick

Post 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.

Honktown
Smart Inserter
Smart Inserter
Posts: 1025
Joined: Thu Oct 03, 2019 7:10 am
Contact:

Re: Add shift to on_nth_tick

Post 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.
I have mods! I guess!
Link

User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2903
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: Add shift to on_nth_tick

Post by darkfrei »

And if I need the event on 1001, 2001, 3001 ticks, then I need to check it every tick?

Honktown
Smart Inserter
Smart Inserter
Posts: 1025
Joined: Thu Oct 03, 2019 7:10 am
Contact:

Re: Add shift to on_nth_tick

Post 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.
I have mods! I guess!
Link

Honktown
Smart Inserter
Smart Inserter
Posts: 1025
Joined: Thu Oct 03, 2019 7:10 am
Contact:

Re: Add shift to on_nth_tick

Post 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.
I have mods! I guess!
Link

Post Reply

Return to “Modding help”