How to call a function from another Lua file?

Hey guys,

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?

If so, can you explain the syntax of doing so?

Roy.

Hi Roy,

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 :slight_smile:

Let me know if you have any problems!

Great detailed answer! much appreciate it TandG

Ill give it a try now and let you know

Thanks!!

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:

http://www.lua.org/pil/contents.html

Hi Roy,
 
Yes you can. The basics are fairly straightforward.
 
So in ‘main.lua’ something like: 

require("scripts.myOtherLuaFile"); helloWorld(); 

In ‘myOtherLuaFile.lua’:

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(); 

Hope that helps!

It does help and iv’e tried it and it works!

What about multiple functions in the other file?

should it be like this ?? :

[lua]

local M1={};

function M1.helloWorld() 

     print(“Hello World”);

end;

return M1; 

– new function start here

local M2={};

function M2.heyThereImANewFunction()

      print(“heyThereImANewFunction”);

end;

return M2;

[/lua]

Roy.

You want something like this.

[lua]

local M={};

function M.helloWorld() 

     print(“Hello World”)

end

– new function start here

function M.heyThereImANewFunction()

      print(“heyThereImANewFunction”)

end

return M;

[/lua]

Look for module tutorials on http://www.tandgapps.co.uk/resources/coronalabs-resources/

As well as basic Lua tables information is important.

Bookmarked it, some useful stuff in there! thanks for sharing!

Roy.

Hi Roy,

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 :slight_smile:

Let me know if you have any problems!

Great detailed answer! much appreciate it TandG

Ill give it a try now and let you know

Thanks!!

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:

http://www.lua.org/pil/contents.html

Hi Roy,
 
Yes you can. The basics are fairly straightforward.
 
So in ‘main.lua’ something like: 

require("scripts.myOtherLuaFile"); helloWorld(); 

In ‘myOtherLuaFile.lua’:

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(); 

Hope that helps!

It does help and iv’e tried it and it works!

What about multiple functions in the other file?

should it be like this ?? :

[lua]

local M1={};

function M1.helloWorld() 

     print(“Hello World”);

end;

return M1; 

– new function start here

local M2={};

function M2.heyThereImANewFunction()

      print(“heyThereImANewFunction”);

end;

return M2;

[/lua]

Roy.

You want something like this.

[lua]

local M={};

function M.helloWorld() 

     print(“Hello World”)

end

– new function start here

function M.heyThereImANewFunction()

      print(“heyThereImANewFunction”)

end

return M;

[/lua]

Look for module tutorials on http://www.tandgapps.co.uk/resources/coronalabs-resources/

As well as basic Lua tables information is important.

Bookmarked it, some useful stuff in there! thanks for sharing!

Roy.