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 initialization. |
on_load(f) | Register a function to be run on save load. |
on_configuration_changed(f) | Register a function to be run when mod configuration changes. |
on_event(event, f, filters) | Register a handler to run on the specified event(s). |
on_nth_tick(tick, f) | Register a handler to run every nth-tick(s). |
register_on_entity_destroyed(entity) → uint64 | Registers an entity so that after it's destroyed, on_entity_destroyed is called. |
generate_event_name() → uint | Generate a new, unique event ID that can be used to raise custom events with LuaBootstrap::raise_event. |
get_event_handler(event) → function(EventData) | Find the event handler for an event. |
get_event_order() → string | Gets the mod event order as a string. |
set_event_filter(event, filters) | Sets the filters for the given event. |
get_event_filter(event) → array[EventFilter] | Gets the filters for the given event. |
raise_event(event, data) | Raise an event. |
raise_console_chat{player_index = …, message = …} | Raises on_console_chat with the given parameters. |
raise_player_crafted_item{item_stack = …, player_index = …, recipe = …} | Raises on_player_crafted_item with the given parameters. |
raise_player_fast_transferred{player_index = …, entity = …, from_player = …} | Raises on_player_fast_transferred with the given parameters. |
raise_biter_base_built{entity = …} | Raises on_biter_base_built with the given parameters. |
raise_market_item_purchased{player_index = …, market = …, offer_index = …, count = …} | Raises on_market_item_purchased with the given parameters. |
raise_script_built{entity = …} | Raises script_raised_built with the given parameters. |
raise_script_destroy{entity = …} | Raises script_raised_destroy with the given parameters. |
raise_script_revive{entity = …, tags = …} | Raises script_raised_revive with the given parameters. |
raise_script_set_tiles{surface_index = …, tiles = …} | Raises script_raised_set_tiles with the given parameters. |
mod_name :: string [R] | The name of the mod from the environment this is used in. |
level :: table [R] | Information about the currently running scenario/campaign/tutorial. |
active_mods :: dictionary[string → string] [R] | A dictionary listing the names of all currently active mods and mapping them to their version. |
object_name :: string [R] | This object's name. |
Register a callback to be run on mod initialization.
This is only called when a new save game is created or when a save file is loaded that previously didn't contain
the mod. During it, the mod gets the chance to set up initial values that it will use for its lifetime. It has
full access to LuaGameScript and the global
table and can change anything about them that it deems
appropriate. No other events will be raised for the mod until it has finished this step.
nil
will unregister it.players
table in global
for later use.
script.on_init(function()
global.players = {}
end)
Register a function to be run on save load.
This is only called for mods that have been part of the save previously, or for players connecting to
a running multiplayer session. It gives the mod the opportunity to do some very specific actions, should it need
to. Doing anything other than these three will lead to desyncs, which breaks multiplayer and replay functionality.
Access to LuaGameScript and LuaRendering is not available. The global
table can be accessed and
is safe to read from, but not write to.
The only legitimate uses of this event are these three:
For all other purposes, LuaBootstrap::on_init, LuaBootstrap::on_configuration_changed or migration scripts should be used instead.
nil
will unregister it. Register a function to be run when mod configuration changes.
This is called when the game version or any mod version changes; when any mod is added or removed; or when prototypes
or startup mod settings have changed. It allows the mod to make any changes it deems appropriate to both the
data structures in its global
table or to the game state through LuaGameScript.
nil
will unregister it.Register a handler to run on the specified event(s). Each mod can only register once for every event, as any additional registration will overwrite the previous one. This holds true even if different filters are used for subsequent registrations.
script.on_event(defines.events.on_tick,
function(event) game.print(event.tick) end)
"fast-inserter"
is built.
script.on_event(defines.events.on_built_entity,
function(event) game.print("Gotta go fast!") end,
{{filter = "name", name = "fast-inserter"}})
Register a handler to run every nth-tick(s). When the game is on tick 0 it will trigger all registered handlers.
nil
as the only
parameter will unregister all nth-tick handlers.nil
will unregister it for
the provided nth-tick(s).Registers an entity so that after it's destroyed, on_entity_destroyed is called. Once an entity is registered, it stays registered until it is actually destroyed, even through save/load cycles. The registration is global across all mods, meaning once one mod registers an entity, all mods listening to on_entity_destroyed will receive the event when it is destroyed. Registering the same entity multiple times will still only fire the destruction event once, and will return the same registration number.
Generate a new, unique event ID that can be used to raise custom events with LuaBootstrap::raise_event.
Find the event handler for an event.
Gets the mod event order as a string.
Sets the filters for the given event. The filters are only retained when set after the actual event registration, because registering for an event with different or no filters will overwrite previously set ones.
script.set_event_filter(defines.events.on_marked_for_deconstruction, {{filter = "ghost", invert = true}})
unit
or a unit-spawner
is built.
script.set_event_filter(defines.events.on_built_entity, {{filter = "type", type = "unit"}, {filter = "type", type = "unit-spawner"}})
rail
is damaged by an acid
attack.
script.set_event_filter(defines.events.on_entity_damaged, {{filter = "rail"}, {filter = "damage-type", type = "acid", mode = "and"}})
Gets the filters for the given event.
nil
if none are defined.Raise an event. Only events generated with LuaBootstrap::generate_event_name and the following can be raised:
local data = {player_index = 1, message = "Hello friends!"}
script.raise_event(defines.events.on_console_chat, data)
Raises on_console_chat with the given parameters.
Raises on_player_crafted_item with the given parameters.
Raises on_player_fast_transferred with the given parameters.
Raises on_biter_base_built with the given parameters.
Raises on_market_item_purchased with the given parameters.
Raises script_raised_built with the given parameters.
Raises script_raised_destroy with the given parameters.
Raises script_raised_revive with the given parameters.
Raises script_raised_set_tiles with the given parameters.
The name of the mod from the environment this is used in.
Information about the currently running scenario/campaign/tutorial.
Table with the following fields:
A dictionary listing the names of all currently active mods and mapping them to their version.
for name, version in pairs(script.active_mods) do
game.print(name .. " version " .. version)
end
This object's name.