In general, the Factorio API consists of 3 phases:
For more information about how mods are loaded, initialized, and the different phases, see Data Lifecycle.
All of the scripting API functionality is implemented via class members. To make any use of the API, therefore, you need to have access to instances of these classes. To facilitate that, some are provided as global classes.
Other global objects:
global dictionary, useful for storing data persistent across a save-load
defines table, which contains symbolic constants used
throughout the API.
The following data types are used across the API:
As described above, it is necessary to have access to instances of classes to use the scripting API. Using the provided global classes, instances of all other classes can be accessed.
local first_player = game.get_player(1)
You can browse the overview of Factorio classes or view the documentation for a particular class in the list below:
|Collection of settings for overriding default ai behavior.
|Control behavior for accumulators.
|Prototype of a achievement.
|Prototype of a ammo category.
|Control behavior for arithmetic combinators.
|Prototype of an autoplace control.
|Entry point for registering event handlers.
|A reference to the burner energy source owned by a specific LuaEntity or LuaEquipment.
|Prototype of a burner energy source.
|A chunk iterator can be used for iterating chunks coordinates of a surface.
|A circuit network associated with a given entity, connector, and wire type.
|Custom game console commands.
|Control behavior for constant combinators.
|Control behavior for container entities.
|This is an abstract base class containing the common functionality between LuaPlayer and entities (see LuaEntity).
|The control behavior for an entity.
|A custom tag that shows on the map view.
|Prototype of a custom input.
|Lazily evaluated table.
|Prototype of a damage.
|Control behavior for decider combinators.
|Prototype of an optimized decorative.
|Prototype of an electric energy source.
|The primary interface for interacting with entities through the Lua API.
|Prototype of an entity.
|An item in a LuaEquipmentGrid, for example one's power armor.
|Prototype of a equipment category.
|An equipment grid is for example the inside of a power armor.
|Prototype of an equipment grid.
|Prototype of a modular equipment.
|Encapsulates statistic data for different parts of the game.
|An array of fluid boxes of an entity.
|A prototype of a fluidbox owned by some LuaEntityPrototype.
|Prototype of a fluid energy source.
|Prototype of a fluid.
|Prototype of a font.
LuaForce encapsulates data local to each "force" or "faction" of the game.
|Prototype of a fuel category.
|Main toplevel type, provides access to most of the API though its members.
|An abstract base class for behaviors that support switching the entity on or off based on some condition.
|Item group or subgroup.
|The root of the GUI.
|An element of a custom GUI.
|Prototype of a heat energy source.
|Control behavior for inserters.
|A storage of item stacks.
|Prototype of an item.
|A reference to an item and count owned by some external entity.
|Control behavior for lamps.
|A lazily loaded value.
|Logistic cell of a particular LuaEntity.
|Control behavior for logistic chests.
|A single logistic network of a given force on a given surface.
|Logistic point of a particular LuaEntity.
|Control behavior for mining drills.
|Prototype of a mod setting.
|Prototype of a module category.
|Prototype of a named noise expression.
|Prototype of a noise layer.
|Prototype of an optimized particle.
|A permission group that defines what players in this group are allowed to do.
|All permission groups.
|A player in the game.
|An object used to measure script performance.
|Control behavior for programmable speakers.
|An interface to send messages to the calling RCON interface.
|Control behavior for rail chain signals.
|A rail path.
|Control behavior for rail signals.
|A deterministic random generator independent from the core games random generator that can be seeded and re-seeded at will.
|A crafting recipe.
|Prototype of a recipe category.
|A crafting recipe prototype.
|Registry of interfaces between scripts.
|Allows rendering of geometric shapes, text and sprites in the game world.
|Prototype of a resource category.
|Control behavior for roboports.
|Runtime settings can be changed through console commands and by the mod that owns the settings.
|Prototype of a shortcut.
|Control behavior for storage tanks.
|Style of a GUI element.
|A "domain" of the world.
|One research item.
|A Technology prototype.
|A single "square" on the map.
|Prototype of a tile.
|Control behavior for train stops.
|Control behavior for transport belts.
|One line on a transport belt.
|Prototype of a trivial smoke.
|A collection of units moving and attacking together.
|Prototype of a virtual signal.
|Prototype of a void energy source.
|Control behavior for walls.
Events are delivered to mods in response to certain actions happening in the game. A mod can register
a callback for a given event, using the LuaBootstrap::on_event function. Events receive a parameter that
is a table containing fields
tick specifying the event ID (see defines.events) and the
tick the event happened. This table may also contain additional fields that depend on the event name.
Additionally some events can be filtered to provide improved performance when a mod doesn't need or
want to react to everything a given event may be triggered by. See Event Filters.
local recently_deceased_entity = event.entity
local time_of_death = event.tick
game.print("Let it be known that " .. recently_deceased_entity.name ..
" died a tragic death on tick " .. time_of_death)
Here is a complete list of Factorio events:
Every event contains at least the
|Custom Input Events
|Called when a custom-input is activated
|Called when a unit/group completes a command.
|Called when an area of the map is cloned.
|Called when a biter migration builds a base.
|Called when a set of positions on the map is cloned.
|Called when a defines.command.build_base command reaches its destination, and before building starts.
|Called when player builds something.
|Called when the deconstruction of an entity is canceled.
|Called when the upgrade of an entity is canceled.
|Called when a character corpse expires due to timeout or all of the items being removed from it.
|Called when a chart tag is created.
|Called when a chart tag is modified by a player.
|Called just before a chart tag is deleted.
|Called when a chunk is charted or re-charted.
|Called when one or more chunks are deleted using LuaSurface::delete_chunk.
|Called when a chunk is generated.
|Called when a combat robot expires through a lack of energy, or timeout.
|Called when someone talks in-game either a player or through the server interface.
|Called when someone enters a command-like message regardless of it being a valid command.
|Called when a cutscene for a player is cancelled.
|Called when a cutscene is playing, each time it reaches a waypoint in that cutscene.
|Called when the map difficulty settings are changed.
|Called when an entity is cloned.
|Called when an entity is damaged.
|Called after an entity is destroyed that has been registered with LuaBootstrap::register_on_entity_destroyed
|Called when an entity dies.
|Called when one of an entity's personal logistic slots changes.
|Called after an entity has been renamed either by the player or through script.
|Called after entity copy-paste is done.
|Called when an entity is spawned by a EnemySpawner
|Called when the a forces cease fire values change.
| Called when a new force is created using
|Called when the a forces friends change.
|Called when LuaForce::reset is finished.
| Called after two forces have been merged using
| Called when two forces are about to be merged using
|Called when a game is created from a scenario.
|Called when LuaGuiElement checked state is changed (related to checkboxes and radio buttons).
|Called when LuaGuiElement is clicked.
|Called when the player closes the GUI they have open.
|Called when a LuaGuiElement is confirmed, for example by pressing Enter in a textfield.
|Called when LuaGuiElement element value is changed (related to choose element buttons).
| Called when LuaGuiElement element location is changed (related to frames in
|Called when the player opens a GUI.
|Called when LuaGuiElement selected tab is changed (related to tabbed-panes).
|Called when LuaGuiElement selection state is changed (related to drop-downs and listboxes).
|Called when LuaGuiElement switch state is changed (related to switches).
|Called when LuaGuiElement text is changed by the player.
|Called when LuaGuiElement slider value is changed (related to the slider element).
|Called when a land mine is armed.
|Called when a custom Lua shortcut is pressed.
|Called when an entity is marked for deconstruction with the Deconstruction planner or via script.
|Called when an entity is marked for upgrade with the Upgrade planner or via script.
|Called after a player purchases some offer from a Market entity.
|Called when the player uses the 'Open item GUI' control on an item defined with the 'mod-openable' flag
|Called directly after a permission group is added.
|Called directly after a permission group is deleted.
|Called directly after a permission group is edited in some way.
|Called directly after a permission string is imported.
|Called when a player picks up an item.
|Called after a player alt-selects an area with a selection-tool item.
|Called after a players ammo inventory changed in some way.
|Called after a players armor inventory changed in some way.
|Called when a player is banned.
|Called after a player builds tiles.
|Called when a player cancels crafting.
|Called after a player changes forces.
|Called when the tile position a player is located at changes.
|Called after a player changes surfaces.
|Called when cheat mode is disabled on a player.
|Called when cheat mode is enabled on a player.
|Called when a player clicks a gps tag
|Called when a player clicks the "confirm" button in the configure Blueprint GUI.
|Called when a player configures spidertron remote to be connected with a given spidertron
|Called when the player crafts an item (just before inserting into player's inventory, not clicking the button to craft).
|Called after the player was created.
|Called after a players cursorstack changed in some way.
|Called when a player selects an area with a deconstruction planner.
|Called when a player is demoted.
|Called after a player dies.
|Called when the display resolution changes for a given player.
|Called when the display scale changes for a given player.
|Called when the player's driving state has changed, this means a player has either entered or left a vehicle.
|Called when a player drops an item on the ground.
|Called when a player fast-transfers something to or from an entity.
|Called after player flushed fluid
|Called after a players gun inventory changed in some way.
|Called after a player joins the game.
|Called when a player is kicked.
|Called after a player leaves the game.
|Called after a players main inventory changed in some way.
|Called after the results of an entity being mined are collected just before the entity is destroyed.
|Called when the player mines something.
|Called after a player mines tiles.
|Called when a player is muted.
|Called when a player invokes the "smart pipette" over an entity.
|Called after the player puts equipment in an equipment grid
|Called when a player is promoted.
|Called when a player is removed (deleted) from the game.
|Called after the player removes equipment from an equipment grid
|Called when a player repairs an entity.
|Called after a player respawns.
|Called when the player rotates an entity.
|Called after a player selects an area with a selection-tool item.
|Called when a player sets a quickbar slot to anything (new value, or set to empty).
|Called when a player selects an area with a blueprint.
|Called when a player toggles alt mode, also known as "show entity info".
|Called when a player toggles the map editor on or off.
|Called after a players trash inventory changed in some way.
|Called when a player is un-banned.
|Called when a player is unmuted.
|Called when a player uses a capsule that results in some game action.
|Called when a player uses spidertron remote to send a spidertron to a given position
|Called after an entity dies.
|Called when players uses an item to build something.
|Called before one or more chunks are deleted using LuaSurface::delete_chunk.
|Called before entity copy-paste is done.
|Called before a ghost entity is destroyed as a result of being marked for deconstruction.
|Called directly before a permission group is deleted.
|Called directly before a permission string is imported.
|Called when a player queues something to be crafted.
|Called before a players dies.
|Called before a player leaves the game.
|Called when the player finishes mining an entity, before the entity is removed from map.
|Called before a player is removed (deleted) from the game.
|Called before a player toggles the map editor on or off.
|Called directly before a robot explodes cliffs.
|Called just before a script inventory is resized.
|Called just before a surface is cleared (all entities removed and all chunks deleted).
|Called just before a surface is deleted.
|Called when a research finishes.
|Called when a research is reversed (unresearched).
|Called when a technology research starts.
|Called when a resource entity reaches 0 or its minimum yield for infinite resources.
|Called when a construction robot builds an entity.
|Called after a robot builds tiles.
|Called directly after a robot explodes cliffs.
|Called when a robot mines an entity.
|Called after the results of an entity being mined are collected just before the entity is destroyed.
|Called after a robot mines tiles.
|Called before a robot mines an entity.
|Called when a rocket silo is ordered to be launched.
|Called when the rocket is launched.
|Called when a runtime mod setting is changed by a player.
|Called just after a script inventory is resized.
|Called when a script path request completes.
|Called when a script trigger effect is triggered.
|Called when the radar finishes scanning a sector.
|Called after the selected entity changes for a given player.
|Called when a spider finishes moving to its autopilot position.
|Called when a translation request generated through LuaPlayer::request_translation is translated.
|Called just after a surface is cleared (all entities removed and all chunks deleted).
|Called when a surface is created.
|Called after a surface is deleted.
|Called after a surface is imported.
|Called when a surface is renamed.
|Called when LuaForce::reset_technology_effects is finished.
|It is fired once every tick.
|Called when a train changes state (started to stopped and vice versa)
|Called when a new train is created either through disconnecting/connecting an existing one or building a new one.
|Called when a trains schedule is changed either by the player or through script.
| Called when an entity with a trigger prototype (such as capsules) create an entity AND that
trigger prototype defined
| Called when an entity with a trigger prototype (such as capsules) fire an artillery projectile AND that
trigger prototype defined
|Called when a unit is added to a unit group.
|Called when a new unit group is created, before any members are added to it.
|Called when a unit group finishes gathering and starts executing its command.
|Called when a unit is removed from a unit group.
|A static event mods can use to tell other mods they built something with a script.
|A static event mods can use to tell other mods they destroyed something with a script.
|A static event mods can use to tell other mods they revived something with a script.
|A static event mods can use to tell other mods they changed tiles on a surface with a script.