DirkScripts
Math
âšī¸ Shared Module â extends the global math table
All functions are added directly to the Lua math global. Accessible via math.* or lib.math.*.
#math.round
Rounds a number to the specified decimal places.
luamath.round(3.14159, 2) -- 3.14 math.round(1567) -- 1567
| Parameter | Type | Required | Description |
|---|---|---|---|
| value | number | string | yes | Value to round |
| places | number | string | no | Decimal places (default: 0) |
#math.clamp
Clamps a value between lower and upper bounds. Auto-swaps boundaries if inverted.
luamath.clamp(15, 0, 10) -- 10
| Parameter | Type | Required | Description |
|---|---|---|---|
| val | number | yes | Value to clamp |
| lower | number | yes | Lower bound |
| upper | number | yes | Upper bound |
#math.interp
Simple linear interpolation between two values.
luamath.interp(0, 100, 0.5) -- 50.0
| Parameter | Type | Required | Description |
|---|---|---|---|
| start | number | yes | Start value |
| finish | number | yes | End value |
| factor | number | yes | Interpolation factor (0â1) |
#math.lerp
Returns an iterator that yields interpolated values each frame over a duration.
luafor value, step in math.lerp(0.0, 100.0, 1000) do print(value, step) -- step goes 0 â 1 end
| Parameter | Type | Required | Description |
|---|---|---|---|
| start | number | vector | table | yes | Start value |
| finish | number | vector | table | yes | End value (same type as start) |
| duration | number | yes | Duration in milliseconds |
The final iteration yields (finish, 1), then nil.
#math.groupdigits
Formats a number with digit grouping.
luamath.groupdigits(1000000) -- "1,000,000" math.groupdigits(1000000, '.') -- "1.000.000"
| Parameter | Type | Required | Description |
|---|---|---|---|
| number | number | string | yes | Number to format |
| separator | string | no | Separator character (default: ,) |
#math.toscalars
Parses numeric scalars from a string.
lualocal x, y, z = math.toscalars("1.0, 2.5, 3.0")
| Parameter | Type | Required | Description |
|---|---|---|---|
| input | string | yes | String containing numeric values |
| min | number | no | Minimum clamp value |
| max | number | no | Maximum clamp value |
| round | boolean | number | no | true to round all, or number to only round the first N values |
#math.tovector
Converts a string or table to a vector type.
luamath.tovector("1.0, 2.5, 3.0") -- vector3(1.0, 2.5, 3.0) math.tovector({x = 1, y = 2}) -- vector2(1, 2)
| Parameter | Type | Required | Description |
|---|---|---|---|
| input | string | table | yes | Input value â tables support {x,y,z} or {x=,y=,z=} |
| min | number | no | Minimum clamp |
| max | number | no | Maximum clamp |
| round | boolean | number | no | Round flag |
Returns vector2, vector3, vector4, or number depending on the number of components.
#math.normaltorotation
Converts a surface normal vector to a rotation vector.
lualocal rot = math.normaltorotation(surfaceNormal) -- vector3(pitch, yaw, 0)
#math.torgba
Converts a string or table to an RGBA vector4.
lualocal rgba = math.torgba("255, 128, 0, 0.5") -- vector4(255, 128, 0, 0.5)
RGB values are 0â255, alpha is 0â1.
#math.hextorgb
Converts a hex colour string to RGB components.
lualocal r, g, b = math.hextorgb("#FF00AA")
#math.tohex
Converts a number to a hex string.
luamath.tohex(255) -- "0xff" math.tohex(255, true) -- "0xFF"
| Parameter | Type | Required | Description |
|---|---|---|---|
| n | number | yes | Number to convert |
| upper | boolean | no | Use uppercase hex letters |
