Calling a function in main.lua from inside of a require()'d lua script?

Hello,

I’m still a beginner at Lua and I could use some help. I’ve written a tutorial for my app that I organized into a file called “tutorial.lua” which runs when the app is first opened. For example:

In main.lua:

local setupMainDisplay  
  
setupMainDisplay = function()  
 -- start running the main app code after the tutorial has been completed  
end  
  
-- Load up the tutorial code  
local tutorial = require("tutorial")  
  
-- Call the displayTutorial() function, which resides in tutorial.lua  
displayTutorial()  

Once the tutorial is complete, I wish to call a function in my main.lua called setupMainDisplay(). So…

In tutorial.lua

  
closeTutorial = function()  
 -- remove all of the tutorial objects  
  
 -- call setupMainDisplay, which resides in main.lua  
 setupMainDisplay()  
end  
  

Unfortunately, the code in tutorial.lua thinks that setupMainDisplay is nil. Any suggestions?

Thanks,

  • Bret [import]uid: 168791 topic_id: 30682 reply_id: 330682[/import]

Make setupMainDisplay global, not local. That will fix your problem.
[import]uid: 7563 topic_id: 30682 reply_id: 122936[/import]

Awesome. Worked like a charm. Thanks! [import]uid: 168791 topic_id: 30682 reply_id: 122939[/import]

Make setupMainDisplay global, not local. That will fix your problem.
[import]uid: 7563 topic_id: 30682 reply_id: 122936[/import]

Awesome. Worked like a charm. Thanks! [import]uid: 168791 topic_id: 30682 reply_id: 122939[/import]