Page 1 of 1

Explanation of "defines.events.on_tick" behavior

Posted: Wed Nov 27, 2024 5:30 pm
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!

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

Posted: Wed Nov 27, 2024 5:35 pm
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})

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

Posted: Wed Nov 27, 2024 5:37 pm
by Kamikazimon
Thanks, that explains a lot!