Factorio Runtime Docs

Global

During the lifetime of a mod, it will frequently want to save mutable data. Although mods are free to store such data in any variable, the game will not persist these through the save/load cycle.

To address this, Factorio provides the global table: a Lua global variable that is serialized and persisted between saving and loading of a map. Each mod has access to its own instance of this table, so there is no need to worry about namespacing. Circular references are handled properly as well.

To clarify, in the context of Factorio, the term "global" can refer to two different things:

  • Global variables in Lua, meaning variables which are defined without the local keyword.
  • The Factorio-specific global variable, which is a table named global. Functionally, it is a Lua global variable that is persisted through the save/load cycle.

The Data Lifecycle lays out some restrictions to accessing the global table. Importantly, the table is not yet loaded during the control.lua step, thus anything written to it then will be overwritten afterwards. Additionally, it is disallowed to write to global during on_load, as doing so will lead to an error to prevent desyncs.

Only specific data can be stored in global:

  • Basic data: nil, strings, numbers, booleans.
  • Tables, with limited metatables:
    • The metatable itself will not be saved.
    • Metatables that are registered with LuaBootstrap::register_metatable will be recorded by name and automatically relinked to the registered table on loading.
    • Any other metatables will be removed; tables with unregistered metatables become plain tables when saved and loaded.
  • References to Factorio's LuaObjects.

Functions are not allowed in global and will throw an error when saving.

Classes

Events

Concepts

Defines

Builtin types

>|