How do we connect external functions to main.lua?

Hi,

Right now we have all our code gathered in main.lua. We don’t want to work with object oriented code, but still find an easy way of splitting the different objects up into separated files.

In our main.lua file we have objects like water, boat, boy, island and cloud - all together creating one massive bit of code. We want to have “BEGIN WATER 3” in it’s own lua file and be able to execute that code in main.lua with a simple function instead. How do we do that?

Here an example from our main.lua file, displaying “water3”:

[lua]--------------- BEGIN WATER 3 ---------------------------------------------------------

local watere = display.newImage( “water3.png”, true )
game:insert( watere )
watere.y = 619
watere.x = 500
watere.xScale = 2

–water sound
local wavesound5 = media.newEventSound(“waves.wav”)

local function playWave5 (event)
media.playEventSound(wavesound5)
end

local w,h = display.contentWidth, display.contentHeight

local function callbackFunc()
print( “Transition 1 completed” )
end

local function mainwater(watere)
end

function loopar()
local myTween = transition.to(watere, {time=2300, x=(400), y=(h-140), transition=easing.inOutQuad, onComplete=loopar2})
end

function loopar2()
local myTween = transition.to(watere, {time=2200, x=(w-500), y=(h-120), transition=easing.inOutQuad, onComplete=loopar})
end

local listener2 = function()
print( “Transition 2 completed” )
end

local myTween = transition.to(watere, {time=2300, x=(w-400), y=(h-140), transition=easing.inOutQuad, onComplete=loopar})

watere:addEventListener(“touch”, playWave5)

---------------- END WATER 3 ---------------------------------------------------------
[import]uid: 46622 topic_id: 9898 reply_id: 309898[/import]

One piece of advice first: read the forum rules. You should use the “lua” tag whenever possible. Anyway:

[lua]In file “water3.lua”:

local watere = display.newImage( “water3.png”, true )
game:insert( watere )
watere.y = 619
watere.x = 500
watere.xScale = 2

–water sound
local wavesound5 = media.newEventSound(“waves.wav”)

local function playWave5 (event)
media.playEventSound(wavesound5)
end

local w,h = display.contentWidth, display.contentHeight

local function callbackFunc()
print( “Transition 1 completed” )
end

local function mainwater(watere)
end

function loopar()
local myTween = transition.to(watere, {time=2300, x=(400), y=(h-140), transition=easing.inOutQuad, onComplete=loopar2})
end

function loopar2()
local myTween = transition.to(watere, {time=2200, x=(w-500), y=(h-120), transition=easing.inOutQuad, onComplete=loopar})
end

local listener2 = function()
print( “Transition 2 completed” )
end

local myTween = transition.to(watere, {time=2300, x=(w-400), y=(h-140), transition=easing.inOutQuad, onComplete=loopar})

watere:addEventListener(“touch”, playWave5) [/lua]

[lua]In file “main.lua”:
require( “water3” ) – executes water3.lua code[/lua]

Done. Unless you want to access water3 functions inside main.lua, which you didn’t mention in your post, but this is another story. [import]uid: 51516 topic_id: 9898 reply_id: 36108[/import]

Thank you for answering!

Our main purpose is to organize the main.lua file. Right now we have objects like water, boat, boy, island and cloud gathered in main.lua. We want to cut each object out and organize them into separate lua files. Because of the structure in lua, some objects appear infront or behind of other objects (made by purpose). So, when cutting objects out into separate files, they need to keep their same position in the “layer hierarchy”.

Then we need to access each object in main.lua, with (if possible) some simple function. [import]uid: 46622 topic_id: 9898 reply_id: 36118[/import]

And if I wanted to access the functions and local variables? [import]uid: 73502 topic_id: 9898 reply_id: 67794[/import]

Check out the following resources, they will help you;

http://blog.anscamobile.com/2011/09/tutorial-modular-classes-in-corona/
http://blog.anscamobile.com/2011/09/a-better-approach-to-external-modules/
http://blog.anscamobile.com/2011/07/using-external-modules-in-corona/

Peach :slight_smile: [import]uid: 52491 topic_id: 9898 reply_id: 67885[/import]