-
Notifications
You must be signed in to change notification settings - Fork 0
Miscellaneous Utility Functions
This section documents the miscellaneous helper functions provided by LibSFUtils. These utilities simplify common Lua tasks, provide safer function invocation, assist with argument handling, formatting, addon metadata, chat output, and several ESO-specific conveniences.
Creates an iterator for a variable argument list without requiring Lua 5.2's table.pack().
Unlike iterating over a temporary table directly, the iterator returns the current argument index, the argument value, and the total number of arguments on each iteration.
for index, value, total in sfutil.iter_args(...) do
...
end| Parameter | Description |
|---|---|
... |
Any number of arguments. |
An iterator function returning:
| Return | Description |
|---|---|
index |
Current argument index. |
value |
Current argument value. |
total |
Total number of arguments originally supplied. |
for i, value, total in sfutil.iter_args("a", 5, true) do
d(i, value, total)
endOutput
1 a 3
2 5 3
3 true 3
Creates a closure that permanently binds a table as the first parameter passed to a callback.
This is useful when passing object methods as callbacks while preserving the desired self value.
local fn = sfutil.closure(callback, selfTable)| Parameter | Description |
|---|---|
callback |
Function to invoke later. |
selfTable |
Table passed as the first parameter to the callback. |
A callable function.
local callback = sfutil.closure(MyObject.Update, MyObject)
callback(10, 20)Internally this performs
MyObject.Update(MyObject, 10, 20)Wraps an existing function so all future calls pass through a wrapper function.
This is useful for:
- debugging
- profiling
- logging
- instrumentation
- temporary hooks
Global function
sfutil.WrapFunction("FunctionName", wrapper)Namespaced function
sfutil.WrapFunction(namespace, "FunctionName", wrapper)function wrapper(originalFunction, ...)The wrapper receives the original function as its first argument and may call it or completely replace its behavior.
sfutil.WrapFunction("MyFunction",
function(original, ...)
d("Before")
local result = original(...)
d("After")
return result
end)Executes a function inside pcall() and safely returns up to ten return values without creating a temporary table.
This version minimizes memory allocations and is useful for frequently called functions.
local ok, result1, result2 = sfutil.safeCall10(fn, ...)| Return | Description |
|---|---|
ok |
true if the call succeeded. |
| remaining | Up to ten return values from the function. |
On failure
false, errorMessagelocal ok, value = sfutil.safeCall10(MyFunction)Executes a function safely using pcall().
Unlike safeCall10(), this version preserves every return value by storing them temporarily in a table.
local ok, ... = sfutil.safeCall(fn, ...)| Return | Description |
|---|---|
ok |
Success flag. |
| remaining | All values returned by the function. |
local ok, a, b, c, d = sfutil.safeCall(MyFunction)Converts a boolean value into "true" or "false".
sfutil.bool2str(true)Returns
true
Converts a string representation into a boolean.
Accepted true values:
"true""1"
Everything else returns false.
sfutil.str2bool("true")Returns
truePerforms a stricter boolean test than Lua's built-in truthiness.
Returns true only for the following values:
true"true"1"1"
Everything else returns false.
sfutil.isTrue("1")Returns
trueReturns default only if value is nil.
Unlike Lua's or operator, false is preserved.
local enabled = sfutil.nilDefault(saved.enabled, false)Returns default if the value is either:
nil- an empty string
local name = sfutil.nilDefaultStr(userName, "Unknown")Creates or populates a table containing information about the current addon and player.
| Field | Description |
|---|---|
addonName |
Addon name. |
server |
Current world/server name. |
account |
Account display name. |
charId |
Character ID. |
charName |
Character name. |
fmtCharName |
Formatted character name. |
API |
Current ESO API version. |
local meta = sfutil.addonMeta("MyAddon")Converts a number of seconds into an HH:MM:SS string.
sfutil.secondsToClock(3665)Returns
01:01:05
Creates a colored prefix suitable for addon chat messages.
local prefix = sfutil.initSystemMsgPrefix("MyAddon")Produces something similar to
[MyAddon]
with color formatting applied.
Displays a colored message in the ESO system chat.
sfutil.systemMsg(prefix, "Settings loaded.")addonChatter is a lightweight helper object that manages normal and debug output in ESO chat.
It automatically applies colors and prefixes to messages.
local chat = sfutil.addonChatter:New("MyAddon")chat:systemMessage("Addon initialized.")Enable debugging
chat:enableDebug()Disable debugging
chat:disableDebug()Toggle debugging
chat:toggleDebug()Print a debug message
chat:debugMsg("Loaded profile.")Returns
trueor
falseReturns the string
true
or
false
chat:setNormalColor(sfutil.hex.white)
chat:setDebugColor(sfutil.hex.orange)Displays a formatted list of slash commands.
Example
chat:slashHelp("Commands", {
{"/my reload", "Reload settings"},
{"/my reset", "Reset profile"},
})Produces
Commands
/my reload = Reload settings
/my reset = Reset profile
These utility functions provide convenient wrappers around many common Lua and ESO programming tasks, including:
- Safe function invocation
- Argument iteration
- Closure creation
- Function wrapping
- Boolean conversion
- Default value handling
- Addon metadata collection
- Time formatting
- System chat output
- Debug message management
They are intended to reduce boilerplate while providing consistent behavior throughout an addon.