找回密码
 register

QQ登录

只需一步,快速开始

搜索
查看: 23|回复: 1

[自写函数] Lua 5.0 新源端 table.pack/table.unpack 使用方法

[复制链接]
  • 打卡等级:本地老炮
  • 打卡总天数:378
  • 打卡月天数:26
  • 打卡总奖励:376
  • 最近打卡:2025-12-26 09:09:14
Waylee 发表于 2025-12-26 09:09 | 显示全部楼层 |阅读模式 | Google Chrome | Windows 10

马上注册,查看网站隐藏内容!!

您需要 登录 才可以下载或查看,没有账号?register

×

本函数用Lua5.0 新源端专用
DLL下载:https://waylee.net/thread-3530-1-1.html


1. table.pack 的用法

1.1 打包一组参数

local t = table.pack(10, 20, 30)

print(t[1], t[2], t[3])   -- 10  20  30
print(t.n)                -- 3

解释:

  • t[1] = 10
  • t[2] = 20
  • t[3] = 30
  • t.n = 3 记录参数个数

1.2 参数里有 nil 也能打包

local t = table.pack(100, nil, 300)

print(t[1])   --> 100
print(t[2])   --> nil
print(t[3])   --> 300
print(t.n)    --> 3

注意:普通 {100, nil, 300} 再用长度是会出问题的,而 table.pack 会单独保存 t.n,不怕 nil。


2. table.unpack 的用法

table.unpack(list [, i [, j]])

2.1 最简单情况:只传 1 个参数

local t = {10, 20, 30}

local a, b, c = table.unpack(t)
print(a, b, c)       -- 10  20  30

默认相当于:

table.unpack(t, 1, len)   -- len 是 table.getn(t) 得到的长度

2.2 指定起始、结束下标

local t = {10, 20, 30, 40, 50}

local a, b = table.unpack(t, 2, 3)
print(a, b)          -- 20  30

local x, y, z = table.unpack(t, 3, 5)
print(x, y, z)       -- 30  40  50

3. pack + unpack 组合(解决 nil 的问题)

因为 table.unpack 的结束下标默认用 table.getn 求出来,中间出现 nil 会“截断”,所以table.pack 搭配时,关键是用 t.n 当结束下标

3.1 正确做法:用 t.n

local t = table.pack(1, nil, 3)

-- 错误用法:
local a, b, c = table.unpack(t)
print(a, b, c)
-- 大概率输出:1  nil  nil  (第 3 个拿不到 3)

-- 正确用法:
local x, y, z = table.unpack(t, 1, t.n)
print(x, y, z)
-- 输出:1  nil  3

口诀:

打包用 table.pack(...)
还原用 table.unpack(t, 1, t.n)


4. 实战例子:保存参数 → 以后再调用

这个在你游戏脚本里会很常用,比如延迟调用/队列调用。

-- 延迟调用列表
local delayed = {}

-- 注册一个将来要执行的调用
function CallLater(fn, ...)
    local args = table.pack(...)
    table.insert(delayed, {fn = fn, args = args})
end

-- 统一执行
function ProcessLater()
    for _, item in ipairs(delayed) do
        local f   = item.fn
        local a   = item.args
        -- 用 t.n 完整还原
        f(table.unpack(a, 1, a.n))
    end
    delayed = {}
end

-- 测试:含 nil 的参数
local function Test(a, b, c)
    print("Test:", a, b, c)
end

CallLater(Test, 100, nil, 300)
CallLater(Test, "A", "B", "C")

ProcessLater()
-- 输出:
-- Test: 100 nil 300
-- Test: A   B   C

5. 对照速查表

功能 写法
打包参数 local t = table.pack(...)
访问第 i 个参数 t[i]
参数总数 t.n
拆包全部 local a,b,c = table.unpack(t, 1, t.n)
拆部分(2 到 4) local x,y,z = table.unpack(t, 2, 4)
  • 打卡等级:本地老炮
  • 打卡总天数:378
  • 打卡月天数:26
  • 打卡总奖励:376
  • 最近打卡:2025-12-26 09:09:14
 楼主| Waylee 发表于 2025-12-26 09:13 | 显示全部楼层 | Google Chrome | Windows 10

Lua 5.0 原生自带函数 unpack 使用教程

基本用法:把表拆成多个返回值

local t = {10, 20, 30}

local a, b, c = unpack(t)
print(a, b, c)        -- 10  20  30

特点:

  • 只看 t[1], t[2], ..., t[n] 这些“顺序索引”;
  • n 来自 table.getn(t) 或内部长度计算;
  • 中间有 nil 会被“截断”。

中间有 nil 的情况

local t = {10, nil, 30}

local a, b, c = unpack(t)
print(a, b, c)        -- 10  nil  nil  (大概率第三个拿不到 30)
您需要登录后才可以回帖 登录 | register

本版积分规则

雪舞知识库 ( 浙ICP备15015590号-1 | 萌ICP备20232229号|浙公网安备33048102000118号 ) |网站地图|天天打卡

GMT+8, 2025-12-26 19:59 , Processed in 0.087337 second(s), 5 queries , Redis On.

Powered by Discuz! X3.5 Licensed

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表