Methods
Utils.absClamp(value: number, min: number, max: number)
Limits the absolute value of a number between two positive numbers, then sets it to its original sign.
@param value
— The value to limit.
@param min
— The minimum bound. If the absolute value of the specified value is less than this number, it is set to it.
@param max
— The maximum bound. If the absolute value of the specified value is greater than this number, it is set to it.
@return result
— The new limited number.
Arguments:
value: number | The value to limit. |
min: number | The minimum bound. If the absolute value of the specified value is less than this number, it is set to it. |
max: number | The maximum bound. If the absolute value of the specified value is greater than this number, it is set to it. |
Returns:
result: number | The new limited number. |
Utils.absMax(a: number, b: number)
Returns the number further from zero.
@param a
— The first number to compare.
@param b
— The second number to compare.
@return result
— The specified number that was further from zero than the other.
Arguments:
a: number | The first number to compare. |
b: number | The second number to compare. |
Returns:
result: number | The specified number that was further from zero than the other. |
Utils.absMin(a: number, b: number)
Returns the number closer to zero.
@param a
— The first number to compare.
@param b
— The second number to compare.
@return result
— The specified number that was closer to zero than the other.
Arguments:
a: number | The first number to compare. |
b: number | The second number to compare. |
Returns:
result: number | The specified number that was closer to zero than the other. |
Utils.absoluteToLocalPath(prefix: string, image: string, path: string)
TODO: this function is a mess please comment it later
@param prefix
— base directory of images in the mod
@param image
— raw image path specified by tileset/map
@param path
— path of tileset/map, image
is relative to this
@return final_path
— nil in case of error
Arguments:
prefix: string | base directory of images in the mod |
image: string | raw image path specified by tileset/map |
path: string | path of tileset/map, |
Returns:
final_path: string|nil | nil in case of error |
Utils.all(tbl: [], func: (fun( <T)boolean)?)
Returns whether every value in a table is true, iterating numerically.
@param tbl
— The table to iterate through.
@param func
— If provided, each value of the table will instead be passed into the function, whose returned value will be considered instead of the table value itself.
@return result
— Whether every value was true or not.
Arguments:
tbl: [] | The table to iterate through. |
func: (fun( <T)boolean)? | If provided, each value of the table will instead be passed into the function, whose returned value will be considered instead of the table value itself. |
Returns:
result: boolean | Whether every value was true or not. |
Utils.angle(x1: number|Object, y1: number|Object, x2: number|Object, y2: number|Object)
Returns the angle from one point to another, or from one object's position to another's.
@param x1
— The horizontal position of the first point.
@param y1
— The vertical position of the first point.
@param x2
— The horizontal position of the second point.
@param y2
— The vertical position of the second point.
@return angle
— The angle from the first point to the second point.
Arguments:
x1: number|Object | The horizontal position of the first point. |
y1: number|Object | The vertical position of the first point. |
x2: number|Object | The horizontal position of the second point. |
y2: number|Object | The vertical position of the second point. |
Returns:
angle: number | The angle from the first point to the second point. |
Utils.angleDiff(a: number, b: number)
Returns the distance between two angles, properly accounting for wrapping around.
@param a
— The first angle to compare.
@param b
— The second angle to compare.
@return diff
— The difference between the two angles.
Arguments:
a: number | The first angle to compare. |
b: number | The second angle to compare. |
Returns:
diff: number | The difference between the two angles. |
Utils.any(tbl: [], func: (fun( <T)boolean)?)
Returns whether any individual value in a table is true, iterating numerically.
@param tbl
— The table to iterate through.
@param func
— If provided, each value of the table will instead be passed into the function, whose returned value will be considered instead of the table value itself.
@return result
— Whether any value was true or not.
Arguments:
tbl: [] | The table to iterate through. |
func: (fun( <T)boolean)? | If provided, each value of the table will instead be passed into the function, whose returned value will be considered instead of the table value itself. |
Returns:
result: boolean | Whether any value was true or not. |
Utils.approach(val: number, target: number, amount: number)
Moves the specified value towards a target value by a specified amount, without exceeding the target.
If the target is less than the value, then the amount will be subtracted from the value instead to approach it.
@param val
— The initial value.
@param target
— The target value to approach.
@param amount
— The amount the initial value should approach the target by.
@return result
— The new value. If the value would have passed the target value, it will instead be set to the target.
Arguments:
val: number | The initial value. |
target: number | The target value to approach. |
amount: number | The amount the initial value should approach the target by. |
Returns:
result: number | The new value. If the value would have passed the target value, it will instead be set to the target. |
Utils.approachAngle(val: number, target: number, amount: number)
Moves the specified angle towards a target angle by a specified amount, properly accounting for wrapping around.
Will always approach in the direction with the shorter distance.
@param val
— The initial angle.
@param target
— The target angle to approach.
@param amount
— The amount the initial angle should approach the target by.
@return result
— The new angle. If the angle would have passed the target angle, it will instead be set to the target.
Arguments:
val: number | The initial angle. |
target: number | The target angle to approach. |
amount: number | The amount the initial angle should approach the target by. |
Returns:
result: number | The new angle. If the angle would have passed the target angle, it will instead be set to the target. |
Utils.between(val: number, a: number, b: number, include: boolean?)
Returns whether a value is between two numbers.
@param val
— The value to compare.
@param a
— The start value of the range.
@param b
— The end value of the range.
@param include
— Determines whether the function should consider being equal to a range value to be "between". (Defaults to false)
@return result
— Whether the value was within the range.
Arguments:
val: number | The value to compare. |
a: number | The start value of the range. |
b: number | The end value of the range. |
include: boolean? | Determines whether the function should consider being equal to a range value to be "between". (Defaults to false) |
Returns:
result: boolean | Whether the value was within the range. |
Utils.ceil(value: number, to: number?)
Rounds the specified value up to the nearest integer.
@param value
— The value to round.
@param to
— If specified, the value will instead be rounded up to the nearest multiple of this number.
@return result
— The rounded value.
Arguments:
value: number | The value to round. |
to: number? | If specified, the value will instead be rounded up to the nearest multiple of this number. |
Returns:
result: number | The rounded value. |
Utils.clamp(val: number, min: number, max: number)
Limits the specified value to be between 2 bounds, setting it to be the respective bound if it exceeds it.
@param val
— The value to limit.
@param min
— The minimum bound. If the value is less than this number, it is set to it.
@param max
— The maximum bound. If the value is greater than this number, it is set to it.
@return result
— The new limited number.
Arguments:
val: number | The value to limit. |
min: number | The minimum bound. If the value is less than this number, it is set to it. |
max: number | The maximum bound. If the value is greater than this number, it is set to it. |
Returns:
result: number | The new limited number. |
Utils.clampMap(val: number, min_a: number, max_a: number, min_b: number, max_b: number, mode: ("in-back"|"in-bounce"|"in-circ"|"in-cubic"|"in-elastic"...(+66))?)
Maps a value between a specified range to its equivalent position in a new range.
@param val
— The initial value in the initial range.
@param min_a
— The start value of the initial range.
@param max_a
— The end value of the initial range.
@param min_b
— The start value of the new range.
@param max_b
— The end value of the new range.
@param mode
— If specified, the value's new position will be eased into the new range based on the percentage of its position in its initial range.
@return result
— The value within the new range.
mode:
| "linear"
| "in-quad"
| "in-cubic"
| "in-quart"
| "in-quint"
| "in-sine"
| "in-expo"
| "in-circ"
| "in-back"
| "in-bounce"
| "in-elastic"
| "out-quad"
| "out-cubic"
| "out-quart"
| "out-quint"
| "out-sine"
| "out-expo"
| "out-circ"
| "out-back"
| "out-bounce"
| "out-elastic"
| "in-out-quad"
| "in-out-cubic"
| "in-out-quart"
| "in-out-quint"
| "in-out-sine"
| "in-out-expo"
| "in-out-circ"
| "in-out-back"
| "in-out-bounce"
| "in-out-elastic"
| "inQuad"
| "outQuad"
| "inOutQuad"
| "outInQuad"
| "inCubic"
| "outCubic"
| "inOutCubic"
| "outInCubic"
| "inQuart"
| "outQuart"
| "inOutQuart"
| "outInQuart"
| "inQuint"
| "outQuint"
| "inOutQuint"
| "outInQuint"
| "inSine"
| "outSine"
| "inOutSine"
| "outInSine"
| "inExpo"
| "outExpo"
| "inOutExpo"
| "outInExpo"
| "inCirc"
| "outCirc"
| "inOutCirc"
| "outInCirc"
| "inElastic"
| "outElastic"
| "inOutElastic"
| "outInElastic"
| "inBack"
| "outBack"
| "inOutBack"
| "outInBack"
| "inBounce"
| "outBounce"
| "inOutBounce"
| "outInBounce"
Arguments:
val: number | The initial value in the initial range. |
min_a: number | The start value of the initial range. |
max_a: number | The end value of the initial range. |
min_b: number | The start value of the new range. |
max_b: number | The end value of the new range. |
mode: ("in-back"|"in-bounce"|"in-circ"|"in-cubic"|"in-elastic"...(+66))? | If specified, the value's new position will be eased into the new range based on the percentage of its position in its initial range. |
Returns:
result: number | The value within the new range. |
Utils.clampWrap(val: number, min: number, max: number)
Limits the specified value to be between 2 bounds, wrapping around if it exceeds it.
@param val
— The value to wrap.
@param min
— The minimum bound. If not specified, defaults to 1
for array wrapping.
@param max
— The maximum bound.
@return result
— The new wrapped number.
Arguments:
val: number | The value to wrap. |
min: number | The minimum bound. If not specified, defaults to |
max: number | The maximum bound. |
Returns:
result: number | The new wrapped number. |
Utils.clear(tbl: table)
Empties a table of all defined values.
@param tbl
— The table to clear.
Arguments:
tbl: table | The table to clear. |
Utils.colliderFromShape(parent: Object, data: table, x: number?, y: number?, properties: table?)
Creates a Collider based on a Tiled object shape.
@param parent
— The object that the new Collider should be parented to.
@param data
— The Tiled shape data.
@param x
— An optional value defining the horizontal position of the collider.
@param y
— An optional value defining the vertical position of the collider.
@param properties
— A table defining additional properties for the collider.
@return collider
— The new Collider instance.
Arguments:
parent: Object | The object that the new Collider should be parented to. |
data: table | The Tiled shape data. |
x: number? | An optional value defining the horizontal position of the collider. |
y: number? | An optional value defining the vertical position of the collider. |
properties: table? | A table defining additional properties for the collider. |
Returns:
collider: Collider | The new Collider instance. |
Utils.contains(str: string, filter: string)
Returns whether a string contains a given substring.
Note: This function uses find
, so special characters must be escaped with a %
.
@param str
— The string to check.
@param filter
— The substring that the string may contain.
@return result
— Whether the string contained the specified substring.
Arguments:
str: string | The string to check. |
filter: string | The substring that the string may contain. |
Returns:
result: boolean | Whether the string contained the specified substring. |
Utils.containsValue(tbl: table, val: any)
Whether the table contains the specified value.
@param tbl
— The table to check the value from.
@param val
— The value to check.
@return result
— Whether the table contains the specified value.
Arguments:
tbl: table | The table to check the value from. |
val: any | The value to check. |
Returns:
result: boolean | Whether the table contains the specified value. |
Utils.copy(tbl: table?>, deep: boolean?, seen: table?)
Makes a new copy of a table, giving it all of the same values.
@param tbl
— The table to copy.
@param deep
— Whether tables inside the specified table should be copied as well.
@param seen
— (Used internally) A table of values used to keep track of which objects have been cloned.
@return new
— The new table.
Arguments:
tbl: table?> | The table to copy. |
deep: boolean? | Whether tables inside the specified table should be copied as well. |
seen: table? | (Used internally) A table of values used to keep track of which objects have been cloned. |
Returns:
new: table?> | The new table. |
Utils.copyInto(new_tbl: table, tbl: table, deep: boolean?, seen: table?)
Copies the values of one table into a different one.
@param new_tbl
— The table receiving the copied values.
@param tbl
— The table to copy values from.
@param deep
— Whether tables inside the specified table should be copied as well.
@param seen
— (Used internally) A table of values used to keep track of which objects have been cloned.
Arguments:
new_tbl: table | The table receiving the copied values. |
tbl: table | The table to copy values from. |
deep: boolean? | Whether tables inside the specified table should be copied as well. |
seen: table? | (Used internally) A table of values used to keep track of which objects have been cloned. |
Returns:
1: nil |
Utils.dist(x1: number, y1: number, x2: number, y2: number)
Returns the distance between two points.
@param x1
— The horizontal position of the first point.
@param y1
— The vertical position of the first point.
@param x2
— The horizontal position of the second point.
@param y2
— The vertical position of the second point.
@return dist
— The linear distance from the first point to the second point.
Arguments:
x1: number | The horizontal position of the first point. |
y1: number | The vertical position of the first point. |
x2: number | The horizontal position of the second point. |
y2: number | The vertical position of the second point. |
Returns:
dist: number | The linear distance from the first point to the second point. |
Utils.dump(o: any)
Returns a string converting a table value into readable text. Useful for debugging table values.
@param o
— The value to convert to a string.
@return result
— The newly generated string.
Arguments:
o: any | The value to convert to a string. |
Returns:
result: string | The newly generated string. |
Utils.ease(a: number, b: number, t: number, mode: "in-back"|"in-bounce"|"in-circ"|"in-cubic"|"in-elastic"...(+66))
Returns a value eased between two numbers, determined by a percentage from 0 to 1.
@param a
— The start value of the range.
@param b
— The end value of the range.
@param t
— The percentage (from 0 to 1) that determines the point on the specified range.
@param mode
— The ease type to use between the two values. (Refer to https://easings.net/)
mode:
| "linear"
| "in-quad"
| "in-cubic"
| "in-quart"
| "in-quint"
| "in-sine"
| "in-expo"
| "in-circ"
| "in-back"
| "in-bounce"
| "in-elastic"
| "out-quad"
| "out-cubic"
| "out-quart"
| "out-quint"
| "out-sine"
| "out-expo"
| "out-circ"
| "out-back"
| "out-bounce"
| "out-elastic"
| "in-out-quad"
| "in-out-cubic"
| "in-out-quart"
| "in-out-quint"
| "in-out-sine"
| "in-out-expo"
| "in-out-circ"
| "in-out-back"
| "in-out-bounce"
| "in-out-elastic"
| "inQuad"
| "outQuad"
| "inOutQuad"
| "outInQuad"
| "inCubic"
| "outCubic"
| "inOutCubic"
| "outInCubic"
| "inQuart"
| "outQuart"
| "inOutQuart"
| "outInQuart"
| "inQuint"
| "outQuint"
| "inOutQuint"
| "outInQuint"
| "inSine"
| "outSine"
| "inOutSine"
| "outInSine"
| "inExpo"
| "outExpo"
| "inOutExpo"
| "outInExpo"
| "inCirc"
| "outCirc"
| "inOutCirc"
| "outInCirc"
| "inElastic"
| "outElastic"
| "inOutElastic"
| "outInElastic"
| "inBack"
| "outBack"
| "inOutBack"
| "outInBack"
| "inBounce"
| "outBounce"
| "inOutBounce"
| "outInBounce"
Arguments:
a: number | The start value of the range. |
b: number | The end value of the range. |
t: number | The percentage (from 0 to 1) that determines the point on the specified range. |
mode: "in-back"|"in-bounce"|"in-circ"|"in-cubic"|"in-elastic"...(+66) | The ease type to use between the two values. (Refer to https://easings.net/) |
Returns:
1: number|unknown |
Utils.endsWith(value: string|table>, suffix: string|table>)
Returns whether a string ends with the specified substring, or a table ends with the specified series of values.
The function will also return a second value, created by copying the inital value and removing the suffix.
@param value
— The value to check the end of.
@param suffix
— The prefix that should be checked.
@return success
— Whether the value ended with the specified suffix.
@return rest
— A new value created by removing the suffix substring or values from the initial value. If the result was unsuccessful, this value will simply be the initial unedited value.
Arguments:
value: string|table> | The value to check the end of. |
suffix: string|table> | The prefix that should be checked. |
Returns:
success: boolean | Whether the value ended with the specified suffix. |
rest: string|table> | A new value created by removing the suffix substring or values from the initial value. If the result was unsuccessful, this value will simply be the initial unedited value. |
Utils.equal(a: any, b: any, deep: boolean?)
Returns whether two tables have an equivalent set of values.
@param a
— The first table to compare.
@param b
— The second table to compare.
@param deep
— Whether table values within these tables should also be compared using Utils.equal()
.
@return success
— Whether the sets of values for the two tables were equivalent.
Arguments:
a: any | The first table to compare. |
b: any | The second table to compare. |
deep: boolean? | Whether table values within these tables should also be compared using |
Returns:
success: boolean | Whether the sets of values for the two tables were equivalent. |
Utils.facingFromAngle(angle: number)
Returns a facing direction nearest to the specified angle.
@param angle
— The angle to convert.
@return direction
— The facing direction the specified angle is closest to.
direction:
| "right"
| "down"
| "left"
| "up"
Arguments:
angle: number | The angle to convert. |
Returns:
direction: "down"|"left"|"right"|"up" | The facing direction the specified angle is closest to. |
Utils.filter(tbl: [], filter: fun( <T)boolean)
Returns a new table containing only values that a function returns true for.
@param tbl
— An array of values.
@param filter
— A function that should return true
for all values in the table to keep, and false
for values to discard.
@return result
— A new array containing only approved values.
Arguments:
tbl: [] | An array of values. |
filter: fun( <T)boolean | A function that should return |
Returns:
result: [] | A new array containing only approved values. |
Utils.filterInPlace(tbl: [], filter: fun( <T)boolean)
Removes values from a table if a function does not return true for them.
@param tbl
— An array of values.
@param filter
— A function that should return true
for all values in the table to keep, and false
for values to discard.
Arguments:
tbl: [] | An array of values. |
filter: fun( <T)boolean | A function that should return |
Utils.findFiles(folder: any, base: any, path: any)
Arguments:
folder: any | |
base: any | |
path: any |
Returns:
1: table |
Utils.flatten(tbl: [][], deep: boolean?)
Merges a list of tables containing values into a single table containing each table's contents.
@param tbl
— The array of tables to merge.
@param deep
— If true, tables contained inside listed tables will also be merged.
@return result
— The new table containing all values.
Arguments:
tbl: [][] | The array of tables to merge. |
deep: boolean? | If true, tables contained inside listed tables will also be merged. |
Returns:
result: [] | The new table containing all values. |
Utils.flipTable(tbl: table)
Flips the values of a 2-dimensional array, such that its columns become its rows, and vice versa.
As an example, the following table:
{
{1, 2},
{3, 4},
}
would result in this when passed into the function:
{
{1, 3},
{2, 4},
}
@param tbl
— The table array to flip the values of.
@return result
— The new flipped array.
Arguments:
tbl: table | The table array to flip the values of. |
Returns:
result: table | The new flipped array. |
Utils.floor(value: number, to: number?)
Rounds the specified value down to the nearest integer.
@param value
— The value to round.
@param to
— If specified, the value will instead be rounded down to the nearest multiple of this number.
@return result
— The rounded value.
Arguments:
value: number | The value to round. |
to: number? | If specified, the value will instead be rounded down to the nearest multiple of this number. |
Returns:
result: number | The rounded value. |
Utils.format(str: string, tbl: table)
This function substitutes values from a table into a string using placeholders in the form of {key}
or {}
, where the latter indexes the table by number.
@param str
— The string to substitute values into.
@param tbl
— The table containing the values to substitute.
@return result
— The formatted string.
Arguments:
str: string | The string to substitute values into. |
tbl: table | The table containing the values to substitute. |
Returns:
result: string | The formatted string. |
Utils.getAnyCase(t: table<any, <V>>, key: string)
Returns the value found for a string index, ignoring case-sensitivity.
@param t
— The table to get the value from.
@param key
— The index to check within the table.
@return value
— The value found at the specified index, or nil
if no similar index was found.
Arguments:
t: table<any, <V>> | The table to get the value from. |
key: string | The index to check within the table. |
Returns:
value: <V>? | The value found at the specified index, or |
Utils.getClassName(class: table, parent_check: boolean?)
Returns the name of a given class, using the name of the global variable for the class.
If it cannot find a global variable associated with the class, it will instead return the name of the class it extends, along with the class's ID.
@param class
— The class instance to check.
@param parent_check
— Whether the function should only return the extended class, and not attach the class's ID, if the class does not have a global name.
@return name
— The name of the class, or nil
if it cannot find one.
Arguments:
class: table | The class instance to check. |
parent_check: boolean? | Whether the function should only return the extended class, and not attach the class's ID, if the class does not have a global name. |
Returns:
name: string? | The name of the class, or |
Utils.getCombinedText(text: string|table)
Concatenates exclusively string values within a table.
@param text
— The table of values to combine.
@return result
— The concatenated string.
Arguments:
text: string|table | The table of values to combine. |
Returns:
result: string | The concatenated string. |
Utils.getDirname(path: string)
See: https //stackoverflow.com/a/12191225
Arguments:
path: string |
Returns:
dirname: string |
Utils.getFacingVector(facing: "down"|"left"|"right"|"up")
Returns two numbers defining a vector based on the specified direction.
@param facing
— The facing direction to get the vector of.
@return x
— The horizontal factor of the specified direction.
@return y
— The vertical factor of the specified direction.
facing:
| "right"
| "down"
| "left"
| "up"
Arguments:
facing: "down"|"left"|"right"|"up" | The facing direction to get the vector of. |
Returns:
x: number | The horizontal factor of the specified direction. |
y: number | The vertical factor of the specified direction. |
Utils.getFilesRecursive(dir: string, ext: string?)
Returns a table of file names within the specified directory, checking subfolders as well.
@param dir
— The file path to check, relative to the LÖVE Kristal directory.
@param ext
— If specified, only files with the specified extension will be returned, and the extension will be stripped. (eg. "png"
will only return .png files)
@return result
— The table of file names.
Arguments:
dir: string | The file path to check, relative to the LÖVE Kristal directory. |
ext: string? | If specified, only files with the specified extension will be returned, and the extension will be stripped. (eg. |
Returns:
result: string[] | The table of file names. |
Utils.getIndex(t: [], value: <T>)
Returns the position of a specified value found within an array.
@param t
— The array to get the position from.
@param value
— The value to find the position of.
@return position
— The position found for the specified value.
Arguments:
t: [] | The array to get the position from. |
value: <T> | The value to find the position of. |
Returns:
position: number? | The position found for the specified value. |
Utils.getKey(t: , <V, value: <V>)
Returns the non-numerical key of a specified value found within a table.
@param t
— The table to get the key from.
@param value
— The value to find the key of.
@return key
— The key found for the specified value.
Arguments:
t: , <V | The table to get the key from. |
value: <V> | The value to find the key of. |
Returns:
key: <K>? | The key found for the specified value. |
Utils.getKeys(t: , any)
Returns a list of every key in a table.
@param t
— The table to get the keys from.
@return result
— An array of each key in the table.
Arguments:
t: , any | The table to get the keys from. |
Returns:
result: [] | An array of each key in the table. |
Utils.getLineIntersect(l1p1x: number, l1p1y: number, l1p2x: number, l1p2y: number, l2p1x: number, l2p1y: number, l2p2x: number, l2p2y: number, seg1: boolean?, seg2: boolean?)
Returns the point at which two lines intersect.
@param l1p1x
— The horizontal position of the first point for the first line.
@param l1p1y
— The vertical position of the first point for the first line.
@param l1p2x
— The horizontal position of the second point for the first line.
@param l1p2y
— The vertical position of the second point for the first line.
@param l2p1x
— The horizontal position of the first point for the second line.
@param l2p1y
— The vertical position of the first point for the second line.
@param l2p2x
— The horizontal position of the second point for the second line.
@param l2p2y
— The vertical position of the second point for the second line.
@param seg1
— If true, the first line will be treated as a line segment instead of an infinite line.
@param seg2
— If true, the second line will be treated as a line segment instead of an infinite line.
@return x
— If the lines intersected, this will be the horizontal position of the intersection; otherwise, this value will be false
.
@return y
— If the lines intersected, this will be the vertical position of the intersection; otherwise, this will be a string describing why the lines did not intersect.
y:
| "The lines are parallel."
| "The lines don't intersect."
Arguments:
l1p1x: number | The horizontal position of the first point for the first line. |
l1p1y: number | The vertical position of the first point for the first line. |
l1p2x: number | The horizontal position of the second point for the first line. |
l1p2y: number | The vertical position of the second point for the first line. |
l2p1x: number | The horizontal position of the first point for the second line. |
l2p1y: number | The vertical position of the first point for the second line. |
l2p2x: number | The horizontal position of the second point for the second line. |
l2p2y: number | The vertical position of the second point for the second line. |
seg1: boolean? | If true, the first line will be treated as a line segment instead of an infinite line. |
seg2: boolean? | If true, the second line will be treated as a line segment instead of an infinite line. |
Returns:
x: boolean|number | If the lines intersected, this will be the horizontal position of the intersection; otherwise, this value will be |
y: number|"The lines are parallel."|"The lines don't intersect." | If the lines intersected, this will be the vertical position of the intersection; otherwise, this will be a string describing why the lines did not intersect. |
Utils.getPointOnPath(path: number, y, t: number)
Returns a point at a certain distance along a path.
@param path
— An array of tables with X and Y values each, defining the coordinates of each point on the path.
@param t
— The distance along the path that the point should be at.
@return x
— The horizontal position of the point found on the path.
@return y
— The vertical position of the point found on the path.
Arguments:
path: number, y | An array of tables with X and Y values each, defining the coordinates of each point on the path. |
t: number | The distance along the path that the point should be at. |
Returns:
x: number | The horizontal position of the point found on the path. |
y: number | The vertical position of the point found on the path. |
Utils.getPolygonBounds(points: number[][])
Returns the bounds of a rectangle containing every point of a polygon.
@param points
— An array of tables with two number values each, defining the points of a polygon.
@return x
— The horizontal position of the bounds.
@return y
— The vertical position of the bounds.
@return width
— The width of the bounds.
@return height
— The height of the bounds.
Arguments:
points: number[][] | An array of tables with two number values each, defining the points of a polygon. |
Returns:
x: number | The horizontal position of the bounds. |
y: number | The vertical position of the bounds. |
width: number | The width of the bounds. |
height: number | The height of the bounds. |
Utils.getPolygonEdges(points: number[][])
Returns a table of line segments based on a set of polygon points.
@param points
— An array of tables with two number values each, defining the points of a polygon.
@return edges
— An array of tables containing four values each, defining line segments describing the edges of a polygon.
Arguments:
points: number[][] | An array of tables with two number values each, defining the points of a polygon. |
Returns:
edges: number[], [2] | An array of tables containing four values each, defining line segments describing the edges of a polygon. |
Utils.getPolygonOffset(points: number[][], dist: number)
Returns a new polygon with points offset outwards by a certain distance.
@param points
— An array of tables with two number values each, defining the points of a polygon.
@param dist
— The distance to offset the points by. If this value is negative, the points will be offset inwards.
@return A
— new polygon array.
Arguments:
points: number[][] | An array of tables with two number values each, defining the points of a polygon. |
dist: number | The distance to offset the points by. If this value is negative, the points will be offset inwards. |
Returns:
A: number[][] | new polygon array. |
Utils.group(tbl: [], count: number)
Creates a table grouping values of a table into a series of tables.
@param tbl
— The table to group values from.
@param count
— The amount of values that should belong in each group. If the table does not divide evenly, the final group of the returned table will be incomplete.
@return result
— The table containing the grouped values.
Arguments:
tbl: [] | The table to group values from. |
count: number | The amount of values that should belong in each group. If the table does not divide evenly, the final group of the returned table will be incomplete. |
Returns:
result: [][] | The table containing the grouped values. |
Utils.hexToRgb(hex: string, value: number?)
Converts a hex color string to an RGBA color table.
@param hex
— The string to convert to RGB. The string must be formatted with a # at the start, eg. "#ff00ff"
.
@param value
— An optional number specifying the alpha the returned table should have.
@return rgba
— The converted RGBA table.
Arguments:
hex: string | The string to convert to RGB. The string must be formatted with a # at the start, eg. |
value: number? | An optional number specifying the alpha the returned table should have. |
Returns:
rgba: number[] | The converted RGBA table. |
Utils.hook(target: table, name: string, hook: fun( fun(...any), ...any), exact_func: boolean?)
Replaces a function within a class with a new function.
Also allows calling the original function, allowing you to add code to the beginning or end of existing functions.
Utils.hook()
should always be called in Mod:init()
. An example of how to hook a function is as follows:
-- this code will hook 'Object:setPosition(x, y)', and will be run whenever that function is called
-- all class functions receive the object instance as the first argument. in this function, i name that argument 'obj', and it refers to whichever object is calling 'setPosition()'
Utils.hook(Object, "setPosition", function(orig, obj, x, y)
-- calls the original code (setting its position as normal)
orig(obj, x, y)
-- sets 'new_x' and 'new_y' variables for the object instance
obj.new_x = x
obj.new_y = y
end)
@param target
— The class variable containing the function you want to hook.
@param name
— The name of the function to hook.
@param hook
— The function containing the new code to replace the old code with. Receives the original function as an argument, followed by the arguments the original function receives.
@param exact_func
— (Used internally) Whether the function should be replaced exactly, or whether it should be replaced with a function that calls the hook function. Should not be specified by users.
Arguments:
target: table | The class variable containing the function you want to hook. |
name: string | The name of the function to hook. |
hook: fun( fun(...any), ...any) | The function containing the new code to replace the old code with. Receives the original function as an argument, followed by the arguments the original function receives. |
exact_func: boolean? | (Used internally) Whether the function should be replaced exactly, or whether it should be replaced with a function that calls the hook function. Should not be specified by users. |
Utils.hookScript(include: (string|function|Class>|<T>)?)
@param include
— The class to extend from. If passed as a string, will be looked up from the current registry (e.g. scripts/data/actors
if creating an actor) or the global namespace.
@return class
— The new class, extended from include
if provided.
@return super
— Allows calling methods from the base class. self
must be passed as the first argument to each method.
Arguments:
include: (string|function|Class>|<T>)? | The class to extend from. If passed as a string, will be looked up from the current registry (e.g. |
Returns:
class: function|Class> | The new class, extended from |
super: function|Class>|function|Class>> | Allows calling methods from the base class. |
Utils.hslToRgb(h: number, s: number, l: number)
Converts HSL values to RGB values. Both HSL and RGB should be values between 0 and 1.
@param h
— The hue value of the HSL color.
@param s
— The saturation value of the HSL color.
@param l
— The lightness value of the HSL color.
@return r
— The red value of the converted color.
@return g
— The green value of the converted color.
@return b
— The blue value of the converted color.
Arguments:
h: number | The hue value of the HSL color. |
s: number | The saturation value of the HSL color. |
l: number | The lightness value of the HSL color. |
Returns:
r: number | The red value of the converted color. |
g: number | The green value of the converted color. |
b: number | The blue value of the converted color. |
Utils.hsvToRgb(h: number, s: number, v: number)
Converts HSV values to RGB values. Both HSV and RGB should be values between 0 and 1.
@param h
— The hue value of the HSV color.
@param s
— The saturation value of the HSV color.
@param v
— The 'value' value of the HSV color.
@return r
— The red value of the converted color.
@return g
— The green value of the converted color.
@return b
— The blue value of the converted color.
Arguments:
h: number | The hue value of the HSV color. |
s: number | The saturation value of the HSV color. |
v: number | The 'value' value of the HSV color. |
Returns:
r: number | The red value of the converted color. |
g: number | The green value of the converted color. |
b: number | The blue value of the converted color. |
Utils.isArray(tbl: table)
Returns whether a table contains exclusively numerical indexes.
@param tbl
— The table to check.
@return result
— Whether the table contains only numerical indexes or not.
Arguments:
tbl: table | The table to check. |
Returns:
result: boolean | Whether the table contains only numerical indexes or not. |
Utils.isFacingAngle(facing: "down"|"left"|"right"|"up", angle: number)
Returns whether the specified angle is considered to be in the specified direction.
@param facing
— The facing direction to compare.
@param angle
— The angle to compare.
@return result
— Whether the angle is closest to the specified facing direction.
facing:
| "right"
| "down"
| "left"
| "up"
Arguments:
facing: "down"|"left"|"right"|"up" | The facing direction to compare. |
angle: number | The angle to compare. |
Returns:
result: boolean | Whether the angle is closest to the specified facing direction. |
Utils.isPolygonClockwise(points: number[][])
Determines whether a polygon's points are clockwise or counterclockwise.
@param points
— An array of tables with two number values each, defining the points of a polygon.
@return result
— Whether the polygon is clockwise or not.
Arguments:
points: number[][] | An array of tables with two number values each, defining the points of a polygon. |
Returns:
result: boolean | Whether the polygon is clockwise or not. |
Utils.iterClass(class: table)
Iterates through the fields of a class (e.g. pairs
) excluding special class variables and functions
Arguments:
class: table |
Returns:
1: fun( table<<K? <K)<K | |
2: table |
Utils.keyFromNumber(t: table, number: number)
Returns a non-numerical index based on its position in a pairs()
iterator.
@param t
— The table to get the index from.
@param number
— The numerical position the index will be at.
@return index
— The index found at the specified position.
Arguments:
t: table | The table to get the index from. |
number: number | The numerical position the index will be at. |
Returns:
index: number? | The index found at the specified position. |
Utils.len(input: string)
Returns the length of a UTF-8 string, erroring if it's invalid.
Arguments:
input: string |
Returns:
length: integer |
Utils.lerp(a: number|table>, b: number|table>, t: number, oob: boolean?)
Returns a value between two numbers, determined by a percentage from 0 to 1.
If given a table, it will lerp between each value in the table.
@param a
— The start value of the range.
@param b
— The end value of the range.
@param t
— The percentage (from 0 to 1) that determines the point on the specified range.
@param oob
— If true, then the percentage can be values beyond the range of 0 to 1.
@return result
— The new value from the range.
Arguments:
a: number|table> | The start value of the range. |
b: number|table> | The end value of the range. |
t: number | The percentage (from 0 to 1) that determines the point on the specified range. |
oob: boolean? | If true, then the percentage can be values beyond the range of 0 to 1. |
Returns:
result: number|table> | The new value from the range. |
Utils.lerpPoint(x1: number, y1: number, x2: number, y2: number, t: number, oob: boolean?)
Lerps between two coordinates.
@param x1
— The horizontal position of the first point.
@param y1
— The vertical position of the first point.
@param x2
— The horizontal position of the second point.
@param y2
— The vertical position of the second point.
@param t
— The percentage (from 0 to 1) that determines the new point on the specified range between the specified points.
@param oob
— If true, then the percentage can be values beyond the range of 0 to 1.
@return new_x
— The horizontal position of the new point.
@return new_y
— The vertical position of the new point.
Arguments:
x1: number | The horizontal position of the first point. |
y1: number | The vertical position of the first point. |
x2: number | The horizontal position of the second point. |
y2: number | The vertical position of the second point. |
t: number | The percentage (from 0 to 1) that determines the new point on the specified range between the specified points. |
oob: boolean? | If true, then the percentage can be values beyond the range of 0 to 1. |
Returns:
new_x: number | The horizontal position of the new point. |
new_y: number | The vertical position of the new point. |
Utils.merge(tbl: table, other: table, deep: boolean?)
Merges the values of one table into another one.
@param tbl
— The table to merge values into.
@param other
— The table to copy values from.
@param deep
— Whether shared table values between the two tables should also be merged.
@return tbl
— The initial table, now containing new values.
Arguments:
tbl: table | The table to merge values into. |
other: table | The table to copy values from. |
deep: boolean? | Whether shared table values between the two tables should also be merged. |
Returns:
tbl: table | The initial table, now containing new values. |
Utils.mergeColor(start_color: number[], end_color: number[], amount: number)
Merges two colors based on a percentage between 0 and 1.
@param start_color
— The first table of RGBA values to merge.
@param end_color
— The second table of RGBA values to merge.
@param amount
— A percentage (from 0 to 1) that determines how much of the second color to merge into the first.
@return result_color
— A new table of RGBA values.
Arguments:
start_color: number[] | The first table of RGBA values to merge. |
end_color: number[] | The second table of RGBA values to merge. |
amount: number | A percentage (from 0 to 1) that determines how much of the second color to merge into the first. |
Returns:
result_color: number[] | A new table of RGBA values. |
Utils.mergeMultiple(: table)
Merges a list of tables into a new table.
@param ...
— The list of tables to merge values from.
@return result
— A new table containing the values of the series of tables provided.
Arguments:
: table | The list of tables to merge values from. |
Returns:
result: table | A new table containing the values of the series of tables provided. |
Utils.numberFromKey(t: table, name: any)
Returns a number based on the position of a specified key in a pairs()
iterator.
@param t
— The table to get the position from.
@param name
— The key to find the position of.
@return position
— The numerical position of the specified index.
Arguments:
t: table | The table to get the position from. |
name: any | The key to find the position of. |
Returns:
position: number? | The numerical position of the specified index. |
Utils.orderedNext(table: , <V, index: <K>?)
Equivalent of the next function, but returns the keys in the alphabetic
order. We use a temporary ordered key table that is stored in the
table being iterated.
See: http //lua-users.org/wiki/SortedIteration
Arguments:
table: , <V | |
index: <K>? |
Returns:
1: <K>|nil | |
2: <V>|nil |
Utils.orderedPairs(t: , <V)
Equivalent of the pairs() function on tables. Allows to iterate in order
@param t
— The table to iterate
See: http //lua-users.org/wiki/SortedIteration
Arguments:
t: , <V | The table to iterate |
Returns:
next: fun( table<<K? <K)<K | |
t: , <V | |
3: nil |
Utils.override(old_func: function, new_func: fun( <T))
Returns a function that calls a new function, giving it an older function as an argument.
Essentially, it's a version of Utils.hook()
that works with local functions.
@param old_func
— The function to be passed into the new function.
@param new_func
— The new function that will be called by the result function.
@return result_func
— A function that will call the new function, providing the original function as an argument, followed by any other arguments that this function receives.
Arguments:
old_func: function | The function to be passed into the new function. |
new_func: fun( <T) | The new function that will be called by the result function. |
Returns:
result_func: function | A function that will call the new function, providing the original function as an argument, followed by any other arguments that this function receives. |
Utils.padString(str: string, len: number, beginning: boolean?, with: string?)
Returns a string with a specified length, filling it with empty spaces by default. Used to make strings consistent lengths for UI.
If the specified string has a length greater than the desired length, it will not be adjusted.
@param str
— The string to extend.
@param len
— The amount of characters the returned string should be.
@param beginning
— If true, the beginning of the string will be filled instead of the end.
@param with
— If specified, the string will be filled with this specified string, instead of with spaces.
@return result
— The new padded result.
Arguments:
str: string | The string to extend. |
len: number | The amount of characters the returned string should be. |
beginning: boolean? | If true, the beginning of the string will be filled instead of the end. |
with: string? | If specified, the string will be filled with this specified string, instead of with spaces. |
Returns:
result: string | The new padded result. |
Utils.parseColorProperty(property: string)
Converts a Tiled color property to an RGBA color table.
@param property
— The property string to convert.
@return rgba
— The converted RGBA table.
Arguments:
property: string | The property string to convert. |
Returns:
rgba: number[]? | The converted RGBA table. |
Utils.parseFlagProperties(flag: string|nil, inverted: string|nil, value: string|nil, default_value: any, properties: table)
Returns a series of values used to determine the behavior of a flag property for a Tiled event.
@param flag
— The name of the flag property.
@param inverted
— The name of the property used to determine if the flag should be inverted.
@param value
— The name of the property used to determine what the flag's value should be compared to.
@param default_value
— If a property for the value
name is not found, the value will be this instead.
@param properties
— The properties table of a Tiled event's data.
@return flag
— The name of the flag to check.
@return inverted
— Whether the result of the check should be inverted.
@return value
— The value that the flag should be compared to.
Arguments:
flag: string|nil | The name of the flag property. |
inverted: string|nil | The name of the property used to determine if the flag should be inverted. |
value: string|nil | The name of the property used to determine what the flag's value should be compared to. |
default_value: any | If a property for the |
properties: table | The properties table of a Tiled event's data. |
Returns:
flag: string | The name of the flag to check. |
inverted: boolean | Whether the result of the check should be inverted. |
value: any | The value that the flag should be compared to. |
Utils.parsePropertyList(id: string, properties: table)
Returns a table with values based on Tiled properties.
The function will check for a series of numbered properties starting with the specified id
string, eg. "id1"
, followed by "id2"
, etc.
@param id
— The name the series of properties should all start with.
@param properties
— The properties table of a Tiled event's data.
@return result
— The list of property values found.
Arguments:
id: string | The name the series of properties should all start with. |
properties: table | The properties table of a Tiled event's data. |
Returns:
result: table | The list of property values found. |
Utils.parsePropertyMultiList(id: string, properties: table)
Returns an array of tables with values based on Tiled properties.
The function will check for a series of layered numbered properties started with the specified id
string, eg. "id1_1"
, followed by "id1_2"
, "id2_1"
, "id2_2"
, etc.
The returned table will contain a list of tables correlating to each individual list.
For example, the first table in the returned array will contain the values for "id1_1"
and "id1_2"
, the second table will contain "id2_1"
and "id2_2"
, etc.
@param id
— The name the series of properties should all start with.
@param properties
— The properties table of a Tiled event's data.
@return result
— The list of property values found.
Arguments:
id: string | The name the series of properties should all start with. |
properties: table | The properties table of a Tiled event's data. |
Returns:
result: table | The list of property values found. |
Utils.parseTileGid(id: number)
Returns the actual GID and flip flags of a tile.
@param id
— The GID of the tile.
@return gid
— The GID of the tile without the flags.
@return flip_x
— Whether the tile should be flipped horizontally.
@return flip_y
— Whether the tile should be flipped vertically.
@return flip_diag
— Whether the tile should be flipped diagonally.
Arguments:
id: number | The GID of the tile. |
Returns:
gid: integer | The GID of the tile without the flags. |
flip_x: boolean | Whether the tile should be flipped horizontally. |
flip_y: boolean | Whether the tile should be flipped vertically. |
flip_diag: boolean | Whether the tile should be flipped diagonally. |
Utils.pick(tbl: [], sort: (fun( <T)boolean)?, remove: boolean?)
Returns a random value from an array.
@param tbl
— An array of values.
@param sort
— If specified, the table will be sorted via Utils.filter(tbl, sort)
before selecting a value.
@param remove
— If true, the selected value will be removed from the given table.
@return result
— The randomly selected value.
Arguments:
tbl: [] | An array of values. |
sort: (fun( <T)boolean)? | If specified, the table will be sorted via |
remove: boolean? | If true, the selected value will be removed from the given table. |
Returns:
result: <T> | The randomly selected value. |
Utils.pickMultiple(tbl: [], amount: number, sort: (fun( <T)boolean)?, remove: any)
Returns multiple random values from an array, not selecting any value more than once.
@param tbl
— An array of values.
@param amount
— The amount of values to select from the table.
@param sort
— If specified, the table will be sorted via Utils.filter(tbl, sort)
before selecting a value.
@return result
— A table containing the randomly selected values.
Arguments:
tbl: [] | An array of values. |
amount: number | The amount of values to select from the table. |
sort: (fun( <T)boolean)? | If specified, the table will be sorted via |
remove: any |
Returns:
result: <T> | A table containing the randomly selected values. |
Utils.random(a: number?, b: number?, c: number?)
Returns a randomly generated decimal value.
If no arguments are provided, the value is between 0 and 1.
If a
is provided, the value is between 0 and a
.
If a
and b
are provided, the value is between a
and b
.
If c
is provided, the value is between a
and b
, rounded to the nearest multiple of c
.
@param a
— The first argument.
@param b
— The second argument.
@param c
— The third argument.
@return rng
— The new random value.
Arguments:
a: number? | The first argument. |
b: number? | The second argument. |
c: number? | The third argument. |
Returns:
rng: number | The new random value. |
Utils.randomAxis()
Returns a table of 2 numbers, defining a vector in a random cardinal direction. (eg. {0, -1}
)
@return vector
— The vector table.
Returns:
vector: number[] | The vector table. |
Utils.randomPointOnBorder(x: number, y: number, w: number, h: number)
Returns the coordinates a random point along the border of the specified rectangle.
@param x
— The horizontal position of the topleft of the rectangle.
@param y
— The vertical position of the topleft of the rectangle.
@param w
— The width of the rectangle.
@param h
— The height of the rectangle.
@return x
— The horizontal position of a random point on the rectangle border.
@return y
— The vertical position of a random point on the rectangle border.
Arguments:
x: number | The horizontal position of the topleft of the rectangle. |
y: number | The vertical position of the topleft of the rectangle. |
w: number | The width of the rectangle. |
h: number | The height of the rectangle. |
Returns:
x: number | The horizontal position of a random point on the rectangle border. |
y: number | The vertical position of a random point on the rectangle border. |
Utils.randomSign()
Returns either -1 or 1.
@return sign
— The new random sign.
Returns:
sign: number | The new random sign. |
Utils.removeDuplicates(tbl: table, deep: boolean?)
Remove duplicate elements from a table.
@param tbl
— The table to remove duplicates from.
@param deep
— Whether tables inside the tbl will also have their duplicates removed.
@return result
— The new table that has its duplicates removed.
Arguments:
tbl: table | The table to remove duplicates from. |
deep: boolean? | Whether tables inside the tbl will also have their duplicates removed. |
Returns:
result: table | The new table that has its duplicates removed. |
Utils.removeFromTable(tbl: table, val: <T>)
Removes the specified value from the table.
@param tbl
— The table to remove the value from.
@param val
— The value to be removed from the table.
@return val
— The now removed value.
Arguments:
tbl: table | The table to remove the value from. |
val: <T> | The value to be removed from the table. |
Returns:
val: <T>? | The now removed value. |
Utils.reverse(tbl: [], group: number?)
Returns a table containing the values of an array in reverse order.
@param tbl
— An array of values.
@param group
— If defined, the values will be grouped into sets of the specified size, and those sets will be reversed.
@return result
— The new table containing the values of the specified array.
Arguments:
tbl: [] | An array of values. |
group: number? | If defined, the values will be grouped into sets of the specified size, and those sets will be reversed. |
Returns:
result: [] | The new table containing the values of the specified array. |
Utils.rgbToHex(rgb: number[])
Converts a table of RGB values to a hex color string.
@param rgb
— The RGB table to convert. Values should be between 0 and 1.
@return hex
— The converted hex string. Formatted with a # at the start, eg. "#ff00ff".
Arguments:
rgb: number[] | The RGB table to convert. Values should be between 0 and 1. |
Returns:
hex: string | The converted hex string. Formatted with a # at the start, eg. "#ff00ff". |
Utils.rgbToHsl(r: number, g: number, b: number)
Converts RGB values to HSL values. Both RGB and HSL should be values between 0 and 1.
@param r
— The red value of the RGB color.
@param g
— The green value of the RGB color.
@param b
— The blue value of the RGB color.
@return h
— The hue value of the converted color.
@return s
— The saturation value of the converted color.
@return l
— The lightness value of the converted color.
Arguments:
r: number | The red value of the RGB color. |
g: number | The green value of the RGB color. |
b: number | The blue value of the RGB color. |
Returns:
h: number | The hue value of the converted color. |
s: number | The saturation value of the converted color. |
l: number | The lightness value of the converted color. |
Utils.rotateTable(tbl: table, ccw: boolean?)
Rotates the values of a 2-dimensional array.
As an example, the following table:
{
{1, 2},
{3, 4},
}
would result in this when passed into the function, rotating it clockwise:
{
{3, 1},
{4, 2},
}
@param tbl
— The table array to rotate the values of.
@param ccw
— Whether the rotation should be counterclockwise.
@return result
— The new rotated array.
Arguments:
tbl: table | The table array to rotate the values of. |
ccw: boolean? | Whether the rotation should be counterclockwise. |
Returns:
result: table | The new rotated array. |
Utils.roughEqual(a: number, b: number)
Returns whether two numbers are roughly equal (less than 0.01 away from each other).
@param a
— The first value to compare.
@param b
— The second value to compare.
@return result
— Whether the two values are roughly equal.
Arguments:
a: number | The first value to compare. |
b: number | The second value to compare. |
Returns:
result: boolean | Whether the two values are roughly equal. |
Utils.round(value: number, to: number?)
Rounds the specified value to the nearest integer.
@param value
— The value to round.
@param to
— If specified, the value will instead be rounded to the nearest multiple of this number.
@return result
— The rounded value.
Arguments:
value: number | The value to round. |
to: number? | If specified, the value will instead be rounded to the nearest multiple of this number. |
Returns:
result: number | The rounded value. |
Utils.roundFromZero(value: number)
Rounds the specified value to the nearest integer away from zero.
@param value
— The value to round.
@return result
— The rounded value.
Arguments:
value: number | The value to round. |
Returns:
result: number | The rounded value. |
Utils.roundToZero(value: number)
Rounds the specified value to the nearest integer towards zero.
@param value
— The value to round.
@return result
— The rounded value.
Arguments:
value: number | The value to round. |
Returns:
result: number | The rounded value. |
Utils.shuffle(tbl: [])
Returns a table containing the values of another table, randomly rearranged.
@param tbl
— An array of values.
@return result
— The new randomly shuffled array.
Arguments:
tbl: [] | An array of values. |
Returns:
result: [] | The new randomly shuffled array. |
Utils.sign(num: number)
Returns the polarity of the specified value: -1 if it's negative, 1 if it's positive, and 0 otherwise.
@param num
— The value to check.
@return sign
— The sign of the value.
Arguments:
num: number | The value to check. |
Returns:
sign: number | The sign of the value. |
Utils.split(str: string, sep: string, remove_empty: boolean?)
Splits a string into a new table of strings using a substring as a separator.
Less optimized than Utils.splitFast()
, but allows separating with multiple characters, and is more likely to work for any string.
@param str
— The string to separate.
@param sep
— The substring used to split the main string.
@param remove_empty
— Whether strings containing no characters shouldn't be included in the result table.
@return result
— The table containing the new split strings.
Arguments:
str: string | The string to separate. |
sep: string | The substring used to split the main string. |
remove_empty: boolean? | Whether strings containing no characters shouldn't be included in the result table. |
Returns:
result: string[] | The table containing the new split strings. |
Utils.splitFast(str: string, sep: string)
Splits a string into a new table of strings using a single character as a separator.
More optimized than Utils.split()
, at the cost of lacking features.
Note: This function uses gmatch
, so special characters must be escaped with a %
.
@param str
— The string to separate.
@param sep
— The character used to split the main string.
@return result
— The table containing the new split strings.
Arguments:
str: string | The string to separate. |
sep: string | The character used to split the main string. |
Returns:
result: string[] | The table containing the new split strings. |
Utils.squishAndTrunc(str: string, font: love.Font, max_width: number, def_scale: number?, min_scale: number?, trunc_affix: (string|false)?)
Finds and returns the scale required to print str
with the font font
, such that it's width does not exceed max_width
.
If a min_scale
is specified, strings that would have to be squished smaller than it will instead have their remaining part truncated.
Returns the input string (truncated if necessary), and the scale to print it at.
@param str
— The string to squish and truncate.
@param font
— The font being used to print the string.
@param max_width
— The maximum width the string should be able to take up.
@param def_scale
— The default scale used to print the string. Defaults to 1
.
@param min_scale
— The minimum scale that the string can be squished to before being truncated.
@param trunc_affix
— The affix added to the string during truncation. If false
, does not add an affix. Defaults to ...
.
@return result
— The truncated result. Returns the original string if it was not truncated.
@return scale
— The scale the result
string should be printed at to fit within the specified width.
trunc_affix:
| false
Arguments:
str: string | The string to squish and truncate. |
font: love.Font | The font being used to print the string. |
max_width: number | The maximum width the string should be able to take up. |
def_scale: number? | The default scale used to print the string. Defaults to |
min_scale: number? | The minimum scale that the string can be squished to before being truncated. |
trunc_affix: (string|false)? | The affix added to the string during truncation. If |
Returns:
result: string | The truncated result. Returns the original string if it was not truncated. |
scale: number | The scale the |
Utils.startsWith(value: string|table>, prefix: string|table>)
Returns whether a string starts with the specified substring, or a table starts with the specified series of values.
The function will also return a second value, created by copying the inital value and removing the prefix.
@param value
— The value to check the beginning of.
@param prefix
— The prefix that should be checked.
@return success
— Whether the value started with the specified prefix.
@return rest
— A new value created by removing the prefix substring or values from the initial value. If the result was unsuccessful, this value will simply be the initial unedited value.
Arguments:
value: string|table> | The value to check the beginning of. |
prefix: string|table> | The prefix that should be checked. |
Returns:
success: boolean | Whether the value started with the specified prefix. |
rest: string|table> | A new value created by removing the prefix substring or values from the initial value. If the result was unsuccessful, this value will simply be the initial unedited value. |
Utils.stringInsert(str1: string, str2: string, pos: number)
Inserts a string into a different string at the specified position.
@param str1
— The string to receive the substring.
@param str2
— The substring to insert into the main string.
@param pos
— The position at which to insert the string.
@return result
— The newly created string.
Arguments:
str1: string | The string to receive the substring. |
str2: string | The substring to insert into the main string. |
pos: number | The position at which to insert the string. |
Returns:
result: string | The newly created string. |
Utils.sub(input: string, from: integer?, to: integer?)
Returns a substring of the specified string, properly accounting for UTF-8.
@param input
— The initial string to get a substring of.
@param from
— The index that the substring should start at. (Defaults to 1, referring to the first character of the string)
@param to
— The index that the substring should end at. (Defaults to -1, referring to the last character of the string)
@return substring
— The new substring.
Arguments:
input: string | The initial string to get a substring of. |
from: integer? | The index that the substring should start at. (Defaults to 1, referring to the first character of the string) |
to: integer? | The index that the substring should end at. (Defaults to -1, referring to the last character of the string) |
Returns:
substring: string | The new substring. |
Utils.tableLength(t: table)
Returns how many indexes a table has, including non-numerical indexes.
@param t
— The table to check.
@return result
— The amount of indexes found.
Arguments:
t: table | The table to check. |
Returns:
result: number | The amount of indexes found. |
Utils.titleCase(str: string)
Converts a string into a new string where the first letter of each "word" (determined by spaces between characters) will be capitalized.
@param str
— The initial string to edit.
@return result
— The new string, in Title Case.
Arguments:
str: string | The initial string to edit. |
Returns:
result: string | The new string, in Title Case. |
Utils.trim(str: string)
Strips a string of padding whitespace.
@param str
— The initial string to edit.
@return result
— The new string, without padding whitespace.
Arguments:
str: string | The initial string to edit. |
Returns:
result: string | The new string, without padding whitespace. |
Utils.unpack(t: [])
Returns every numerically indexed value of a table.
This fixes the issue with unpack()
not returning nil
values.
@param t
— The table to unpack.
@return ...
— The values of the table.
Arguments:
t: [] | The table to unpack. |
Returns:
...: <T> | The values of the table. |
Utils.unpackColor(color: number[])
Returns the values of an RGB table individually.
@param color
— An RGB(A) table.
@return r
— The red value of the color.
@return g
— The green value of the color.
@return b
— The blue value of the color.
@return a
— The alpha value of the color, or 1 if it was not specified.
Arguments:
color: number[] | An RGB(A) table. |
Returns:
r: number | The red value of the color. |
g: number | The green value of the color. |
b: number | The blue value of the color. |
a: number | The alpha value of the color, or 1 if it was not specified. |
Utils.unpackPolygon(points: number[][])
Converts a set of polygon points to a series of numbers.
@param points
— An array of tables with two number values each, defining the points of a polygon.
@return ...
— A series of numbers describing the horizontal and vertical positions of each point in the polygon.
Arguments:
points: number[][] | An array of tables with two number values each, defining the points of a polygon. |
Returns:
...: number | A series of numbers describing the horizontal and vertical positions of each point in the polygon. |
Utils.wave(val: number, min: number, max: number)
Returns a value between two numbers, sinusoidally positioned based on the specified value.
@param val
— The number used to determine the sine position.
@param min
— The start value of the range.
@param max
— The end value of the range.
@return result
— The sine-based value within the range.
Arguments:
val: number | The number used to determine the sine position. |
min: number | The start value of the range. |
max: number | The end value of the range. |
Returns:
result: number | The sine-based value within the range. |
Utils.xor(: any)
XOR (eXclusive OR) logic operation
@param ...
— [conditions]
Arguments:
: any | [conditions] |
Returns:
1: boolean |