Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling function from a different lua-file

I have this in menu.lua

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

and this in databaseconnection.lua

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

    return "some data"
end 

The only thing I want is that String ("some data") from getLoggedIn(), but all I get is an error:

...\corona\menu.lua:51:attempt to call field 'getLoggedIn' (a nil value)

The outprint is never reached. I'm working on Corona SDK and Sublime, the needed data from isLoggedIn() comes from a sqlite-request. How can I reach that function?

like image 483
Arthur Avatar asked Mar 10 '14 14:03

Arthur


1 Answers

One direct way to write a module is to return a table that includes the functions you need:

local M = {}

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

return M

Note that the function needs to be non-local, or it would be private.

See PiL for other advanced methods.

like image 143
Yu Hao Avatar answered Sep 19 '22 19:09

Yu Hao