Explanation of "defines.events.on_tick" behavior

Place to get help with not working mods / modding interface.
Kamikazimon
Burner Inserter
Burner Inserter
Posts: 5
Joined: Fri Oct 25, 2024 4:01 pm
Contact:

Explanation of "defines.events.on_tick" behavior

Post by Kamikazimon »

Hello,

I am currently trying to understand the trigger logic behind the "defines.events.on_tick" event. I have the following code implemented in control.lua:

Code: Select all

update_ticks = {15, 30, 45, 60}

script.on_event(defines.events.on_tick, function(event)
    update(event)
end)

function update(event)
    for i=1,#update_ticks,1 do
        if event.tick % update_ticks[i] == 0 then
            game.print("updated at tick ".. event.tick)
        end
    end
end

Everything is as expected; I can see the game printing the message 4 times per second (on each tick, where the remainder of the the current tick divided by either 15, 30, 45, or 60 equals 0). No the puzzling part: I change the code of the update function to this:

Code: Select all

function update(event)
    for i=1,#update_ticks,1 do
        if event.tick % update_ticks[i] == 0 then
            game.print("updated at tick ") -- I simply removed the reference to event.tick here
        end
    end
end
Now the code gets called once per second; not (as I expect) 4 times per second. I can't wrap my head around why that is... can someone shed some light here?
Thanks!
User avatar
boskid
Factorio Staff
Factorio Staff
Posts: 3393
Joined: Thu Dec 14, 2017 6:56 pm
Contact:

Re: Explanation of "defines.events.on_tick" behavior

Post by boskid »

That is a feature of the print which skips printing a text that was already sent within last 60 ticks. If you want those messages to be printed regardless of same content, you may need to use PrintSettings::skip with a value of defines.print_skip.never:

Code: Select all

game.print("updated at tick ", {skip=defines.print_skip.never})
Kamikazimon
Burner Inserter
Burner Inserter
Posts: 5
Joined: Fri Oct 25, 2024 4:01 pm
Contact:

Re: Explanation of "defines.events.on_tick" behavior

Post by Kamikazimon »

Thanks, that explains a lot!
Post Reply

Return to “Modding help”