Use case
I want to have an entity perform an action, then perform that action again later after a (random) number of ticks. Multiple entities will be doing this, so will be possible that more than one will be doing this one the same tick. (To be very specific I want to give the cats in my mod the ability to swipe items off of belts every once in a while.)Current Solution
I maintain a table of desired future "happenings" (they are not actual events, just function calls), and then check whether the current tick is in there every tick, that will look something like this:Code: Select all
storage.future_happenings = {
SOME_TICK_NUMBER = {
happening_1 = [...],
happening_2 = [...]
},
ANOTHER_TICK = {
happening_3 = [...]
},
[...]
}
Code: Select all
script.on_event(defines.events.on_tick, function(event)
if storage.future_happenings[event.tick] then
for _, happening in pairs(storage.future_happenings[event.tick]) do
[...] -- Do the thing I want to do
end
storage.future_happenings[event.tick] = nil -- Remove it, since we're done with it
end
end