1

我试图在一个函数中填充一个表,然后使用它在以后的调用中收到的值,在使用后删除每个值。由于某种原因,这些值不会在函数调用之间保持不变。保存句柄的表应在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

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

4

2 回答 2

4

这是一个基本错误,只是由于那里的代码量过于复杂。考虑这个简化的例子:

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

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

这不打印任何东西!但是什么给了?t里面肯定有东西!

问题是 ipairs() 仅迭代表中的数字索引,但您使用字符串键插入值。要么您需要改用pairs(),要么使用数字索引(可能不是您想要的)将值插入您的subscriptionHandles 表中。

于 2012-11-09T16:29:02.573 回答
2

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

  • subscriptionHandles_AudioMixerManager并被subscriptionHandles_AudioManager分配一个空表。
  • subscriptionHandles_AudioMixerManager和的所有元素subscriptionHandles_AudioManager都设置为零。
于 2012-11-09T16:24:21.910 回答