LuaBootstrap

class LuaBootstrap - sort
on_init(f) Register a callback to be run on mod init.
on_load(f) Register a function to be run on module load.
on_configuration_changed(f) Register a function to be run when mod configuration changes.
on_event(event, f) Register a handler to run on event or events.
generate_event_name() → uint Generate a new, unique event ID.
get_event_handler(event) Find the event handler for an event.
raise_event(event, table) Raise an event.

Entry point for registering event handlers. It is accessible through the global object named script.

on_init(f)

Register a callback to be run on mod init. This is called once when a new save game is created or once when a save file is loaded that previously didn't contain the mod. This is always called before other event handlers and is meant for setting up initial values that a mod will use for its lifetime.

Parameters
f :: function(): The function to call. Passing nil will unregister the handler.
on_load(f)

Register a function to be run on module load. This is called every time a save file is loaded *except* for the instance when a mod is loaded into a save file that it previously wasn't part of. Additionally this is called when connecting to any other game in a multiplayer session and should never change the game state.

This is meant for 3 specific reasons and only 3:

  • re-register conditional event handlers
  • re-setup meta tables
  • create local references to tables stored in the global table

In all other instances the LuaBootstrap::on_init, LuaBootstrap::on_configuration_changed or migration scripts should be used. Doing any other logic when loading a save file can break the replay and cause desync issues if the mod is used in multiplayer.

Parameters
f :: function(): The function to call. Passing nil will unregister the handler.
on_configuration_changed(f)

Register a function to be run when mod configuration changes. This is called any time the game version changes, prototypes change, startup mod settings change, and any time mod versions change including adding or removing mods.

Parameters
f :: function(ConfigurationChangedData): The handler for this event. Passing nil will unregister the handler.
on_event(event, f)

Register a handler to run on event or events.

Parameters
event :: defines.events or array of defines.events or string: The events or custom-input name to invoke the handler on
f :: function(Event): The handler to run. Passing nil will unregister the handler. The handler will receive a table that contains the key name (of type defines.events) specifying the name of the event it was called to handle, and tick that specifies when the event was created. This table will also contain other fields, depending on the type of the event. See the list of Factorio events for a listing of these additional fields.
generate_event_name() → uint

Generate a new, unique event ID.

Return value
The new ID
get_event_handler(event)

Find the event handler for an event.

Parameters
event :: uint: The event identifier to get a handler for
Return value
Reference to the function currently registered as the handler.
raise_event(event, table)

Raise an event.

Parameters
event :: uint: ID of the event to raise
table: Table with extra data. This table will be passed to the event handler.