Accessing other Lua files?

Hey all,

After my main.lua script passes control to splash.lua, I need splash to call a function inside main.lua. For that I’ve simply added:

local main = require("main")

to the top of splash.lua. Further down in the file I’ve then attempted to invoke a call to the onFirstView function (which resides in main.lua) which calls composer.gotoScene(“view1”) along with a few other bits of code (setting up the tab bar, sounds etc). 

When the splash script has done its thing, the call to this main local variable is hit (I’ve tried a variety of calls that have been suggested in other Q&A boards) and the error I get is one that appears to suggest that require(“main”) isn’t doing what I want it to do:
 

“splash.lua 92: attempt to index upvalue ‘main’ (a boolean value)”

I’m sure it’s something simple newbies trip up over on their adventures through Lua - anyone care to point me in the right direction? Thanks :slight_smile:

you cant directly grab functions which havent been placed in a table through your require function. In very simple terms, the require function basically returns the the thing you return at the end of the lua file you passed as a parameter. So, you need to do something like this.

main.lua

local m = {} -- it doesn''t have to be at the top, just in scope of the functions you want to place in it -- all the code from your main here m.kindaglobalishfunction = function (...)     for \_,v in ipairs(arg) do         print(v)     end end return m -- CRUCIAL!

splash.lua

local main = require "main"   main.kindaglobalishfunction("hello","it works!")

Hey lemonade, thanks for the response.

Unfortunately I still get the same error message. Is it possible ‘main’ is a reserved keyword?

Also, how would one go about fetching a variable? I understand ‘local’ variables aren’t accessible via other files - I assume a getMyVar func would be able to return the variable?

For clarity:

splash.lua:

At the head of the file:

local main = require "main"

Later on in splash.lua:

local function onEnterFrame(event) ... elseif os.time() \> (timeFadeComplete + 3) then main.onFirstView() end end end

main.lua:

At the top of the file:

– include Corona’s “widget” library

local widget = require “widget”

local composer = require “composer”

local tabBar = nil

local m = {}

– event listeners for tab buttons:

m.onFirstView = function( event )

tabBar = widget.newTabBar{

top = display.contentHeight - 50, – 50 is default height for tabBar widget

buttons = tabButtons

}

print(“Bar created”)

composer.gotoScene( “view1” )

end

And then as you say, ‘return m’ at the end of the file.

Edit: Is it possible that I’m not providing a correct filepath? Seen that error sprawled across the net. 

ah i see its my fault. It seems main as a filename is not requireable … thats strange. Im not sure how well it would work with composer either, If anything ive learnt the most from this. The alternative is to have a seperate .lua file which contains all of your functions, variables, tables, etc that you use regularly … . Sorry for not answering properly :unsure:

Regards, Lemon

You cannot require main.lua.  It’s the main app, it’s already loaded.  You can make those functions global, but globals are bad. The best way is to create a new lua file, perhaps called “functions.lua”, “utility.lua”, “stuff.lua” or whatever works for you (I use utility.lua).  Then in that lua file do:

local M = {} function M.someFunction( param )      -- do stuff      return someValue -- if needed end   function M.someOtherFunctioName(param)      -- do stuff      return someValue -- if needed end   return M

Then in any .lua file you need those functions:

local utility = require( “utility” )

utility.someFunction()

Rob

That would make sense Rob - Lua’s a whole different world to C++, I do apologise! At least ‘globals are bad’ is consistent  :wink:

Don’t worry about it Lemon, I just appreciate the intent to help! 

Thanks to both of you :slight_smile:

Final question!

I’ve taken the Corona tabBar template and began building on that. My intent has always been to have a splash screen open up, do its magic and then make a call back to main.lua to onFirstView(), which will load the first tab. 

It’s that that I’m struggling with atm. I could move these functions to another lua file and then require composer within there, but then it just seems like I’m essentially doing exactly the same thing as I’m doing in main anyway…  :huh:

Alternatively I just rewrite most of the template to accommodate it :smiley: But before doing that I deemed it wise to ask your advice. 

You might want to look at our Business App Sample here:

https://github.com/coronalabs/business-app-sample

It is a tabbar based app that has a splash screen.

Rob

you cant directly grab functions which havent been placed in a table through your require function. In very simple terms, the require function basically returns the the thing you return at the end of the lua file you passed as a parameter. So, you need to do something like this.

main.lua

local m = {} -- it doesn''t have to be at the top, just in scope of the functions you want to place in it -- all the code from your main here m.kindaglobalishfunction = function (...)     for \_,v in ipairs(arg) do         print(v)     end end return m -- CRUCIAL!

splash.lua

local main = require "main"   main.kindaglobalishfunction("hello","it works!")

Hey lemonade, thanks for the response.

Unfortunately I still get the same error message. Is it possible ‘main’ is a reserved keyword?

Also, how would one go about fetching a variable? I understand ‘local’ variables aren’t accessible via other files - I assume a getMyVar func would be able to return the variable?

For clarity:

splash.lua:

At the head of the file:

local main = require "main"

Later on in splash.lua:

local function onEnterFrame(event) ... elseif os.time() \> (timeFadeComplete + 3) then main.onFirstView() end end end

main.lua:

At the top of the file:

– include Corona’s “widget” library

local widget = require “widget”

local composer = require “composer”

local tabBar = nil

local m = {}

– event listeners for tab buttons:

m.onFirstView = function( event )

tabBar = widget.newTabBar{

top = display.contentHeight - 50, – 50 is default height for tabBar widget

buttons = tabButtons

}

print(“Bar created”)

composer.gotoScene( “view1” )

end

And then as you say, ‘return m’ at the end of the file.

Edit: Is it possible that I’m not providing a correct filepath? Seen that error sprawled across the net. 

ah i see its my fault. It seems main as a filename is not requireable … thats strange. Im not sure how well it would work with composer either, If anything ive learnt the most from this. The alternative is to have a seperate .lua file which contains all of your functions, variables, tables, etc that you use regularly … . Sorry for not answering properly :unsure:

Regards, Lemon

You cannot require main.lua.  It’s the main app, it’s already loaded.  You can make those functions global, but globals are bad. The best way is to create a new lua file, perhaps called “functions.lua”, “utility.lua”, “stuff.lua” or whatever works for you (I use utility.lua).  Then in that lua file do:

local M = {} function M.someFunction( param )      -- do stuff      return someValue -- if needed end   function M.someOtherFunctioName(param)      -- do stuff      return someValue -- if needed end   return M

Then in any .lua file you need those functions:

local utility = require( “utility” )

utility.someFunction()

Rob

That would make sense Rob - Lua’s a whole different world to C++, I do apologise! At least ‘globals are bad’ is consistent  :wink:

Don’t worry about it Lemon, I just appreciate the intent to help! 

Thanks to both of you :slight_smile:

Final question!

I’ve taken the Corona tabBar template and began building on that. My intent has always been to have a splash screen open up, do its magic and then make a call back to main.lua to onFirstView(), which will load the first tab. 

It’s that that I’m struggling with atm. I could move these functions to another lua file and then require composer within there, but then it just seems like I’m essentially doing exactly the same thing as I’m doing in main anyway…  :huh:

Alternatively I just rewrite most of the template to accommodate it :smiley: But before doing that I deemed it wise to ask your advice. 

You might want to look at our Business App Sample here:

https://github.com/coronalabs/business-app-sample

It is a tabbar based app that has a splash screen.

Rob