在Lua函数中填充表不会在外部反映出来

时间:2012-11-09 15:00:13

标签: function reference lua lua-table

我正在尝试在一个函数中填充一个表,然后使用它在稍后调用中接收的值,在使用它之后删除每个值。由于某种原因,值不在函数调用之间持久存在。调用subscribeToService()时,应填充包含句柄的表,然后在unsubscribeToService()中填充。

local subscriptionSignals_AudioMixerManager = {"volumeLevel", "muteStatus", "fadeLevel", "balanceLevel", "bassLevel", "trebleLevel", "midLevel", "AVCLevel", "activeAudioSrc", "interruptSrc"}
local signalHandlers_AudioMixerManager = {["volumeLevel"] = OnSignal, ["muteStatus"] = OnSignal, ["fadeLevel"] = onSignal, ["balanceLevel"] = OnSignal, ["bassLevel"] = OnSignal, ["trebleLevel"] = OnSignal, ["midLevel"] = OnSignal, ["AVCLevel"] = OnSignal, ["activeAudioSrc"] = OnSignal, ["interruptSrc"] = OnSignal}
local subscriptionHandles_AudioMixerManager = {}

local subscriptionSignals_AudioManager = {"targetSource"}
local signalHandlers_AudioManager = {["targetSource"] = onAudioSourceChange}
local subscriptionHandles_AudioManager = {}

local function subscribeToService(objectPath, signalNames, signalHandlers, subscriptionHandles)
    print("Subscribing...")
    for i,v in ipairs(signalNames) do
            subscriptionHandles[v] = service.subscribe(objectPath, v, signalHandlers[v]);
            print(v .. " handle: ")
            print(subscriptionHandles[v])
        end
    print("Done subscribing")
end

local function unsubscribeFromService(subscriptionHandles)
    print("Unsubscribing...")
    for i,v in ipairs(subscriptionHandles) do
            print("Entered for loop")
            service.unsubscribe(v)
            print(v)
            subscriptionHandles[i] = nil
        end
    print("Done unsubscribing")
end

local function subscribe()
    subscribeToService(AudioMixerManager, subscriptionSignals_AudioMixerManager, signalHandlers_AudioMixerManager, subscriptionHandles_AudioMixerManager)
    subscribeToService(AudioManager, subscriptionSignals_AudioManager, signalHandlers_AudioManager, subscriptionHandles_AudioManager)

    local result, error = service.invoke(AudioMixerManager, "registerMuteClient", {serviceName="audioSettings-interrupt"})
    muteClientID = result.id
    result, error = service.invoke(AudioMixerManager, "registerMuteClient", {serviceName="audioSettings-modechange"})
    muteClientID2 = result.id
end

local function unsubscribe()
    unsubscribeFromService(subscriptionHandles_AudioMixerManager);
    unsubscribeFromService(subscriptionHandles_AudioManager);
end

我在调用subscribe()后再输出unsubscribe()

时输出以下内容
Subscribing...
volumeLevel handle:
userdata: 18f418
muteStatus handle:
userdata: 18f490
fadeLevel handle:
userdata: 18f508
balanceLevel handle:
userdata: 18f580
bassLevel handle:
userdata: 18f5f8
trebleLevel handle:
userdata: 18f670
midLevel handle:
userdata: 18f6e8
AVCLevel handle:
userdata: 18f760
activeAudioSrc handle:
userdata: 18f7d8
interruptSrc handle:
userdata: 18f850
Done subscribing
Subscribing...
targetSource handle:
userdata: 18f8c8
Done subscribing

Unsubscribing...
Done unsubscribing
Unsubscribing...
Done unsubscribing

看起来它甚至没有进入unsubscribeFromService()中的for循环,我认为这意味着它传递的表因某种原因是空的。在Lua中是否存在表格不能通过引用传递的情况?它可以解释这种行为,因为我已经分解了较小的组件并单独测试它们,它们似乎正在工作。

2 个答案:

答案 0 :(得分:4)

这是一个基本错误,只是因为您拥有的代码量而过于复杂。考虑这个简化的例子:

local t = {}
t["asdf"] = {}

for i,v in ipairs(t) do
    print(i, v)
end

这不打印任何东西!但是给了什么?肯定有一些东西!

问题是ipairs()只迭代表中的 numeric 索引,但是您使用字符串键插入值。您需要切换到使用pairs(),或者使用数字索引(可能不是您想要的)将值插入您的subscriptionHandles表。

答案 1 :(得分:2)

表总是在Lua中通过引用传递。这意味着subscribe()unsubscribe()来电之间发生的事情是:

  • subscriptionHandles_AudioMixerManagersubscriptionHandles_AudioManager分配了一个空表。
  • subscriptionHandles_AudioMixerManagersubscriptionHandles_AudioManager的所有元素都设置为nil。