Lua Lua类层次结构

class("newclass", parentclass)

Lua 在lua中调用函数和assigment

self.object = domainname.object2:function(param1,param2)

Lua 秒延迟

--- Delay for a number of seconds.
-- @param delay Number of seconds
function delay_s(delay)
    delay = delay or 1
    local time_to = os.time() + delay
    while os.time() < time_to do end
end

Lua Dir(对象内省像Python的目录)

--- Returns string representation of object obj
-- @return String representation of obj
function dir(obj,level)
    local s,t = '', type(obj)
    
    level = level or '  '

    if (t=='nil') or (t=='boolean') or (t=='number') or (t=='string') then
        s = tostring(obj)
        if t=='string' then
            s = '"' .. s .. '"'
        end
    elseif t=='function' then s='function'
    elseif t=='userdata' then s='userdata'
    elseif t=='thread' then s='thread'
    elseif t=='table' then
        s = '{'
        for k,v in pairs(obj) do
            local k_str = tostring(k)
            if type(k)=='string' then
                k_str = '["' .. k_str .. '"]'
            end
            s = s .. k_str .. ' = ' .. dir(v,level .. level) .. ', '
        end
        s = string.sub(s, 1, -3)
        s = s .. '}'
    end
    
    return s
end

Lua str_rpad - 填充右侧的字符串

--- Pads str to length len with char from left
string.rpad = function(str, len, char)
    if char == nil then char = ' ' end
    return string.rep(char, len - #str) .. str
end

Lua str_lpad - 填充左侧的字符串

--- Pads str to length len with char from right
string.lpad = function(str, len, char)
    if char == nil then char = ' ' end
    return str .. string.rep(char, len - #str)
end

Lua in_table / in_array

function in_table ( e, t )
for _,v in pairs(t) do
if (v==e) then return true end
end
return false
end

Lua 血液规格旋转宏

#showtooltip
/castsequence reset=combat/shift/alt Icy Touch, Plague Strike,Heart Strike,Heart Strike, Death Strike, Death Strike,Heart Strike,Heart Strike, Heart Strike
/startattack

Lua 转储表

function dump(table, indent)
    local indent = indent or ''
    for key, value in pairs(table) do
        io.write(indent, '[', tostring(key), ']') 
        if type(value) == "table" then
            io.write(':\n') dump(value, indent .. '\t')
        else
            io.write(' = ', tostring(value), '\n')
        end
    end
end

Lua “按键排序”表迭代器

-- "Sorted by key" table iterator 
-- Extracted from http://www.lua.org/pil/19.3.html

function pairsKeySorted(t, f)
    local a = {}    
    for n in pairs(t) do
        table.insert(a, n)
    end    
    table.sort(a, f)
    
    local i = 0      -- iterator variable
    local iter = function ()   -- iterator function
        i = i + 1
        if a[i] == nil then
            return nil
        else
            return a[i], t[a[i]]
        end
    end
    
    return iter
end