Factorio.com|Forums|Wiki|Mod Portal|API Docs Prototype|Runtime|Auxiliary

# Factorio Runtime Docs Version 2.0.7

## Class LuaHelpers new

Provides various helper and utility functions. It is accessible through the global object named `helpers`.

## Members

table_to_json(data) | → string | Convert a table to a JSON string
Convert a table to a JSON string
json_to_table(json) | → AnyBasic? | Convert a JSON string to a table.
Convert a JSON string to a table.
write_file(filename, data, append?, for_player?) |  | Write a file to the `script-output` folder, located in the game's user data directory. [...]
Write a file to the `script-output` folder, located in the game's user data directory. [...]
remove_path(path) |  | Remove a file or directory in the `script-output` folder, located in the game's user data directory. [...]
Remove a file or directory in the `script-output` folder, located in the game's user data directory. [...]
direction_to_string(direction) | → string | Converts the given direction into the string version of the direction.
Converts the given direction into the string version of the direction.
evaluate_expression(expression, variables?) | → double | Evaluate an expression, substituting variables as provided.
Evaluate an expression, substituting variables as provided.
encode_string(string) | → string? | Deflates and base64 encodes the given string.
Deflates and base64 encodes the given string.
decode_string(string) | → string? | Base64 decodes and inflates the given string.
Base64 decodes and inflates the given string.
parse_map_exchange_string(map_exchange_string) | → MapExchangeStringData | Convert a map exchange string to map gen settings and map settings.
Convert a map exchange string to map gen settings and map settings.
check_prototype_translations() |  | Goes over all items, entities, tiles, recipes, technologies among other things and logs if the locale is incorrect. [...]
Goes over all items, entities, tiles, recipes, technologies among other things and logs if the locale is incorrect. [...]
is_valid_sound_path(sound_path) | → boolean | Checks if the given SoundPath is valid.
Checks if the given SoundPath is valid.
is_valid_sprite_path(sprite_path) | → boolean | Checks if the given SpritePath is valid and contains a loaded sprite. [...]
Checks if the given SpritePath is valid and contains a loaded sprite. [...]
create_profiler(stopped?) | → LuaProfiler | Creates a LuaProfiler, which is used for measuring script performance. [...]
Creates a LuaProfiler, which is used for measuring script performance. [...]
object_name | :: R string | The class name of this object. [...]
The class name of this object. [...]

## Methods

### table_to_json(data) → string

Convert a table to a JSON string

#### Parameters

data | :: table |

### json_to_table(json) → AnyBasic?

Convert a JSON string to a table.

#### Parameters

json | :: string | The string to convert.
The string to convert.

#### Return values

→ AnyBasic? | The returned object, or `nil` if the JSON string was invalid.

### write_file(filename, data, append?, for_player?)

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

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`.
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.
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`.
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 | :: uint? | If given, the file will only be written for this `player_index`. Providing `0` will only write to the server's output if present.
If given, the file will only be written for this `player_index`. Providing `0` will only write to the server's output if present.

### remove_path(path)

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

path | :: string | The path to the file or directory to remove, relative to `script-output`.
The path to the file or directory to remove, relative to `script-output`.

### direction_to_string(direction) → string

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

#### Parameters

direction | :: defines.direction |

### evaluate_expression(expression, variables?) → double

Evaluate an expression, substituting variables as provided.

#### Parameters

expression | :: MathExpression | The expression to evaluate.
The expression to evaluate.
variables | :: dictionary[string → double]? | Variables to be substituted.
Variables to be substituted.

#### Example

    -- Calculate the number of research units required to unlock mining productivity level 10
    local formula = game.forces["player"].technologies["mining-productivity-4"].research_unit_count_formula
    local units = helpers.evaluate_expression(formula, { L = 10, l = 10 })

### encode_string(string) → string?

Deflates and base64 encodes the given string.

#### Parameters

string | :: string | The string to encode.
The string to encode.

#### Return values

→ string? | The encoded string or `nil` if the encode failed.

### decode_string(string) → string?

Base64 decodes and inflates the given string.

#### Parameters

string | :: string | The string to decode.
The string to decode.

#### Return values

→ string? | The decoded string or `nil` if the decode failed.

### parse_map_exchange_string(map_exchange_string) → MapExchangeStringData

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

#### Parameters

map_exchange_string | :: string |

### check_prototype_translations()

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.

### is_valid_sound_path(sound_path) → boolean

Checks if the given SoundPath is valid.

#### Parameters

sound_path | :: SoundPath | Path to the sound.
Path to the sound.

### is_valid_sprite_path(sprite_path) → boolean

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

#### Parameters

sprite_path | :: SpritePath | Path to the image.
Path to the image.

### create_profiler(stopped?) → LuaProfiler

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

LuaProfiler cannot be serialized.

#### Parameters

stopped | :: boolean? | Create the timer stopped
Create the timer stopped

## Attributes

### object_name :: Read string

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.