Ive read some corona articles about storyboard and also about organised project…
I would like to split my code into various Lua files, so the main.lua remains in the parent directory and all the other lua file are in a ‘scripts’ directory.
Can I call a function that is located inside a different lua file, and call it from the main.lua file?
Yes thats definitely possible and quite a good way of doing things! I’ll show a quick example of another Lua file and then how to access it from the main.lua file below.
[lua]
– scriptTest.lua (in your scripts directory)
local M = {}
local function testFunction()
print(“Test function called”)
end
M.testFunction = testFunction
return M
[/lua]
Now in your main.lua file you would do something like the following:
[lua]
–main.lua
local scriptTest = require(“scripts.scriptTest”)
scriptTest.testFunction()
[/lua]
None of that has been tested as i just wrote it now, but it should all work fine
If you need information about Lua, can I recommend checking out Programming in Lua. The First Edition is available free online and is essential for any newcomers to Corona:
function helloWorld() print("Hello World"); end;
“scripts.myOtherLuaFile” uses dot notation for the path, so “scripts/interface/other/myOtherLuaFile.lua” becomes “scripts.interface.other.myOtherLuaFile”.
I rarely do this however, because you end up with scoping issues and loads of global functions.
A better solution is to use ‘modular classes’. This emulates class and object behaviour in lua and keeps things a lot neater.
So ‘myOtherLuaFile.lua’ becomes:
local publicClass={}; function publicClass.helloWorld() print("Hello World"); end; return publicClass;
In ‘main.lua’
local myClass = require("scripts.myOtherLuaFile"); myClass.helloWorld();
Yes thats definitely possible and quite a good way of doing things! I’ll show a quick example of another Lua file and then how to access it from the main.lua file below.
[lua]
– scriptTest.lua (in your scripts directory)
local M = {}
local function testFunction()
print(“Test function called”)
end
M.testFunction = testFunction
return M
[/lua]
Now in your main.lua file you would do something like the following:
[lua]
–main.lua
local scriptTest = require(“scripts.scriptTest”)
scriptTest.testFunction()
[/lua]
None of that has been tested as i just wrote it now, but it should all work fine
If you need information about Lua, can I recommend checking out Programming in Lua. The First Edition is available free online and is essential for any newcomers to Corona:
function helloWorld() print("Hello World"); end;
“scripts.myOtherLuaFile” uses dot notation for the path, so “scripts/interface/other/myOtherLuaFile.lua” becomes “scripts.interface.other.myOtherLuaFile”.
I rarely do this however, because you end up with scoping issues and loads of global functions.
A better solution is to use ‘modular classes’. This emulates class and object behaviour in lua and keeps things a lot neater.
So ‘myOtherLuaFile.lua’ becomes:
local publicClass={}; function publicClass.helloWorld() print("Hello World"); end; return publicClass;
In ‘main.lua’
local myClass = require("scripts.myOtherLuaFile"); myClass.helloWorld();