Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

How to call a function from another Lua file?
Started by POM Aug 08 2013 11:07 PM

7 replies to this topic
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

POM

[GLOBAL: userInfoPane.html]
POM
  • Contributor

  • 124 posts
  • Corona SDK

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.



[TOPIC: post.html]
#2

TandG

[GLOBAL: userInfoPane.html]
TandG
  • Contributor

  • 420 posts
  • Corona SDK

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.

 

-- scriptTest.lua (in your scripts directory)
local M = {}
 
local function testFunction()
      print("Test function called")
end
M.testFunction = testFunction
 
return M

 

Now in your main.lua file you would do something like the following:

--main.lua
local scriptTest = require("scripts.scriptTest")
scriptTest.testFunction()

 

None of that has been tested as i just wrote it now, but it should all work fine :)

Let me know if you have any problems!

 

 

 

 

 



[TOPIC: post.html]
#3

POM

[GLOBAL: userInfoPane.html]
POM
  • Contributor

  • 124 posts
  • Corona SDK

Great detailed answer! much appreciate it TandG

Ill give it a try now and let you know

 

Thanks!!



[TOPIC: post.html]
#4

SegaBoy

[GLOBAL: userInfoPane.html]
SegaBoy
  • Contributor

  • 635 posts
  • Corona SDK

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



[TOPIC: post.html]
#5

keirakaine

[GLOBAL: userInfoPane.html]
keirakaine
  • Observer

  • 1 posts
  • Corona SDK

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!



[TOPIC: post.html]
#6

POM

[GLOBAL: userInfoPane.html]
POM
  • Contributor

  • 124 posts
  • Corona SDK

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

 

What about multiple functions in the other file?

 

should it be like this ?? :

 

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;

 

Roy.



[TOPIC: post.html]
#7

jonjonsson

[GLOBAL: userInfoPane.html]
jonjonsson
  • Corona Geek

  • 1,051 posts
  • Corona SDK

You want something like this.

 

local M={};

function M.helloWorld() 
     print("Hello World")
end
 
-- new function start here
function M.heyThereImANewFunction()
      print("heyThereImANewFunction")
end

return M;

 

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

As well as basic Lua tables information is important.



[TOPIC: post.html]
#8

POM

[GLOBAL: userInfoPane.html]
POM
  • Contributor

  • 124 posts
  • Corona SDK

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

 

Roy.




[topic_controls]
[/topic_controls]