如何在 Lua 中执行带参数的回调函数

我正试图为回调函数附加一些额外的逻辑,但问题在于回调还需要一个参数。

我有这个函数,最后一个参数是回调函数:

game.showAlert(
  "Title",
  "Message",
  function( event )
    if (event.action == "clicked") then
      --Do stuff
    end
  end
)

我正在尝试通过以下方式在回调后注入一些其他逻辑:

game.showAlert = function (title, message, tblButtons, tblListener)
  function onComplete()
    --执行传入的监听器
    if (tblListener and type(tblListener) == "function") then
        tblListener()
    end

    --做其他的事情
  end

  native.showAlert(title, message, tblButtons, onComplete)
end

当我调用 tblListener() 时,我会收到一个错误,说明 eventnil,这是有道理的。

问题是,我该如何按照最初的意图正确地执行回调并传递正确的参数和上下文?

点赞