on-tick-n module

Schedule tasks to be executed later.

Usage

local on_tick_n = require("__flib__.on-tick-n")

Functions

init() Initialize the module's script data table.
retrieve(tick) Retrieve the tasks for the given tick.
add(tick, task) Add a task to execute on the given tick.
remove(ident) Remove a scheduled task.

Concepts

TaskIdent A unique identifier for a previously added task, used in on-tick-n.remove.
Tasks An table of tasks.

Functions

# init()

Initialize the module's script data table.

Must be called at the beginning of on_init. Can also be used to delete all current tasks.

# retrieve(tick)

Retrieve the tasks for the given tick.

Must be called during on_tick.

Parameters: Returns:
  • (Tasks or nil) The tasks to execute on this tick, or nil.
# add(tick, task)

Add a task to execute on the given tick.

Parameters:
  • tick : (number)
  • task : (any) The data representing this task. This can be anything that is not a function.
Returns:
  • (TaskIdent) An identifier for the task. Save this if you might remove the task before execution.
# remove(ident)

Remove a scheduled task.

Parameters:

Concepts

# TaskIdent

A unique identifier for a previously added task, used in on-tick-n.remove.

  • tick : (number) The tick this task is scheduled for.
  • index : (number) The tasks' index in the tick's Tasks table.
# Tasks

An table of tasks.

Each task can be anything that is not a function, as specified in on-tick-n.add.

This is not an array, there may be gaps. Always use pairs to iterate this table.

Usage:
event.on_tick(function(e)
  for _, task in pairs(on_tick_n.retrieve(e.tick) or {}) do
    if task == "say_hi" then
      game.print("Hello there!")
    elseif task == "order_66" then
      for _, player in pairs(game.players) do
        player.die()
      end
    end
  end
end)