Entry point for registering event handlers. It is accessible through the global object named script.
stringunknownfalseThe name of the mod from the environment this is used in.
{ campaign_name?: string, is_simulation?: boolean, is_tutorial?: boolean, level_name: string, mod_name?: string }unknownfalseInformation about the currently running scenario/campaign/tutorial.
Dict<string, string>unknownfalseA dictionary listing the names of all currently active mods and mapping them to their version.
{ expansion_shaders: boolean, freezing: boolean, quality: boolean, rail_bridges: boolean, segmented_units: boolean, space_travel: boolean, spoiling: boolean }unknownfalseA dictionary of feature flags mapping to whether they are enabled.
stringunknownfalseThe class name of this object. Available even when valid is false. For LuaStruct objects it may also be suffixed with a dotted path to a member of the struct.
LuaBootstrap.on_init(handler: function() | nil)
bootstrap.on_init(handler)
takes_table: falsetable_optional: falseRegister a function 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 storage 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.
For more context, refer to the Data Lifecycle page.
handler: function() | nil — The handler for this event. Passing nil will unregister it.LuaBootstrap.on_load(handler: function() | nil)
bootstrap.on_load(handler)
takes_table: falsetable_optional: falseRegister 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 rectify potential differences in local state introduced by the save/load cycle. Doing anything other than the following three will lead to desyncs, breaking multiplayer and replay functionality. Access to LuaGameScript is not available. The storage table can be accessed and is safe to read from, but not write to, as doing so will lead to an error.
The only legitimate uses of this event are these:
For all other purposes, LuaBootstrap::on_init, LuaBootstrap::on_configuration_changed or migrations should be used instead.
For more context, refer to the Data Lifecycle page.
handler: function() | nil — The handler for this event. Passing nil will unregister it.LuaBootstrap.on_configuration_changed(handler: function(ConfigurationChangedData) | nil)
bootstrap.on_configuration_changed(handler)
takes_table: falsetable_optional: falseRegister a function to be run when mod configuration changes.
This is called when the game version or any mod version changed, when any mod was added or removed, when a startup setting has changed, when any prototypes have been added or removed, or when a migration was applied. It allows the mod to make any changes it deems appropriate to both the data structures in its storage table or to the game state through LuaGameScript.
For more context, refer to the Data Lifecycle page.
handler: function(ConfigurationChangedData) | nil — The handler for this event. Passing nil will unregister it.LuaBootstrap.on_event(event: LuaEventType | Array<LuaEventType>, handler: function(EventData) | nil, filters?: EventFilter)
bootstrap.on_event(event, handler, filters)
takes_table: falsetable_optional: falseRegister 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.
event: LuaEventType | Array<LuaEventType> — The event(s) or custom-input to invoke the handler on.handler: function(EventData) | nil — The handler for this event. Passing nil will unregister it.filters?: EventFilter — The filters for this event. Can only be used when registering for individual events.LuaBootstrap.on_nth_tick(tick: uint32 | Array<uint32> | nil, handler: function(NthTickEventData) | nil)
bootstrap.on_nth_tick(tick, handler)
takes_table: falsetable_optional: falseRegister a handler to run every nth-tick(s). When the game is on tick 0 it will trigger all registered handlers.
tick: uint32 | Array<uint32> | nil — The nth-tick(s) to invoke the handler on. Passing nil as the only parameter will unregister all nth-tick handlers.handler: function(NthTickEventData) | nil — The handler to run. Passing nil will unregister it for the provided nth-tick(s).LuaBootstrap.register_on_object_destroyed(object: RegistrationTarget) -> uint64, uint64, defines.target_type
bootstrap.register_on_object_destroyed(object)
takes_table: falsetable_optional: falseRegisters an object so that after it's destroyed, on_object_destroyed is called.
Once an object 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 object, all mods listening to on_object_destroyed will receive the event when it is destroyed. Registering the same object multiple times will still only fire the destruction event once, and will return the same registration number.
Depending on when a given object is destroyed, on_object_destroyed will either be fired at the end of the current tick or at the end of the next tick.
object: RegistrationTarget — The object to register.uint64 — The registration number. It is used to identify the object in the on_object_destroyed event.uint64 — The useful identifier of the object if it has one. This identifier is specific to the object type, for example for trains it is the value LuaTrain::id.defines.target_type — Type of the target object.LuaBootstrap.register_metatable(name: string, metatable: table)
bootstrap.register_metatable(name, metatable)
takes_table: falsetable_optional: falseRegister a metatable to have linkage recorded and restored when saving/loading.
The metatable itself will not be saved. Instead, only the linkage to a registered metatable is saved, and the metatable registered under that name will be used when loading the table.
register_metatable() can not be used in the console, in event listeners or during a remote.call().
The metatable first needs to be defined in the mod's root scope, then registered using this method. From then on, it will be properly restored for tables in storage.
local metatable =
{
__index = function(key)
return "no value for key " .. key
end
}
script.register_metatable("my_metatable", metatable)
This previously defined metatable can then be set on any table as usual:
local table = {key="value"}
setmetatable(table, metatable)
name: string — The name of this metatable. Names must be unique per mod.metatable: table — The metatable to register.LuaBootstrap.generate_event_name() -> defines.events
bootstrap.generate_event_name()
takes_table: falsetable_optional: falseGenerate a new, unique event ID that can be used to raise custom events with LuaBootstrap::raise_event.
defines.events — The newly generated event ID. This will be a new value that does not correspond to any named entry in defines.events.LuaBootstrap.get_event_id(event: LuaEventType) -> defines.events
bootstrap.get_event_id(event)
takes_table: falsetable_optional: falseConverts LuaEventType into related value of defines.events. Value will be provided also if event was not given a constant inside of defines.events.
event: LuaEventTypedefines.eventsLuaBootstrap.get_event_handler(event: LuaEventType) -> function(EventData)?
bootstrap.get_event_handler(event)
takes_table: falsetable_optional: falseFind the event handler for an event.
event: LuaEventType — The event identifier to get a handler for.function(EventData)? — Reference to the function currently registered as the handler, if it was found.LuaBootstrap.get_event_order() -> string
bootstrap.get_event_order()
takes_table: falsetable_optional: falseGets the mod event order as a string.
stringLuaBootstrap.set_event_filter(event: LuaEventType, filters?: EventFilter)
bootstrap.set_event_filter(event, filters)
takes_table: falsetable_optional: falseSets 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.
Limit the on_marked_for_deconstruction event to only be received when a non-ghost entity is marked for deconstruction.
script.set_event_filter(defines.events.on_marked_for_deconstruction, {{filter = "ghost", invert = true}})
Limit the on_built_entity event to only be received when either a 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"}})
Limit the on_entity_damaged event to only be received when a 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"}})
event: LuaEventType — ID of the event to filter.filters?: EventFilter — The filters or nil to clear them.LuaBootstrap.get_event_filter(event: LuaEventType) -> EventFilter?
bootstrap.get_event_filter(event)
takes_table: falsetable_optional: falseGets the filters for the given event.
event: LuaEventType — ID of the event to get.EventFilter? — The filters or nil if none are defined.LuaBootstrap.raise_event(event: LuaEventType, data: table)
bootstrap.raise_event(event, data)
takes_table: falsetable_optional: falseRaise an event. Only events generated with LuaBootstrap::generate_event_name and the following can be raised:
event: LuaEventType — ID or name of the event to raise.data: table — Table with extra data that will be passed to the event handler. Any invalid LuaObjects will silently stop the event from being raised.LuaBootstrap.raise_console_chat{ player_index: uint32, message: string }
bootstrap.raise_console_chat{ player_index=player_index, message=message }
takes_table: truetable_optional: falseplayer_index: uint32 — The player doing the chatting.message: string — The chat message to send.LuaBootstrap.raise_player_crafted_item{ item_stack: LuaItemStack, player_index: uint32, recipe: RecipeID }
bootstrap.raise_player_crafted_item{ item_stack=item_stack, player_index=player_index, recipe=recipe }
takes_table: truetable_optional: falseitem_stack: LuaItemStack — The item that has been crafted.player_index: uint32 — The player doing the crafting.recipe: RecipeID — The recipe used to craft this item.LuaBootstrap.raise_player_fast_transferred{ player_index: uint32, entity: LuaEntity, from_player: boolean, is_split: boolean }
bootstrap.raise_player_fast_transferred{ player_index=player_index, entity=entity, from_player=true, is_split=true }
takes_table: truetable_optional: falseplayer_index: uint32 — The player transferred from or to.entity: LuaEntity — The entity transferred from or to.from_player: boolean — Whether the transfer was from player to entity. If false, the transfer was from entity to player.is_split: boolean — Whether the transfer was a split action (half stack).LuaBootstrap.raise_biter_base_built{ entity: LuaEntity }
bootstrap.raise_biter_base_built{ entity=entity }
takes_table: truetable_optional: falseentity: LuaEntity — The entity that was built.LuaBootstrap.raise_market_item_purchased{ player_index: uint32, market: LuaEntity, offer_index: uint32, count: uint32 }
bootstrap.raise_market_item_purchased{ player_index=player_index, market=market, offer_index=offer_index, count=count }
takes_table: truetable_optional: falseplayer_index: uint32 — The player who did the purchasing.market: LuaEntity — The market entity.offer_index: uint32 — The index of the offer purchased.count: uint32 — The amount of offers purchased.LuaBootstrap.raise_script_built{ entity: LuaEntity }
bootstrap.raise_script_built{ entity=entity }
takes_table: truetable_optional: falseentity: LuaEntity — The entity that has been built.LuaBootstrap.raise_script_destroy{ entity: LuaEntity }
bootstrap.raise_script_destroy{ entity=entity }
takes_table: truetable_optional: falseentity: LuaEntity — The entity that was destroyed.LuaBootstrap.raise_script_revive{ entity: LuaEntity, tags?: Tags }
bootstrap.raise_script_revive{ entity=entity }
takes_table: truetable_optional: falseentity: LuaEntity — The entity that was revived.tags?: Tags — The tags associated with this entity, if any.LuaBootstrap.raise_script_teleported{ entity: LuaEntity, old_surface_index: uint8, old_position: MapPosition }
bootstrap.raise_script_teleported{ entity=entity, old_surface_index=old_surface_index, old_position=old_position }
takes_table: truetable_optional: falseentity: LuaEntity — The entity that was teleported.old_surface_index: uint8 — The entity's surface before the teleportation.old_position: MapPosition — The entity's position before the teleportation.LuaBootstrap.raise_script_set_tiles{ surface_index: uint32, tiles: Array<Tile> }
bootstrap.raise_script_set_tiles{ surface_index=surface_index, tiles=tiles }
takes_table: truetable_optional: falsesurface_index: uint32 — The surface whose tiles have been changed.tiles: Array<Tile> — The tiles that have been changed.LuaBootstrap.raise_script_destroy_segmented_unit{ segmented_unit: LuaSegmentedUnit }
bootstrap.raise_script_destroy_segmented_unit{ segmented_unit=segmented_unit }
takes_table: truetable_optional: falsesegmented_unit: LuaSegmentedUnit — The segmented unit that was destroyed.