Basic

_G

Description: Global variable that holds the global environment (all global functions/variables)
Example:
1
print(_G.tostring(true)) -- tostring is global, which makes it accessible via _G as well
Copied!

setfenv

Description: sets the environment for a function to use, rather than _G.
Usage: setfenv(f)
Example:
1
function x()
2
zzz.x = 5
3
print() -- will not work here, the environment is not _G.
4
end
5
​
6
setfenv(x, {zzz = {x = 10}})
Copied!

getfenv

Description: returns the environment in use by given function or stack level, default 1.
Usage: getfenv([f])
Example:
1
function x()
2
zzz.x = 5
3
print() -- will not work here, the environment is not _G.
4
end
5
​
6
setfenv(x, {zzz = {x = 10}})
7
print(getfenv(x)) -- environment is {zzz = {x = 10}}
Copied!

setmetatable

Description: sets the metatable for given table
Usage: setmetatable(table, metatable)
Example:
1
local t = {}
2
setmetatable(t, {__call = print})
Copied!

getmetatable

Description: gets the metatable of specified table, returning the __metatable metamethod's value if applicable
Usage: getmetatable(table)
Example:
1
local t = {}
2
setmetatable(t, {__call = print})
3
print(getmetatable(t)) -- {__call = print}
4
setmetatable(t, {__metatable = {x = 5}})
5
print(getmetatable(t)) -- {x = 5}
Copied!

rawset

Description: sets a value in the table at specified index without invoking any metamethods
Usage: rawset(table, index, value)
Example:
1
local t = {}
2
setmetatable(t, {__newindex = function(self, k, v)
3
print('Value set at index '.. tostring(k) ..' to value '.. tostring(v))
4
end})
5
​
6
t.x = 5 -- Value set at index x to value 5
7
rawset(t, 'xx', 50) -- prints nothing, __newindex is not be invoked
Copied!

rawget

Description: returns a value from a table at given index without invoking any metamethods
Usage: rawget(table, index)
Example:
1
local t = {}
2
setmetatable(t, {__index = function(self, k, v)
3
print('Accessing value at index '.. tostring(k))
4
end})
5
​
6
print(t.x) -- Accessing value at index x
7
print(rawget(t, 'x')) -- nothing, __index is not invoked
Copied!

rawequal

Description: compares two values using == without invoking any metamethod
Usage: rawequal(v1, v2)
Example:
1
local mt = {__eq = function(a, b)
2
print('Checking equality ( '.. tostring(a) ..' vs '.. tostring(b) ..' )')
3
return rawequal(a, b)
4
end}
5
​
6
local t = {}
7
setmetatable(t, mt)
8
​
9
local t2 = {}
10
setmetatable(t2, mt)
11
​
12
print(t == t2) -- Checking equality ( table: 00f28b00 vs table: 00f28a10 )
13
print(rawequal(t, t2)) -- nothing, __eq is not invoked
Copied!

pairs

Description: iterator to traverse over a table in any order possible
Usage: pairs(t)
Example:
1
local t = {1, 2, 3}
2
for k, v in pairs(t) do
3
print(k, v)
4
end
Copied!

ipairs

Description: iterator to traverse over a table in sequence
Usage: ipairs(t)
Example:
1
local t = {1, 2, 3}
2
for k, v in ipairs(t) do
3
print(k, v)
4
end
Copied!

loadstring

Description: loads a Lua chunk
Usage: loadstring(string [, chunkName])
Example:
1
local f = loadstring([[return function() print(123) end]])
2
f() -- 123
Copied!

loadfile

Description: loads a Lua chunk from specified file, or from standard input if filename is not specified
Usage: loadfile([filename])
Example:
1
-- x.lua
2
return function() print(123) end
3
​
4
-- other file.lua
5
local f = loadfile("x.lua")
6
f() -- 123
Copied!

next

Description: returns the next key, value pair in table starting from specified index, otherwise index, is nil
Usage: next(table [, index])
Example:
1
local t = {1, 2, 3}
2
print(next(t, 2)) -- 3, 3
3
print(next(t)) -- 1, 1
Copied!

pcall

Description: calls a function in a protected state, returning any errors if they happen, otherwise returns true if successful plus the returned values from f
Usage: pcall(f, ...)
Example:
1
function x(n)
2
return x + n
3
end
4
​
5
local success, ret = pcall(x, 5)
6
print(success, ret) -- false C:\Users\user\lua_file.lua:2: attempt to perform arithmetic on a function value (global 'x')
Copied!

xpcall

Description: calls a function in a protected state, using err as the error handler and returning true if no errors happen, otherwise returns false plus the result from err
Usage: xpcall(f, err)
Example:
1
function x(n)
2
return x + n
3
end
4
​
5
local function x_error_handler(error)
6
print(error)
7
return 123
8
end
9
​
10
local success, ret = xpcall(x, x_error_handler)
11
print(success, ret)
Copied!
Output:
1
C:\Users\user\lua_file.lua:2: attempt to perform arithmetic on a function value (global 'x')
2
false 123
Copied!

unpack

Description: unpacks a table in sequence, starting from i and ending with j (1, #table respectively by default), returning all values from it
Usage: unpack(table [, i [, j]])
Example:
1
local t = {1, 2, 3, 4, 5}
2
print(unpack(t)) -- 1, 2, 3, 4, 5
3
print(unpack(t, 2, 4)) -- 2, 3, 4
Copied!

type

Description: returns the data type of given value
Usage: type(value)
Example:
1
print(type(123)) -- number
2
print(type({})) -- table
3
print(type(nil)) -- nil
4
print(type('')) -- string
5
print(type(true)) -- boolean
Copied!

tonumber

Description: converts value to a number if possible
Usage: tonumber(val [,base])
Example:
1
local x = '5'
2
print(tonumber(x)) -- 5
3
print(type(tonumber(x))) -- number
4
print(type(x)) -- string
Copied!

tostring

Description: converts value to a string
Usage: tostring(val)
Example:
1
print(tostring({})) -- table: 00ee88a8
2
print(tostring(5)) -- 5
3
​
4
print('This is a boolean: '.. tostring(true)) -- This is a boolean: true
Copied!
​
​
Last modified 2yr ago