Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Lua function callback technique


May 12, 2021 Lua


Table of contents


Tip 1:

local a = {};function b()    print("Hello World")enda["sell"] = {callFunc =b}a["sell"].callFunc()

Tip 2:

Use lua's own unpack:

Explanation: Expand the constant array (table with only consecutive numbers subsenter) into a string of return values, but there is nothing you can do about table making keys with strings or something else.

function unpackex(tbl, args)    local ret = {}    for _,v in ipairs(args) do        table.insert(ret, tbl[v])    end    return unpack(ret)endprint(unpackex({one = {"one", "two", "three"}, two = "T" , three = "TH"},{"one", "two", "three"}))

Output: table: 00ABC2D0TTH