【发布时间】:2023-03-04 16:43:01
【问题描述】:
我遇到了古老的unpack bug,我在 Lua 中有一个可以包含 nil 值的数组,我想用 nil 值解压数组;这似乎是不可能的。这个逻辑的替代方案是什么?
这是我尝试运行的代码
function InputSystem:poll(name, ...)
local system = self:findComponent(name)
local values, arr = {...}, {}
for i, v in pairs(values) do
arr[#arr+1] = system[v]
end
--If the first guy is null this does not work!! WHY
return unpack(arr, 1, table.maxn(values))
end
我的想法是我动态地轮询我的输入系统,以便我只返回我想要的值:
local dragged,clicked,scrolled = self.SystemManager:findSystem('Input'):poll('Mouse', 'Dragged', 'Clicked', 'Scrolled')
有什么想法吗?谢谢
编辑:
我似乎没有完全理解 Lua。我想返回与 ... 传入的相同数量的变量,但是在循环中如果找不到该属性,我认为它会将其设置为 nil,但这似乎是错误的。
function InputSystem:poll(name, ...)
local system = self:findComponent(name)
local values, arr = {...}, {}
for i, v in pairs(values) do
arr[#arr+1] = system[v] --If not found set nil
end
--I want this to return the length of ... in variables
--Example I pass 'Dragged', 'Clicked' I would want it to return nil {x:1,y:1}
return unpack(arr, 1, table.maxn(values))
end
显然我是 Lua 大师...
【问题讨论】:
-
一个没有被指定做你想做的事情的函数不会让它出错......
-
@Deduplicator 我不是那个意思,我是在引用以前的帖子时遇到同样的问题。是我的逻辑错了。