Home

LuaHelpers

Provides various helper and utility functions. It is accessible through the global object named helpers in all stages (settings, prototype and runtime).

Attributes

object_name

The 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.

game_version

Current version of game

Methods

table_to_json

LuaHelpers.table_to_json(data: table) -> string

Call As

helpers.table_to_json(data)

Call Convention

Convert a table to a JSON string

Parameters

Returns

json_to_table

LuaHelpers.json_to_table(json: string) -> AnyBasic?

Call As

helpers.json_to_table(json)

Call Convention

Convert a JSON string to a table.

Parameters

Returns

write_file

LuaHelpers.write_file(filename: string, data: LocalisedString, append?: boolean, for_player?: uint32)

Call As

helpers.write_file(filename, data, true)

Call Convention

Write 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.

Parameters

send_udp

LuaHelpers.send_udp(port: uint16, data: LocalisedString, for_player?: uint32)

Call As

helpers.send_udp(port, data, for_player)

Call Convention

Send data to a UDP port on localhost for a specified player, if enabled.

This must be enabled per-instance with --enable-lua-udp.

Parameters

recv_udp

LuaHelpers.recv_udp(for_player?: uint32)

Call As

helpers.recv_udp()

Call Convention

Dispatch 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.

Parameters

remove_path

LuaHelpers.remove_path(path: string)

Call As

helpers.remove_path(path)

Call Convention

Remove 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.

Parameters

direction_to_string

LuaHelpers.direction_to_string(direction: defines.direction) -> string

Call As

helpers.direction_to_string(direction)

Call Convention

Converts the given direction into the string version of the direction.

Parameters

Returns

evaluate_expression

LuaHelpers.evaluate_expression(expression: MathExpression, variables?: Dict<string, double>) -> double

Call As

helpers.evaluate_expression(expression, variables)

Call Convention

Evaluate an expression, substituting variables as provided.

Parameters

Returns

encode_string

LuaHelpers.encode_string(string: string) -> string?

Call As

helpers.encode_string(string)

Call Convention

Deflates and base64 encodes the given string.

Parameters

Returns

decode_string

LuaHelpers.decode_string(string: string) -> string?

Call As

helpers.decode_string(string)

Call Convention

Base64 decodes and inflates the given string.

Parameters

Returns

parse_map_exchange_string

LuaHelpers.parse_map_exchange_string(map_exchange_string: string) -> MapExchangeStringData

Call As

helpers.parse_map_exchange_string(map_exchange_string)

Call Convention

Convert a map exchange string to map gen settings and map settings.

Not available in settings and prototype stages.

Parameters

Returns

check_prototype_translations

LuaHelpers.check_prototype_translations()

Call As

helpers.check_prototype_translations()

Call Convention

Goes 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.

is_valid_sound_path

LuaHelpers.is_valid_sound_path(sound_path: SoundPath) -> boolean

Call As

helpers.is_valid_sound_path(sound_path)

Call Convention

Checks if the given SoundPath is valid.

Not available in settings and prototype stages.

Parameters

Returns

is_valid_sprite_path

LuaHelpers.is_valid_sprite_path(sprite_path: SpritePath) -> boolean

Call As

helpers.is_valid_sprite_path(sprite_path)

Call Convention

Checks 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.

Parameters

Returns

create_profiler

LuaHelpers.create_profiler(stopped?: boolean) -> LuaProfiler

Call As

helpers.create_profiler()

Call Convention

Creates a LuaProfiler, which is used for measuring script performance.

LuaProfiler cannot be serialized.

Not available in settings and prototype stages.

Parameters

Returns

compare_versions

LuaHelpers.compare_versions(first: string, second: string) -> int32

Call As

helpers.compare_versions(first, second)

Call Convention

Compares 2 version strings.

Parameters

Returns

multilingual_to_lower

LuaHelpers.multilingual_to_lower(input: string) -> string

Call As

helpers.multilingual_to_lower(input)

Call Convention

Converts the given string to lowercase and returns it. Unlike string.lower(), this function supports non-Latin characters.

Parameters

Returns