Global Module
These functions have been added at the global scope rather than any specific module. #printf printf(string fmt, ...)

(C-style) Format and print a string. This acts much like io.write() except that it does not append the newline character (\n).

'fmt' should be a string that details how you want the output formatted. Special patterns (prefixed with %) are formatted and replaced with the additional arguments passed to this function.

Example:
printf("Hello %s\n", "World"); -- %s replaces a string, so this prints: Hello World printf("An integer: %d\n", 100); -- Prints: An integer: 100 printf("A float: %f\n", 1.23); -- Prints: A float: 1.230000 printf("Float 2: %2.3f\n", 1.23); -- %2.3f specifies (at least) 2 leading numbers and 3 after. Prints: Float 2: 1.230 printf("Hex: 0x%X\n", 12345678); -- Prints: 0xBC614E printf("Character: %c\n", 65); -- ASCII 65 is A, so prints: Character: A printf("My name is %s, I am %d years old, and %d%% awesome.\n", "Trogdor", "1478", 100); -- You can use any number of replacements. -- %% prints a single % character; use this to avoid confusing the interpreter -- "1478" (or any number represented as a string) is still convertible to a number so %d still works

For format specifiers, please refer to C++ reference for printf(). or Lua 5.3 lua_pushfstring().

#sprintf string sprintf()

(C-style) Format a string and return the result. This function is essentially the same as printf(), but instead returns the result as a string rather than printing it directly to the console.

Example:
local myString = sprintf("%d + %d = %d", 3, 5, 3+5); print(myString); -- Prints: 3 + 5 = 8
#unpack2 table unpack2

Takes a variable argument list and returns them as a table. That is, any arguments fed into this function become a list.

Example code:
local function func(...) local tab = unpack2(...); for i,v in pairs(tab) do print(i, v); end end func(17, "Banana", 3.14159265359, func, {a=1});
Example output:
1 17 2 Banana 3 3.14159265359 4 function: 037e8528 5 table: 0383fcb0
#include [variable result] include(string filename) [variable result] include(string filename, boolean force)

"include" (dofile) a file. If 'force' is false or ungiven, this will not include the same file multiple times. The returned value is whatever the file returns (if it is to be executed).

You may use this to run additional scripts relative to the script executing this file. This is useful to pull in additional code for your project before executing the contents of the current file.

Example:
-- Include our Animal class before we try to create an instance of it include("classes/animal.lua"); -- It is now safe to use code from classes/animal.lua function macro.init() animal = Animal(); end

You may also use this function to load and return data from a file. In this case, you may want to specify true for 'force' as you may want to retreive this data more than once. If 'force' is left unspecified, the data will only be returned on the first attempt.

data.lua
return { aWholeBunchOfDataHere = {1, 2, 3, --[[...]]} }
Another script in the same directory as data.lua
local myData = include("data.lua", true); -- myData now contains an actual table of the data from data.lua

NOTE: This function modified the CWD, both before and after loading the target file.

Page last updated at 2018-09-25 20:47:39


Copyright 2024