LuaBootstrap
Entry point for registering event handlers. It is accessible through the global object named script
.
LuaBootstrap
Register a function to be run on mod initialization.
Register a function to be run on save load.
Register a function to be run when mod configuration changes.
Register a handler to run on the specified event(s).
Register a handler to run every nth-tick(s).
Registers an entity so that after it's destroyed, on_entity_destroyed is called.
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.
Gets the filters for the given event.
Raise an event.
A dictionary listing the names of all currently active mods and mapping them to their version.
This object's name.
on_init(f)
Register 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 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.
The handler for this event. Passing nil
will unregister it.
Initialize a players
table in global
for later use.
script.on_init(function()
global.players = {}
end)
For more context, refer to the Data Lifecycle page.
on_load(f)
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 is not available. The global 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 the following:
- Re-setup metatables as they are not persisted through the save/load cycle.
- Re-setup conditional event handlers, meaning subscribing to an event only when some condition is met to save processing time.
- Create local references to data stored in the global table.
For all other purposes, LuaBootstrap::on_init, LuaBootstrap::on_configuration_changed or migrations should be used instead.
The handler for this event. Passing nil
will unregister it.
For more context, refer to the Data Lifecycle page.
on_configuration_changed(f)
Register a function to be run when mod configuration changes. This is called when the major game version or any mod version changed, when any mod was added or removed, when a startup setting has changed, or when any prototypes have been added or removed. 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.
The handler for this event. Passing nil
will unregister it.
For more context, refer to the Data Lifecycle page.
on_event(event,
f,
filters)
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.
The event(s) or custom-input to invoke the handler on.
The handler for this event. Passing nil
will unregister it.
The filters for this event. Can only be used when registering for individual events.
Register for the on_tick event to print the current tick to console each tick.
script.on_event(defines.events.on_tick,
function(event) game.print(event.tick) end)
Register for the on_built_entity event, limiting it to only be received when a "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"}})
on_nth_tick(tick,
f)
Register a handler to run every nth-tick(s). When the game is on tick 0 it will trigger all registered handlers.
The nth-tick(s) to invoke the handler on. Passing nil
as the only parameter will unregister all nth-tick handlers.
The handler to run. Passing nil
will unregister it for the provided nth-tick(s).
register_on_entity_destroyed(entity)
→
uint64
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.
The entity to register.
The registration number. It is used to identify the entity in the on_entity_destroyed event.
Depending on when a given entity is destroyed, on_entity_destroyed will either be fired at the end of the current tick or at the end of the next tick.
generate_event_name()
→
uint
Generate a new, unique event ID that can be used to raise custom events with LuaBootstrap::raise_event.
The newly generated event ID.
get_event_handler(event)
→
function(EventData)?
set_event_filter(event,
filters)
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.
ID of the event to filter.
The filters or nil
to clear them.
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"}})
get_event_filter(event)
→
array[EventFilter]?
Gets the filters for the given event.
ID of the event to get.
The filters or nil
if none are defined.
raise_event(event,
data)
Raise an event. Only events generated with LuaBootstrap::generate_event_name and the following can be raised:
ID of the event to raise.
Table with extra data that will be passed to the event handler. Any invalid LuaObjects will silently stop the event from being raised.
Raise the on_console_chat event with the desired message 'from' the first player.
local data = {player_index = 1, message = "Hello friends!"}
script.raise_event(defines.events.on_console_chat, data)
raise_console_chat{player_index=…,
message=…}
Raised with the provided arguments.
raise_player_crafted_item{item_stack=…,
player_index=…,
recipe=…}
The item that has been crafted.
The player doing the crafting.
The recipe used to craft this item.
Raised with the provided arguments.
raise_player_fast_transferred{player_index=…,
entity=…,
from_player=…}
The player transferred from or to.
The entity transferred from or to.
Whether the transfer was from player to entity. If false
, the transfer was from entity to player.
Raised with the provided arguments.
raise_biter_base_built{entity=…}
The entity that was built.
Raised with the provided arguments.
raise_market_item_purchased{player_index=…,
market=…,
offer_index=…,
count=…}
raise_script_built{entity=…}
The entity that has been built.
Raised with the provided arguments.
raise_script_destroy{entity=…}
The entity that was destroyed.
Raised with the provided arguments.
raise_script_revive{entity=…,
tags=…}
mod_name
:: string
[Read]
The name of the mod from the environment this is used in.
level
:: table
[Read]
Information about the currently running scenario/campaign/tutorial.
active_mods
:: dictionary[string → string]
[Read]
A dictionary listing the names of all currently active mods and mapping them to their version.
This will print the names and versions of all active mods to the console.
for name, version in pairs(script.active_mods) do
game.print(name .. " version " .. version)
end
object_name
:: string
[Read]
This object's name.