从不同的lua文件调用函数

Art*_*hur 5 lua module function coronasdk

我在menu.lua中有这个

local db = require "databaseconnection"
...
local function onEndBtnRelease()
    local thisandthat = db.getLoggedIn()
    native.showAlert( "Corona SDK", thisandthat.." teststring", { "OK" } )
end
...
Run Code Online (Sandbox Code Playgroud)

这在databaseconnection.lua中

local function getLoggedIn()
    print("Test")
    --[[...
    ]]--

    return "some data"
end 
Run Code Online (Sandbox Code Playgroud)

我唯一想要的是String("some data")from getLoggedIn(),但我得到的只是一个错误:

...\corona\menu.lua:51:尝试调用字段'getLoggedIn'(零值)

永远不会达到外包.我正在研究Corona SDK和Sublime,所需的数据来自isLoggedIn()sqlite-request.我怎样才能达到这个功能?

Yu *_*Hao 6

编写模块的一种直接方法是返回一个包含所需函数的表:

local M = {}

function M.getLoggedIn()
    print("Test")
    --...
    return "some data"
end 

return M
Run Code Online (Sandbox Code Playgroud)

请注意,该函数必须是非函数local,或者它是私有函数.

有关其他高级方法,请参阅PiL.