Core Module - Common - Adds some commonly used functions used in many modules
utils.color_presets |
utils.game |
type_check(value[, test_type=nil]) | Asserts the argument is of type test_type |
type_error(value, test_type, error_message, level) | Raises an error if the value is of the wrong type |
multi_type_check(value, test_types) | Asserts the argument is one of type test_types |
multi_type_error(value, test_types, error_message, level) | Raises an error if the value is of the wrong type |
validate_argument_type(value, test_type, param_number[, param_name]) | Raises an error when the value is the incorrect type, uses a consistent error message format |
validate_argument_multi_type(value, test_types, param_number[, param_name]) | Raises an error when the value is the incorrect type, uses a consistent error message format |
error_if_runtime() | Will raise an error if called during runtime |
error_if_runetime_closure(func) | Will raise an error if the function is a closure |
string_contains(s, contains) | Tests if a string contains a given substring. |
resolve_value(value) | Used to resolve a value that could also be a function returning that value |
cast_bool(var) | Converts a varible into its boolean value, nil and false return false |
ternary(c, t, f) | Returns either the second or third argument based on the first argument |
comma_value(n) | Returns a string for a number with comma seperators |
set_and_return(tbl, key, value) | Sets a table element to value while also returning value. |
write_json(path, tbl) | Writes a table object to a file in json format |
opt_require(path) | Calls a require that will not error if the file is not found |
get_file_path([offset=0]) | Returns a desync safe file path for the current file |
enum(tbl) | Converts a table to an enum |
auto_complete(options, input[, use_key=false][, rtn_key=false]) | Returns the closest match to the input |
get_actor(player_name) | Returns a valid string with the name of the actor of a command. |
format_chat_colour(message, color) | Returns a message with valid chat tags to change its colour |
format_chat_colour_localized(message, color) | Returns a message with valid chat tags to change its colour, using localization |
format_chat_player_name(player[, raw_string=false]) | Returns the players name in the players color |
player_return(value[, colour=defines.colour.white][, player=game.player]) | Will return a value of any type to the player/server console, allows colour for in-game players |
format_time(ticks, options) | Formats tick into a clean format, denominations from highest to lowest -- time will use : separates -- when a denomination is false it will overflow into the next one |
copy_items_stack(items[, surface=navies][, position={0][, radius=32][, chest_type=iron-chest]) | Copies items to the position and stores them in the closest entity of the type given -- Copies the items by prototype name, but keeps them in the original inventory |
move_items_stack(items[, surface=navies][, position={0][, radius=32][, chest_type=iron-chest]) | Moves items to the position and stores them in the closest entity of the type given -- Differs from move_items by accepting a table of LuaItemStack and transferring them into the inventory - not copying |
print_grid_value(value, surface, position[, scale=1][, offset=0][, immutable=false]) | Prints a colored value on a location, color is based on the value. |
clear_flying_text(surface) | Clears all flying text entities on a surface |
Asserts the argument is of type test_type
Parameters:-- Check for a string value
local is_string = type_check(value, 'string')
-- Check for a nil value
local is_nil = type_check(value)
Raises an error if the value is of the wrong type
Parameters:-- Raise error if value is not a number
type_error(value, 'number', 'Value must be a number')
Asserts the argument is one of type test_types
Parameters:-- Check for a string or table
local is_string_or_table = multi_type_check(value, {'string', 'table'})
Raises an error if the value is of the wrong type
Parameters:-- Raise error if value is not a string or table
multi_type_error('foo', {'string', 'table'}, 'Value must be a string or table')
Raises an error when the value is the incorrect type, uses a consistent error message format
Parameters:-- Output: "Bad argument #2 to "<anon>"; argument is of type string expected number"
validate_argument_type(value, 'number', 2)
-- Output: "Bad argument #2 to "<anon>"; "repeat_count" is of type string expected number"
validate_argument_type(value, 'number', 2, 'repeat_count')
Raises an error when the value is the incorrect type, uses a consistent error message format
Parameters:-- Output: "Bad argument #2 to "<anon>"; argument is of type number expected string or table"
validate_argument_type(value, {'string', 'table'}, 2)
-- Output: "Bad argument #2 to "<anon>"; "player" is of type number expected string or table"
validate_argument_type(value, {'string', 'table'}, 2, 'player')
Will raise an error if called during runtime
Usage:error_if_runtime()
Will raise an error if the function is a closure
Parameters:error_if_runetime_closure(func)
Tests if a string contains a given substring.
Parameters: Returns:-- Test if a string contains a sub string
local found = string_contains(str, 'foo')
Used to resolve a value that could also be a function returning that value
Parameters:-- Default value handling
-- if default value is not a function then it is returned
-- if default value is a function then it is called with the first argument being self
local value = Common.resolve_value(self.defaut_value, self)
Converts a varible into its boolean value, nil and false return false
Parameters:local bool = cast_bool(var)
Returns either the second or third argument based on the first argument
Parameters:ternary(input_string == 'test', 'Input is test', 'Input is not test')
Returns a string for a number with comma seperators
Parameters:comma_value(input_number)
Sets a table element to value while also returning value.
Parameters:-- Set and return value
local value = set_and_return(players, player.name, player.online_time)
Writes a table object to a file in json format
Parameters:-- Write a lua table as a json to script-outpt/dump
write_json('dump', tbl)
Calls a require that will not error if the file is not found
Parameters:local file = opt_require('file.not.present') -- will not cause any error
-- Require a file without causing errors, for when a file might not exist
local Module = opt_require 'expcore.common'
Returns a desync safe file path for the current file
Parameters:-- Get the current file path
local file_path = get_file_path()
Converts a table to an enum
Parameters:-- Make an enum
local colors = enum{
'red',
'green',
'blue'
}
Returns the closest match to the input
Parameters:-- Get the element that includes "foo"
local value = auto_complete(tbl, "foo")
-- Get the element with a key that includes "foo"
local value = auto_complete(tbl, "foo", true)
-- Get the key with that includes "foo"
local key = auto_complete(tbl, "foo", true, true)
Returns a valid string with the name of the actor of a command.
Parameters:-- Get the current actor
local player_name = get_actor()
Returns a message with valid chat tags to change its colour
Parameters:-- Use factorio tags to color a chat message
local message = format_chat_colour('Hello, World!', { r=355, g=100, b=100 })
Returns a message with valid chat tags to change its colour, using localization
Parameters:-- Use factorio tags and locale strings to color a chat message
local message = format_chat_colour_localized('Hello, World!', { r=355, g=100, b=100 })
Returns the players name in the players color
Parameters:-- Format a players name using the players color as a string
local message = format_chat_player_name(game.player, true)
Will return a value of any type to the player/server console, allows colour for in-game players
Parameters:-- Return a value to the current actor, rcon included
player_return('Hello, World!')
-- Return a value to the current actor, with color
player_return('Hello, World!', 'green')
-- Return to a player other than the current
player_return('Hello, World!', nil, player)
Formats tick into a clean format, denominations from highest to lowest -- time will use : separates -- when a denomination is false it will overflow into the next one
Parameters:-- Output: "0h 5m"
local time = format_time(18000, { hours=true, minutes=true, string=true })
-- Output: "0 hours and 5 minutes"
local time = format_time(18000, { hours=true, minutes=true, string=true, long=true })
-- Output: "00:05:00"
local time = format_time(18000, { hours=true, minutes=true, seconds=true, string=true })
-- Output: "--:--:--"
local time = format_time(18000, { hours=true, minutes=true, seconds=true, string=true, null=true })
Copies items to the position and stores them in the closest entity of the type given -- Copies the items by prototype name, but keeps them in the original inventory
Parameters:-- Copy all the items in a players inventory and place them in chests at {0, 0}
copy_items_stack(game.player.get_main_inventory().get_contents())
Moves items to the position and stores them in the closest entity of the type given -- Differs from move_items by accepting a table of LuaItemStack and transferring them into the inventory - not copying
Parameters:-- Copy all the items in a players inventory and place them in chests at {0, 0}
move_items_stack(game.player.get_main_inventory())
Prints a colored value on a location, color is based on the value.
nb: src is below but the gradent has been edited https://github.com/Refactorio/RedMew/blob/9184b2940f311d8c9c891e83429fc57ec7e0c4a2/map_gen/maps/diggy/debug.lua#L31
Parameters:-- Place a 0 at {0, 0}
print_grid_value(0, game.player.surface, { x=0, y=0 })
Clears all flying text entities on a surface
Parameters:-- Remove all flying text on the surface
clear_flying_text(game.player.surface)