Provides various helper and utility functions. It is accessible through the global object named helpers in all stages (settings, prototype and runtime).
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.
stringunknownfalseCurrent version of game
LuaHelpers.table_to_json(data: table) -> string
helpers.table_to_json(data)
takes_table: falsetable_optional: falseConvert a table to a JSON string
data: tablestringLuaHelpers.json_to_table(json: string) -> AnyBasic?
helpers.json_to_table(json)
takes_table: falsetable_optional: falseConvert a JSON string to a table.
json: string — The string to convert.AnyBasic? — The returned object, or nil if the JSON string was invalid.LuaHelpers.write_file(filename: string, data: LocalisedString, append?: boolean, for_player?: uint32)
helpers.write_file(filename, data, true)
takes_table: falsetable_optional: falseWrite a file to the script-output folder, located in the game's user data directory. The name and file extension of the file can be specified via the filename parameter.
filename: string — The name of the file. Providing a directory path (ex. "save/here/example.txt") will create the necessary folder structure in script-output.data: LocalisedString — The content to write to the file.append?: boolean — If true, data will be appended to the end of the file. Defaults to false, which will overwrite any pre-existing file with the new data.for_player?: uint32 — If given, the file will only be written for this player_index. Providing 0 will only write to the server's output if present. for_player cannot be used in settings and prototype stages.LuaHelpers.send_udp(port: uint16, data: LocalisedString, for_player?: uint32)
helpers.send_udp(port, data, for_player)
takes_table: falsetable_optional: falseSend data to a UDP port on localhost for a specified player, if enabled.
This must be enabled per-instance with --enable-lua-udp.
port: uint16 — Destination port number (localhost only)data: LocalisedString — The content to send.for_player?: uint32 — If given, the packet will only be sent from this player_index. Providing 0 will only send from the server if present. for_player cannot be used in settings and prototype stages.LuaHelpers.recv_udp(for_player?: uint32)
helpers.recv_udp()
takes_table: falsetable_optional: falseDispatch defines.events.on_udp_packet_received events for any new packets received by the specified player or the server.
This must be enabled per-instance with --enable-lua-udp.
UDP socket when enabled requests 256KB of receive buffer from the operating system. If there is more data than this between two subsequent calls of this method, data will be lost. That also applies to periods when the game is paused or is being saved as in those case the game update is not happening.
Note: lua event is not raised immediately as the UDP packet needs to be introduced into game state by means of input actions. Please keep incoming traffic as small as possible as in case of multiplayer game with many players, all this data will have to go through the multiplayer server and be distributed to all clients.
Not available in settings and prototype stages.
for_player?: uint32 — If given, packets will only be read from this player_index. Providing 0 will only read from the server if present.LuaHelpers.remove_path(path: string)
helpers.remove_path(path)
takes_table: falsetable_optional: falseRemove a file or directory in the script-output folder, located in the game's user data directory. Can be used to remove files created by LuaHelpers::write_file.
path: string — The path to the file or directory to remove, relative to script-output.LuaHelpers.direction_to_string(direction: defines.direction) -> string
helpers.direction_to_string(direction)
takes_table: falsetable_optional: falseConverts the given direction into the string version of the direction.
direction: defines.directionstringLuaHelpers.evaluate_expression(expression: MathExpression, variables?: Dict<string, double>) -> double
helpers.evaluate_expression(expression, variables)
takes_table: falsetable_optional: falseEvaluate an expression, substituting variables as provided.
expression: MathExpression — The expression to evaluate.variables?: Dict<string, double> — Variables to be substituted.doubleLuaHelpers.encode_string(string: string) -> string?
helpers.encode_string(string)
takes_table: falsetable_optional: falseDeflates and base64 encodes the given string.
string: string — The string to encode.string? — The encoded string or nil if the encode failed.LuaHelpers.decode_string(string: string) -> string?
helpers.decode_string(string)
takes_table: falsetable_optional: falseBase64 decodes and inflates the given string.
string: string — The string to decode.string? — The decoded string or nil if the decode failed.LuaHelpers.parse_map_exchange_string(map_exchange_string: string) -> MapExchangeStringData
helpers.parse_map_exchange_string(map_exchange_string)
takes_table: falsetable_optional: falseConvert a map exchange string to map gen settings and map settings.
Not available in settings and prototype stages.
map_exchange_string: stringMapExchangeStringDataLuaHelpers.check_prototype_translations()
helpers.check_prototype_translations()
takes_table: falsetable_optional: falseGoes over all items, entities, tiles, recipes, technologies among other things and logs if the locale is incorrect.
Also prints true/false if called from the console.
Not available in settings and prototype stages.
LuaHelpers.is_valid_sound_path(sound_path: SoundPath) -> boolean
helpers.is_valid_sound_path(sound_path)
takes_table: falsetable_optional: falseChecks if the given SoundPath is valid.
Not available in settings and prototype stages.
sound_path: SoundPath — Path to the sound.booleanLuaHelpers.is_valid_sprite_path(sprite_path: SpritePath) -> boolean
helpers.is_valid_sprite_path(sprite_path)
takes_table: falsetable_optional: falseChecks if the given SpritePath is valid and contains a loaded sprite. The existence of the image is not checked for paths of type file.
Not available in settings and prototype stages.
sprite_path: SpritePath — Path to the image.booleanLuaHelpers.create_profiler(stopped?: boolean) -> LuaProfiler
helpers.create_profiler()
takes_table: falsetable_optional: falseCreates a LuaProfiler, which is used for measuring script performance.
LuaProfiler cannot be serialized.
Not available in settings and prototype stages.
stopped?: boolean — Create the timer stoppedLuaProfilerLuaHelpers.compare_versions(first: string, second: string) -> int32
helpers.compare_versions(first, second)
takes_table: falsetable_optional: falseCompares 2 version strings.
first: string — First version string to compare.second: string — Second version string to compare.int32 — -1 if first is smaller than second, 0 if first equal second, 1 if first is greater than second.LuaHelpers.multilingual_to_lower(input: string) -> string
helpers.multilingual_to_lower(input)
takes_table: falsetable_optional: falseConverts the given string to lowercase and returns it. Unlike string.lower(), this function supports non-Latin characters.
input: stringstring — The input string converted to lowercase.